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/README.md

82 lines
1.6 KiB
Markdown
Raw Normal View History

DependencyInjection Component
=============================
2011-12-18 13:18:13 +00:00
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:
2014-09-26 11:51:50 +01:00
```php
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
2011-12-18 13:18:13 +00:00
2014-09-26 11:51:50 +01:00
$sc = new ContainerBuilder();
$sc
->register('foo', '%foo.class%')
->addArgument(new Reference('bar'))
;
$sc->setParameter('foo.class', 'Foo');
2011-12-18 13:18:13 +00:00
2014-09-26 11:51:50 +01:00
$sc->get('foo');
```
Method Calls (Setter Injection):
2014-09-26 11:51:50 +01:00
```php
$sc = new ContainerBuilder();
2014-09-26 11:51:50 +01:00
$sc
->register('bar', '%bar.class%')
->addMethodCall('setFoo', array(new Reference('foo')))
;
$sc->setParameter('bar.class', 'Bar');
2014-09-26 11:51:50 +01:00
$sc->get('bar');
```
Factory Class:
If your service is retrieved by calling a static method:
2014-09-26 11:51:50 +01:00
```php
$sc = new ContainerBuilder();
2014-09-26 11:51:50 +01:00
$sc
->register('bar', '%bar.class%')
->setFactoryClass('%bar.class%')
->setFactoryMethod('getInstance')
->addArgument('Aarrg!!!')
;
$sc->setParameter('bar.class', 'Bar');
2014-09-26 11:51:50 +01:00
$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.
2014-09-26 11:51:50 +01:00
```php
$sc = new ContainerBuilder();
2014-09-26 11:51:50 +01:00
$sc
->register('bar', '%bar.class%')
->setFile('/path/to/file')
->addArgument('Aarrg!!!')
;
$sc->setParameter('bar.class', 'Bar');
2014-09-26 11:51:50 +01:00
$sc->get('bar');
```
2012-04-20 06:18:49 +01:00
Resources
---------
You can run the unit tests with the following command:
$ cd path/to/Symfony/Component/DependencyInjection/
$ composer.phar install
$ phpunit