thispagecannotbefound / silex-php-di
Silex Service Provider for the PHP-DI Dependency Injection Container.
Requires
- acclimate/container: ~1.0
- mnapoli/php-di: ~4.1
- silex/silex: ~1.1
This package is not auto-updated.
Last update: 2025-02-15 17:15:38 UTC
README
Silex Service Provider for the PHP-DI Dependency Injection Container. It does not replace Silex's default DI container Pimple, but rather adds the extra functionality PHP-DI offers.
Installation
This library is available on Packagist. To include it using Composer, add the following to your composer.json
:
"require": {
"thispagecannotbefound/silex-php-di": "*"
}
Registering
Assuming $app
is a Silex Application instance:
$app->register(new ThisPageCannotBeFound\Silex\Provider\PhpDiServiceProvider(), array(
'di.definitions' => '/path/to/config.php',
));
Parameters
The following parameters are available. Non are required.
di.definitions
: (array of) path(s) of your PHP-DI injection definitions.di.options
: array of options:cache
: Enables the use of a cache for the definitions. Must be aDoctrine\Common\Cache\Cache
instance. Defaults tonull
.container_class
: Name of the container class, used to create the DI container. Defaults toDI\Container
.useAnnotations
: Enable or disable the use of annotations to guess injections. Defaults totrue
.useAutowiring
: Enable or disable the use of autowiring to guess injections. Defaults totrue
.writeProxiesToFile
: If true, write the proxies to disk to improve performances. Defaults tofalse
.proxyDirectory
: Directory where to write the proxies. Defaults tonull
.silexAliases
: Add aliases for common Silex services. Defaults totrue
. (see below)injectOnControllers
: Fulfill controller dependencies after it has been resolved. Defaults totrue
. (see below)
Please refer to the official documentation for more information on container configuration.
silexAliases
This adds aliases for some common Silex service providers, for example:
return array (
'Doctrine\DBAL\Connection' => \DI\link('db'),
);
This means that when your class requests a Doctrine\DBAL\Connection
injection, it will get the same value as when requesting $app['db']
, which is the default defined in Silex's DoctrineServiceProvider
.
injectOnControllers
If you organize your controllers in classes, enabling this option will fulfill the dependencies of your controller instance. This is achieved by replacing the default Silex controller resolver with this provider's PhpDiControllerResolver
.
Services
The provider exposes the following services:
di
: The dependency injection container, instance of Acclimate'sCompositeContainer
. (see "Acclimate" section below)di.raw
: Instance of the wrappedDI\Container
instance. (see "Acclimate" section below)di.builder
: TheDI\ContainerBuilder
instance, allowing you to further configure or replace the builder.
Acclimate (container interoperability)
This Service Provider uses Acclimate, as suggested by the PHP-DI docs, to allow for PHP-DI and Silex's Pimple to work together. This is manifested through Acclimate's CompositeContainer
. The way it is configured will make values defined by PHP-DI have a higher priority. Consider the following:
<?php
// contents of definitions file, e.g. config.php
return array(
'foo' => 'bar'
);
// example Silex app, e.g. app.php (continuing from registration as demonstrated above)
$app['foo'] = 'baz'; // set Pimple value
$value = $app['di']->get('foo'); // the returned value will be "bar"
If the definitions file (config.php
) would not have had an entry for "foo", the result would have been "baz".
Also, this means that $app['di']
actually returns the generated CompositeContainer
instance. If, for some reason, you need to access the PHP-DI container, you can do so through $app['di.raw']
.