Julio Montoya 321ce65f18 Updating vendors | 11 年之前 | |
---|---|---|
.. | ||
Compiler | 11 年之前 | |
Dumper | 11 年之前 | |
Exception | 11 年之前 | |
Extension | 11 年之前 | |
LazyProxy | 11 年之前 | |
Loader | 11 年之前 | |
ParameterBag | 11 年之前 | |
Tests | 11 年之前 | |
Alias.php | 12 年之前 | |
CHANGELOG.md | 12 年之前 | |
Container.php | 11 年之前 | |
ContainerAware.php | 12 年之前 | |
ContainerAwareInterface.php | 12 年之前 | |
ContainerBuilder.php | 11 年之前 | |
ContainerInterface.php | 12 年之前 | |
Definition.php | 11 年之前 | |
DefinitionDecorator.php | 11 年之前 | |
IntrospectableContainerInterface.php | 12 年之前 | |
LICENSE | 12 年之前 | |
Parameter.php | 12 年之前 | |
README.md | 12 年之前 | |
Reference.php | 12 年之前 | |
Scope.php | 12 年之前 | |
ScopeInterface.php | 12 年之前 | |
SimpleXMLElement.php | 12 年之前 | |
TaggedContainerInterface.php | 12 年之前 | |
Variable.php | 12 年之前 | |
composer.json | 11 年之前 | |
phpunit.xml.dist | 12 年之前 |
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');
You can run the unit tests with the following command:
$ cd path/to/Symfony/Component/DependencyInjection/
$ composer.phar install --dev
$ phpunit