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
Fabien Potencier f66e109d14 Merge branch '2.2'
* 2.2:
  Fixed XmlFileLoaderTest::testLoadThrowsExceptionWithInvalidFileEvenWithoutSchemaValidation
  moved file hash calculation to own method
  [Validator] Add check for existing metadata on property
  added support for the X-Forwarded-For header (closes #6982, closes #7000)
  fixed the IP address in HttpCache when calling the backend
  [EventDispatcher] Added assertion.
  [EventDispathcer] Fix removeListener
  [DependencyInjection] Add clone for resources which were introduced in 2.1
  [DependencyInjection] Allow frozen containers to be dumped to graphviz
  Fix 'undefined index' error, when entering scope recursively
  [Security] fixed session creation on login (closes #7011)
  replaced usage of the deprecated pattern routing key (replaced with path)
  Add dot character `.` to legal mime subtype regular expression
  [HttpFoundation] fixed the creation of sub-requests under some circumstancies (closes #6923, closes #6936)
2013-02-11 12:27:01 +01:00
..
Compiler [DIC] Move PrependExtensionInterface to the Extension namespace 2013-01-23 17:42:45 +01:00
Dumper Merge branch '2.1' into 2.2 2013-02-11 12:26:43 +01:00
Exception updated license blocks 2012-03-31 18:00:32 -03:00
Extension merged branch vicb/fmwk/config (PR #6852) 2013-01-28 18:21:49 +01:00
Loader Fixed most of the docblocks/unused namespaces 2012-12-19 08:09:49 +01:00
ParameterBag merged 2.0 2012-09-17 22:41:57 +02:00
Tests Merge branch '2.1' into 2.2 2013-02-11 12:26:43 +01:00
.gitignore made usage of Composer autoloader for subtree-split unit tests 2012-11-09 14:10:06 +01:00
Alias.php fixed phpdoc @param alignment 2012-05-15 22:19:31 +02:00
CHANGELOG.md [DIC] Better handling of enableable configurations 2013-01-28 18:21:49 +01:00
composer.json Merge branch '2.2' 2013-02-11 11:29:42 +01:00
Container.php Merge branch '2.1' into 2.2 2013-02-11 12:26:43 +01:00
ContainerAware.php [DependencyInjection] made ContainerAware class abstract. 2012-05-30 09:01:58 +02:00
ContainerAwareInterface.php fixed CS 2012-07-09 14:54:20 +02:00
ContainerBuilder.php [DependencyInjection] enhanced some error messages 2013-02-06 16:38:36 +01:00
ContainerInterface.php merged 2.0 2012-09-17 22:41:57 +02:00
Definition.php Fixed most of the docblocks/unused namespaces 2012-12-19 08:09:49 +01:00
DefinitionDecorator.php Fixed most of the docblocks/unused namespaces 2012-12-19 08:09:49 +01:00
IntrospectableContainerInterface.php fixed CS 2012-07-09 14:54:20 +02:00
LICENSE updated license year 2013-01-04 17:59:43 +01:00
Parameter.php [DependencyInjection] tagged the public @api 2011-07-20 10:50:27 +02:00
phpunit.xml.dist made usage of Composer autoloader for subtree-split unit tests 2012-11-09 14:10:06 +01:00
README.md made usage of Composer autoloader for subtree-split unit tests 2012-11-09 14:10:06 +01:00
Reference.php [DependencyInjection] Made the reference case insensitive 2011-12-08 16:30:50 +01:00
Scope.php updated license blocks 2012-03-31 18:00:32 -03:00
ScopeInterface.php fixed CS 2012-07-09 14:54:20 +02:00
SimpleXMLElement.php [DependencyInjection] Using class from Config component to loading XML files 2012-12-14 15:34:01 +01:00
TaggedContainerInterface.php fixed CS 2012-07-09 14:54:20 +02:00
Variable.php fixed CS 2011-06-08 12:16:48 +02: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.phar install --dev
$ phpunit