e9e4869c98
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
-------
|
||
---|---|---|
.. | ||
Compiler | ||
Dumper | ||
Exception | ||
Extension | ||
LazyProxy | ||
Loader | ||
ParameterBag | ||
Tests | ||
.gitignore | ||
Alias.php | ||
CHANGELOG.md | ||
composer.json | ||
Container.php | ||
ContainerAwareInterface.php | ||
ContainerAwareTrait.php | ||
ContainerBuilder.php | ||
ContainerInterface.php | ||
Definition.php | ||
DefinitionDecorator.php | ||
ExpressionLanguage.php | ||
ExpressionLanguageProvider.php | ||
LICENSE | ||
Parameter.php | ||
phpunit.xml.dist | ||
README.md | ||
Reference.php | ||
ResettableContainerInterface.php | ||
TaggedContainerInterface.php | ||
Variable.php |
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