This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/DependencyInjection
2016-01-27 23:29:55 +01:00
..
Compiler resolve aliases in factory services 2016-01-27 08:57:17 +01:00
Dumper use nowdoc instead of heredoc 2015-12-21 17:05:00 +01:00
Exception [DependencyInjection] fixed ? position in some exception messages (refs #7861) 2013-05-01 06:24:56 +02:00
Extension [2.3] Static Code Analysis for Components 2016-01-12 12:31:34 +01:00
LazyProxy made {@inheritdoc} annotations consistent across the board 2014-04-16 09:04:20 +02:00
Loader [DependencyInjection] enforce tags to have a name 2016-01-27 23:29:55 +01:00
ParameterBag remove api tags from code 2015-09-28 19:11:22 +02:00
Tests [DependencyInjection] enforce tags to have a name 2016-01-27 23:29:55 +01:00
.gitignore Added missing files .gitignore 2013-07-21 14:12:18 +02:00
Alias.php remove api tags from code 2015-09-28 19:11:22 +02:00
CHANGELOG.md [DIC] Better handling of enableable configurations 2013-01-28 18:21:49 +01:00
composer.json added the new Composer exclude-from-classmap option 2015-10-30 12:48:51 -07:00
Container.php [2.3] Static Code Analysis for Components 2016-01-12 12:31:34 +01:00
ContainerAware.php [DI] Clean a phpdoc 2015-11-04 19:12:53 +01:00
ContainerAwareInterface.php [DI] Clean a phpdoc 2015-11-04 19:12:53 +01:00
ContainerBuilder.php [DependencyInjection] Unescape parameters for all types of injection 2015-11-19 15:08:33 +01:00
ContainerInterface.php remove api tags from code 2015-09-28 19:11:22 +02:00
Definition.php [DI] remove useless condition around unset 2015-11-26 18:53:25 +01:00
DefinitionDecorator.php remove api tags from code 2015-09-28 19:11:22 +02:00
IntrospectableContainerInterface.php [2.3] CS And DocBlock Fixes 2014-12-22 16:58:09 +01:00
LICENSE Update copyright year 2016-01-01 23:53:47 -03:00
Parameter.php remove api tags from code 2015-09-28 19:11:22 +02:00
phpunit.xml.dist Add missing exclusions from phpunit.xml.dist 2015-11-18 09:19:46 +01:00
README.md renamed composer.phar to composer to be consistent with the Symfony docs 2015-02-08 08:41:14 +01:00
Reference.php remove api tags from code 2015-09-28 19:11:22 +02:00
Scope.php remove api tags from code 2015-09-28 19:11:22 +02:00
ScopeInterface.php remove api tags from code 2015-09-28 19:11:22 +02:00
SimpleXMLElement.php Docblock fixes 2014-11-30 13:33:44 +00:00
TaggedContainerInterface.php remove api tags from code 2015-09-28 19:11:22 +02:00
Variable.php [2.3] CS And DocBlock Fixes 2014-12-22 16:58:09 +01:00

DependencyInjection Component

DependencyInjection manages your services via a robust and flexible Dependency Injection Container.

Here is a simple example that shows how to register services and parameters:

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

$sc = new ContainerBuilder();
$sc
    ->register('foo', '%foo.class%')
    ->addArgument(new Reference('bar'))
;
$sc->setParameter('foo.class', 'Foo');

$sc->get('foo');

Method Calls (Setter Injection):

$sc = new ContainerBuilder();

$sc
    ->register('bar', '%bar.class%')
    ->addMethodCall('setFoo', array(new Reference('foo')))
;
$sc->setParameter('bar.class', 'Bar');

$sc->get('bar');

Factory Class:

If your service is retrieved by calling a static method:

$sc = new ContainerBuilder();

$sc
    ->register('bar', '%bar.class%')
    ->setFactoryClass('%bar.class%')
    ->setFactoryMethod('getInstance')
    ->addArgument('Aarrg!!!')
;
$sc->setParameter('bar.class', 'Bar');

$sc->get('bar');

File Include:

For some services, especially those that are difficult or impossible to autoload, you may need the container to include a file before instantiating your class.

$sc = new ContainerBuilder();

$sc
    ->register('bar', '%bar.class%')
    ->setFile('/path/to/file')
    ->addArgument('Aarrg!!!')
;
$sc->setParameter('bar.class', 'Bar');

$sc->get('bar');

Resources

You can run the unit tests with the following command:

$ cd path/to/Symfony/Component/DependencyInjection/
$ composer install
$ phpunit