Skip to content

Support for Silex through a Service Provider #8

Description

@augustohp

A Service Provider would make Tardis easy to use by another developers, the provider should:

  • Provide a Tardis\Factory service.
  • Manage proxy factories internally.
  • Proxy an existing service, extending it.

Example

Consider this a suggestion, not as a contract to be followed or an example of implementation:

<?php

$app = new Silex\Application;
/** Other relevant configuration, such as cache storages. */ 
$app->register(new Computaria\Tardis\Bridge\SilexProvider); // provides `tardis.factory`

// Creating the proxy factories
$app['cache.persistent'] = $app->share(function($app) {
    return $app['tardis.factory']->grow($app['cache.storage.redis']);
});
$app['cache.volatile'] = $app->share(function($app) {
    return $app['tardis.factory']->grow($app['cache.storage.memcache']);
});

// Providing cached instances (using Pimple/Silex as container)
$app['user.repository'] = $app->share(function($app) {
    return $app['cache.persistent']->cacheCallsFrom('MyApp\Repository\Users');
});
$app['user.stats'] = $app->share(function($app) {
    return $app['cache.volatile']->cacheCallsFrom($app['user.model']);
});

As one can see, this is very verbose, if we could provide something to make it less verbose I would be very happy! 😄

$app['tardis.cache']->serviceUsingStorage('user.repository', $app['redis']);
$app['tardis.cache']->serviceUsingStorage('user.stats', $app['memcache']);

This would remove some flexibility (which can always be achieved through creating objects by hand, as shown previously), but making the factory already provide a service can make for much verbose declaration that does a lot of job for the developer:

  • Extend an existing service declaration, making it a cached-only service now.
  • Choose the best identity generator strategy available on the fly.
  • Automatically creates and keeps proxy factories for each storage and reusing them when possible.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions