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 e9e4869c98 feature #17132 [DependencyInjection] Properly ignore invalid reference arguments in collection arguments (ogizanagi)
This PR was merged into the 3.1-dev branch.

Discussion
----------

[DependencyInjection] Properly ignore invalid reference arguments in collection arguments

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

With this new feature, the following configuration:

```xml
<service id="baz" class="Baz"/>

<service id="foo" class="Foo">
    <argument type="collection">
        <argument type="service" id="bar" on-invalid="ignore" />
        <argument type="service" id="baz" />
        <argument type="service" id="moo" on-invalid="null" />
    </argument>

    <argument type="service" id="bar" on-invalid="ignore" />
    <argument type="service" id="baz" />
    <argument type="service" id="moo" on-invalid="null" />

    <call method="foo">
        <argument type="service" id="bar" on-invalid="ignore" />
    </call>
    <call method="fooCollection">
        <argument type="collection">
            <argument type="service" id="bar" on-invalid="ignore" />
            <argument type="service" id="baz" />
            <argument type="service" id="moo" on-invalid="null" />
        </argument>
    </call>
</service>
```

will result into the following `Definition`:

```php
Definition {#64 ▼
  -class: "Foo"
  // […]
  -calls: array:1 [▼
    0 => array:2 [▼
      0 => "fooCollection"
      1 => array:1 [▼
        0 => array:2 [▼
          0 => Reference {#59 ▼
            -id: "baz"
            -invalidBehavior: 1
          }
          1 => null
        ]
      ]
    ]
  ]
  // […]
  #arguments: array:4 [▼
    0 => array:2 [▼
      0 => Reference {#63 ▼
        -id: "baz"
        -invalidBehavior: 1
      }
      1 => null
    ]
    1 => null
    2 => Reference {#56 ▼
      -id: "baz"
      -invalidBehavior: 1
    }
    3 => null
  ]
}
```

Invalid references are ignored instead of replaced by `null` when they are part of a collection argument.
If the collection argument is part of a method call and contains an invalid reference, the method call is kept, but the invalid reference removed.

The behavior stays the same as before for non-collection arguments.

🎄

Commits
-------

cfc4879 [DependencyInjection] Properly ignore invalid reference arguments in collection arguments
2016-01-25 12:14:38 +01:00
..
Compiler feature #17132 [DependencyInjection] Properly ignore invalid reference arguments in collection arguments (ogizanagi) 2016-01-25 12:14:38 +01:00
Dumper minor #17465 Remove unused imports (Koc) 2016-01-21 10:53:31 +01:00
Exception [DI] Remove deprecated scope concept 2015-09-04 22:04:06 +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 feature #17323 [DependencyInjection] Deprecate unsupported attributes/elements for alias (Ener-Getick) 2016-01-16 15:18:33 +01:00
ParameterBag Merge branch '2.8' 2015-09-29 16:08:28 +02:00
Tests feature #17132 [DependencyInjection] Properly ignore invalid reference arguments in collection arguments (ogizanagi) 2016-01-25 12:14:38 +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 Merge branch '2.8' 2015-11-04 09:18:19 +01:00
composer.json updated version to 3.1 2015-11-30 22:39:17 +01:00
Container.php Merge branch '2.8' into 3.0 2016-01-12 18:46:41 +01:00
ContainerAwareInterface.php [DI] Clean a phpdoc 2015-11-04 19:12:53 +01:00
ContainerAwareTrait.php Merge branch '2.3' into 2.7 2015-11-04 19:50:51 +01:00
ContainerBuilder.php Merge branch '2.8' 2015-11-20 18:45:52 +01:00
ContainerInterface.php Merge branch '2.8' 2015-09-29 16:08:28 +02:00
Definition.php Merge branch '2.8' 2015-11-27 06:46:53 +01:00
DefinitionDecorator.php Merge branch '2.7' into 2.8 2016-01-21 10:05:51 +01:00
ExpressionLanguage.php Fixed minor typo - override 2015-01-03 21:58:03 +01:00
ExpressionLanguageProvider.php [DependencyInjection] added support for custom expression language functions 2014-09-25 19:08:38 +02: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 Merge branch '2.3' into 2.6 2015-02-11 08:12:14 +01:00
Reference.php Merge branch '2.8' 2015-09-29 16:08:28 +02:00
ResettableContainerInterface.php Implement resettable containers 2015-07-22 10:09:28 +02: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%')
    ->setFactory(array('%bar.class%', '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