\Dewdrop\Silex\ServiceControllerInjector

This class provides the ability to declare Silex controller dependencies by class name, where the dependencies are automatically injected into the controller.

It gives smart IDE's the ability to provide autocomplete support for such dependencies within controller functions, without requiring the developer to provide type hint docblocks or to reference such services by array syntax with hard-coded keys.

By default, classes are instantiated with no constructor arguments and require no configuration.

If a service needs a factory to instantiate it, provide the service class name as the key, and the Silex application key where the factory function exists as the value. For example, consider the following:

// Register the service
$silex->register(new ControllerInjector([
    'My\Service' => 'my-service',
]));

// Make a route for a simple dependency that can be instantiated with no constructor arguments
$silex->get('/run-something', function (Something $something) {
    return $something->report();
});

// Define a shared instance of My\Service, which requires some configuration during instantiation
$silex['my-service'] = $silex->share(function () {
    return new \My\Service(new SomeDependency());
});

// Make a route that declares My\Service as a dependency
$silex->get('/run-my-service', function (\My\Service $myService) {
    return $myService->run();
});

When the browser is directed to /run-something, this service automatically instantiates a new Something object and makes it available to the controller closure, so that the result of its report() method is returned.

When the browser is directed to /run-my-service, this service automatically fetches the My\Service object from the application container and passes it to the controller closure, so that the result of its run() method is returned.

Summary

Methods
Properties
Constants
__construct()
register()
boot()
No public properties found
No constants found
No protected methods found
$serviceFactoryMap
N/A
No private methods found
No private properties found
N/A

Properties

$serviceFactoryMap

$serviceFactoryMap : array

Map of service classes to factory functions.

Type

array

Methods

__construct()

__construct(array  $serviceFactoryMap = array()) 

Sets the map of services classes to factory functions.

Parameters

array $serviceFactoryMap

register()

register(\Silex\Application  $app) 

Registers services on the given app.

This method should only be used to configure services and parameters. It should not get services.

Parameters

\Silex\Application $app

boot()

boot(\Silex\Application  $app) 

Bootstraps the application.

This method is called after all services are registered and should be used for "dynamic" configuration (whenever a service must be requested).

Parameters

\Silex\Application $app