[DI] rename ResolveDefinitionTemplatesPass to ResolveChildDefinitionsPass
This commit is contained in:
parent
2fde0942ac
commit
a7f98c60d9
@ -52,6 +52,9 @@ DependencyInjection
|
|||||||
|
|
||||||
* Case insensitivity of parameter names is deprecated and will be removed in 4.0.
|
* Case insensitivity of parameter names is deprecated and will be removed in 4.0.
|
||||||
|
|
||||||
|
* The `ResolveDefinitionTemplatesPass` class is deprecated and will be removed in 4.0.
|
||||||
|
Use the `ResolveChildDefinitionsPass` class instead.
|
||||||
|
|
||||||
Debug
|
Debug
|
||||||
-----
|
-----
|
||||||
|
|
||||||
|
@ -159,6 +159,9 @@ DependencyInjection
|
|||||||
* The `DefinitionDecorator` class has been removed. Use the `ChildDefinition`
|
* The `DefinitionDecorator` class has been removed. Use the `ChildDefinition`
|
||||||
class instead.
|
class instead.
|
||||||
|
|
||||||
|
* The `ResolveDefinitionTemplatesPass` class has been removed.
|
||||||
|
Use the `ResolveChildDefinitionsPass` class instead.
|
||||||
|
|
||||||
* Using unsupported configuration keys in YAML configuration files raises an
|
* Using unsupported configuration keys in YAML configuration files raises an
|
||||||
exception.
|
exception.
|
||||||
|
|
||||||
|
@ -9,6 +9,7 @@ CHANGELOG
|
|||||||
* deprecated the ability to check for the initialization of a private service with the `Container::initialized()` method
|
* deprecated the ability to check for the initialization of a private service with the `Container::initialized()` method
|
||||||
* deprecated support for top-level anonymous services in XML
|
* deprecated support for top-level anonymous services in XML
|
||||||
* deprecated case insensitivity of parameter names
|
* deprecated case insensitivity of parameter names
|
||||||
|
* deprecated the `ResolveDefinitionTemplatesPass` class in favor of `ResolveChildDefinitionsPass`
|
||||||
|
|
||||||
3.3.0
|
3.3.0
|
||||||
-----
|
-----
|
||||||
|
@ -48,7 +48,7 @@ class PassConfig
|
|||||||
|
|
||||||
$this->optimizationPasses = array(array(
|
$this->optimizationPasses = array(array(
|
||||||
new ExtensionCompilerPass(),
|
new ExtensionCompilerPass(),
|
||||||
new ResolveDefinitionTemplatesPass(),
|
new ResolveChildDefinitionsPass(),
|
||||||
new ServiceLocatorTagPass(),
|
new ServiceLocatorTagPass(),
|
||||||
new DecoratorServicePass(),
|
new DecoratorServicePass(),
|
||||||
new ResolveParameterPlaceHoldersPass(false),
|
new ResolveParameterPlaceHoldersPass(false),
|
||||||
|
@ -0,0 +1,183 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Symfony\Component\DependencyInjection\Compiler;
|
||||||
|
|
||||||
|
use Symfony\Component\DependencyInjection\ChildDefinition;
|
||||||
|
use Symfony\Component\DependencyInjection\Definition;
|
||||||
|
use Symfony\Component\DependencyInjection\Exception\ExceptionInterface;
|
||||||
|
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This replaces all ChildDefinition instances with their equivalent fully
|
||||||
|
* merged Definition instance.
|
||||||
|
*
|
||||||
|
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
|
||||||
|
* @author Nicolas Grekas <p@tchwork.com>
|
||||||
|
*/
|
||||||
|
class ResolveChildDefinitionsPass extends AbstractRecursivePass
|
||||||
|
{
|
||||||
|
protected function processValue($value, $isRoot = false)
|
||||||
|
{
|
||||||
|
if (!$value instanceof Definition) {
|
||||||
|
return parent::processValue($value, $isRoot);
|
||||||
|
}
|
||||||
|
if ($isRoot) {
|
||||||
|
// yes, we are specifically fetching the definition from the
|
||||||
|
// container to ensure we are not operating on stale data
|
||||||
|
$value = $this->container->getDefinition($this->currentId);
|
||||||
|
}
|
||||||
|
if ($value instanceof ChildDefinition) {
|
||||||
|
$value = $this->resolveDefinition($value);
|
||||||
|
if ($isRoot) {
|
||||||
|
$this->container->setDefinition($this->currentId, $value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return parent::processValue($value, $isRoot);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resolves the definition.
|
||||||
|
*
|
||||||
|
* @return Definition
|
||||||
|
*
|
||||||
|
* @throws RuntimeException When the definition is invalid
|
||||||
|
*/
|
||||||
|
private function resolveDefinition(ChildDefinition $definition)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
return $this->doResolveDefinition($definition);
|
||||||
|
} catch (ExceptionInterface $e) {
|
||||||
|
$r = new \ReflectionProperty($e, 'message');
|
||||||
|
$r->setAccessible(true);
|
||||||
|
$r->setValue($e, sprintf('Service "%s": %s', $this->currentId, $e->getMessage()));
|
||||||
|
|
||||||
|
throw $e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function doResolveDefinition(ChildDefinition $definition)
|
||||||
|
{
|
||||||
|
if (!$this->container->has($parent = $definition->getParent())) {
|
||||||
|
throw new RuntimeException(sprintf('Parent definition "%s" does not exist.', $parent));
|
||||||
|
}
|
||||||
|
|
||||||
|
$parentDef = $this->container->findDefinition($parent);
|
||||||
|
if ($parentDef instanceof ChildDefinition) {
|
||||||
|
$id = $this->currentId;
|
||||||
|
$this->currentId = $parent;
|
||||||
|
$parentDef = $this->resolveDefinition($parentDef);
|
||||||
|
$this->container->setDefinition($parent, $parentDef);
|
||||||
|
$this->currentId = $id;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->container->log($this, sprintf('Resolving inheritance for "%s" (parent: %s).', $this->currentId, $parent));
|
||||||
|
$def = new Definition();
|
||||||
|
|
||||||
|
// merge in parent definition
|
||||||
|
// purposely ignored attributes: abstract, shared, tags, autoconfigured
|
||||||
|
$def->setClass($parentDef->getClass());
|
||||||
|
$def->setArguments($parentDef->getArguments());
|
||||||
|
$def->setMethodCalls($parentDef->getMethodCalls());
|
||||||
|
$def->setProperties($parentDef->getProperties());
|
||||||
|
if ($parentDef->getAutowiringTypes(false)) {
|
||||||
|
$def->setAutowiringTypes($parentDef->getAutowiringTypes(false));
|
||||||
|
}
|
||||||
|
if ($parentDef->isDeprecated()) {
|
||||||
|
$def->setDeprecated(true, $parentDef->getDeprecationMessage('%service_id%'));
|
||||||
|
}
|
||||||
|
$def->setFactory($parentDef->getFactory());
|
||||||
|
$def->setConfigurator($parentDef->getConfigurator());
|
||||||
|
$def->setFile($parentDef->getFile());
|
||||||
|
$def->setPublic($parentDef->isPublic());
|
||||||
|
$def->setLazy($parentDef->isLazy());
|
||||||
|
$def->setAutowired($parentDef->isAutowired());
|
||||||
|
$def->setChanges($parentDef->getChanges());
|
||||||
|
|
||||||
|
$def->setBindings($parentDef->getBindings());
|
||||||
|
|
||||||
|
// overwrite with values specified in the decorator
|
||||||
|
$changes = $definition->getChanges();
|
||||||
|
if (isset($changes['class'])) {
|
||||||
|
$def->setClass($definition->getClass());
|
||||||
|
}
|
||||||
|
if (isset($changes['factory'])) {
|
||||||
|
$def->setFactory($definition->getFactory());
|
||||||
|
}
|
||||||
|
if (isset($changes['configurator'])) {
|
||||||
|
$def->setConfigurator($definition->getConfigurator());
|
||||||
|
}
|
||||||
|
if (isset($changes['file'])) {
|
||||||
|
$def->setFile($definition->getFile());
|
||||||
|
}
|
||||||
|
if (isset($changes['public'])) {
|
||||||
|
$def->setPublic($definition->isPublic());
|
||||||
|
}
|
||||||
|
if (isset($changes['lazy'])) {
|
||||||
|
$def->setLazy($definition->isLazy());
|
||||||
|
}
|
||||||
|
if (isset($changes['deprecated'])) {
|
||||||
|
$def->setDeprecated($definition->isDeprecated(), $definition->getDeprecationMessage('%service_id%'));
|
||||||
|
}
|
||||||
|
if (isset($changes['autowired'])) {
|
||||||
|
$def->setAutowired($definition->isAutowired());
|
||||||
|
}
|
||||||
|
if (isset($changes['shared'])) {
|
||||||
|
$def->setShared($definition->isShared());
|
||||||
|
}
|
||||||
|
if (isset($changes['decorated_service'])) {
|
||||||
|
$decoratedService = $definition->getDecoratedService();
|
||||||
|
if (null === $decoratedService) {
|
||||||
|
$def->setDecoratedService($decoratedService);
|
||||||
|
} else {
|
||||||
|
$def->setDecoratedService($decoratedService[0], $decoratedService[1], $decoratedService[2]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// merge arguments
|
||||||
|
foreach ($definition->getArguments() as $k => $v) {
|
||||||
|
if (is_numeric($k)) {
|
||||||
|
$def->addArgument($v);
|
||||||
|
} elseif (0 === strpos($k, 'index_')) {
|
||||||
|
$def->replaceArgument((int) substr($k, strlen('index_')), $v);
|
||||||
|
} else {
|
||||||
|
$def->setArgument($k, $v);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// merge properties
|
||||||
|
foreach ($definition->getProperties() as $k => $v) {
|
||||||
|
$def->setProperty($k, $v);
|
||||||
|
}
|
||||||
|
|
||||||
|
// append method calls
|
||||||
|
if ($calls = $definition->getMethodCalls()) {
|
||||||
|
$def->setMethodCalls(array_merge($def->getMethodCalls(), $calls));
|
||||||
|
}
|
||||||
|
|
||||||
|
// merge autowiring types
|
||||||
|
foreach ($definition->getAutowiringTypes(false) as $autowiringType) {
|
||||||
|
$def->addAutowiringType($autowiringType);
|
||||||
|
}
|
||||||
|
|
||||||
|
// these attributes are always taken from the child
|
||||||
|
$def->setAbstract($definition->isAbstract());
|
||||||
|
$def->setTags($definition->getTags());
|
||||||
|
// autoconfigure is never taken from parent (on purpose)
|
||||||
|
// and it's not legal on an instanceof
|
||||||
|
$def->setAutoconfigured($definition->isAutoconfigured());
|
||||||
|
|
||||||
|
return $def;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class_alias(ResolveChildDefinitionsPass::class, ResolveDefinitionTemplatesPass::class);
|
@ -11,171 +11,19 @@
|
|||||||
|
|
||||||
namespace Symfony\Component\DependencyInjection\Compiler;
|
namespace Symfony\Component\DependencyInjection\Compiler;
|
||||||
|
|
||||||
use Symfony\Component\DependencyInjection\ChildDefinition;
|
@trigger_error('The '.__NAMESPACE__.'\ResolveDefinitionTemplatesPass class is deprecated since version 3.4 and will be removed in 4.0. Use the ResolveChildDefinitionsPass class instead.', E_USER_DEPRECATED);
|
||||||
use Symfony\Component\DependencyInjection\Definition;
|
|
||||||
use Symfony\Component\DependencyInjection\Exception\ExceptionInterface;
|
|
||||||
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
|
|
||||||
|
|
||||||
|
class_exists(ResolveChildDefinitionsPass::class);
|
||||||
|
|
||||||
|
if (false) {
|
||||||
/**
|
/**
|
||||||
* This replaces all ChildDefinition instances with their equivalent fully
|
* This definition decorates another definition.
|
||||||
* merged Definition instance.
|
|
||||||
*
|
*
|
||||||
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
|
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
|
||||||
* @author Nicolas Grekas <p@tchwork.com>
|
*
|
||||||
|
* @deprecated The ResolveDefinitionTemplatesPass class is deprecated since version 3.4 and will be removed in 4.0. Use the ResolveChildDefinitionsPass class instead.
|
||||||
*/
|
*/
|
||||||
class ResolveDefinitionTemplatesPass extends AbstractRecursivePass
|
class ResolveDefinitionTemplatesPass extends AbstractRecursivePass
|
||||||
{
|
{
|
||||||
protected function processValue($value, $isRoot = false)
|
|
||||||
{
|
|
||||||
if (!$value instanceof Definition) {
|
|
||||||
return parent::processValue($value, $isRoot);
|
|
||||||
}
|
|
||||||
if ($isRoot) {
|
|
||||||
// yes, we are specifically fetching the definition from the
|
|
||||||
// container to ensure we are not operating on stale data
|
|
||||||
$value = $this->container->getDefinition($this->currentId);
|
|
||||||
}
|
|
||||||
if ($value instanceof ChildDefinition) {
|
|
||||||
$value = $this->resolveDefinition($value);
|
|
||||||
if ($isRoot) {
|
|
||||||
$this->container->setDefinition($this->currentId, $value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return parent::processValue($value, $isRoot);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Resolves the definition.
|
|
||||||
*
|
|
||||||
* @return Definition
|
|
||||||
*
|
|
||||||
* @throws RuntimeException When the definition is invalid
|
|
||||||
*/
|
|
||||||
private function resolveDefinition(ChildDefinition $definition)
|
|
||||||
{
|
|
||||||
try {
|
|
||||||
return $this->doResolveDefinition($definition);
|
|
||||||
} catch (ExceptionInterface $e) {
|
|
||||||
$r = new \ReflectionProperty($e, 'message');
|
|
||||||
$r->setAccessible(true);
|
|
||||||
$r->setValue($e, sprintf('Service "%s": %s', $this->currentId, $e->getMessage()));
|
|
||||||
|
|
||||||
throw $e;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private function doResolveDefinition(ChildDefinition $definition)
|
|
||||||
{
|
|
||||||
if (!$this->container->has($parent = $definition->getParent())) {
|
|
||||||
throw new RuntimeException(sprintf('Parent definition "%s" does not exist.', $parent));
|
|
||||||
}
|
|
||||||
|
|
||||||
$parentDef = $this->container->findDefinition($parent);
|
|
||||||
if ($parentDef instanceof ChildDefinition) {
|
|
||||||
$id = $this->currentId;
|
|
||||||
$this->currentId = $parent;
|
|
||||||
$parentDef = $this->resolveDefinition($parentDef);
|
|
||||||
$this->container->setDefinition($parent, $parentDef);
|
|
||||||
$this->currentId = $id;
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->container->log($this, sprintf('Resolving inheritance for "%s" (parent: %s).', $this->currentId, $parent));
|
|
||||||
$def = new Definition();
|
|
||||||
|
|
||||||
// merge in parent definition
|
|
||||||
// purposely ignored attributes: abstract, shared, tags, autoconfigured
|
|
||||||
$def->setClass($parentDef->getClass());
|
|
||||||
$def->setArguments($parentDef->getArguments());
|
|
||||||
$def->setMethodCalls($parentDef->getMethodCalls());
|
|
||||||
$def->setProperties($parentDef->getProperties());
|
|
||||||
if ($parentDef->getAutowiringTypes(false)) {
|
|
||||||
$def->setAutowiringTypes($parentDef->getAutowiringTypes(false));
|
|
||||||
}
|
|
||||||
if ($parentDef->isDeprecated()) {
|
|
||||||
$def->setDeprecated(true, $parentDef->getDeprecationMessage('%service_id%'));
|
|
||||||
}
|
|
||||||
$def->setFactory($parentDef->getFactory());
|
|
||||||
$def->setConfigurator($parentDef->getConfigurator());
|
|
||||||
$def->setFile($parentDef->getFile());
|
|
||||||
$def->setPublic($parentDef->isPublic());
|
|
||||||
$def->setLazy($parentDef->isLazy());
|
|
||||||
$def->setAutowired($parentDef->isAutowired());
|
|
||||||
$def->setChanges($parentDef->getChanges());
|
|
||||||
|
|
||||||
$def->setBindings($parentDef->getBindings());
|
|
||||||
|
|
||||||
// overwrite with values specified in the decorator
|
|
||||||
$changes = $definition->getChanges();
|
|
||||||
if (isset($changes['class'])) {
|
|
||||||
$def->setClass($definition->getClass());
|
|
||||||
}
|
|
||||||
if (isset($changes['factory'])) {
|
|
||||||
$def->setFactory($definition->getFactory());
|
|
||||||
}
|
|
||||||
if (isset($changes['configurator'])) {
|
|
||||||
$def->setConfigurator($definition->getConfigurator());
|
|
||||||
}
|
|
||||||
if (isset($changes['file'])) {
|
|
||||||
$def->setFile($definition->getFile());
|
|
||||||
}
|
|
||||||
if (isset($changes['public'])) {
|
|
||||||
$def->setPublic($definition->isPublic());
|
|
||||||
}
|
|
||||||
if (isset($changes['lazy'])) {
|
|
||||||
$def->setLazy($definition->isLazy());
|
|
||||||
}
|
|
||||||
if (isset($changes['deprecated'])) {
|
|
||||||
$def->setDeprecated($definition->isDeprecated(), $definition->getDeprecationMessage('%service_id%'));
|
|
||||||
}
|
|
||||||
if (isset($changes['autowired'])) {
|
|
||||||
$def->setAutowired($definition->isAutowired());
|
|
||||||
}
|
|
||||||
if (isset($changes['shared'])) {
|
|
||||||
$def->setShared($definition->isShared());
|
|
||||||
}
|
|
||||||
if (isset($changes['decorated_service'])) {
|
|
||||||
$decoratedService = $definition->getDecoratedService();
|
|
||||||
if (null === $decoratedService) {
|
|
||||||
$def->setDecoratedService($decoratedService);
|
|
||||||
} else {
|
|
||||||
$def->setDecoratedService($decoratedService[0], $decoratedService[1], $decoratedService[2]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// merge arguments
|
|
||||||
foreach ($definition->getArguments() as $k => $v) {
|
|
||||||
if (is_numeric($k)) {
|
|
||||||
$def->addArgument($v);
|
|
||||||
} elseif (0 === strpos($k, 'index_')) {
|
|
||||||
$def->replaceArgument((int) substr($k, strlen('index_')), $v);
|
|
||||||
} else {
|
|
||||||
$def->setArgument($k, $v);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// merge properties
|
|
||||||
foreach ($definition->getProperties() as $k => $v) {
|
|
||||||
$def->setProperty($k, $v);
|
|
||||||
}
|
|
||||||
|
|
||||||
// append method calls
|
|
||||||
if ($calls = $definition->getMethodCalls()) {
|
|
||||||
$def->setMethodCalls(array_merge($def->getMethodCalls(), $calls));
|
|
||||||
}
|
|
||||||
|
|
||||||
// merge autowiring types
|
|
||||||
foreach ($definition->getAutowiringTypes(false) as $autowiringType) {
|
|
||||||
$def->addAutowiringType($autowiringType);
|
|
||||||
}
|
|
||||||
|
|
||||||
// these attributes are always taken from the child
|
|
||||||
$def->setAbstract($definition->isAbstract());
|
|
||||||
$def->setTags($definition->getTags());
|
|
||||||
// autoconfigure is never taken from parent (on purpose)
|
|
||||||
// and it's not legal on an instanceof
|
|
||||||
$def->setAutoconfigured($definition->isAutoconfigured());
|
|
||||||
|
|
||||||
return $def;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,413 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Symfony\Component\DependencyInjection\Tests\Compiler;
|
||||||
|
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use Symfony\Component\DependencyInjection\ChildDefinition;
|
||||||
|
use Symfony\Component\DependencyInjection\Compiler\ResolveChildDefinitionsPass;
|
||||||
|
use Symfony\Component\DependencyInjection\Compiler\ResolveDefinitionTemplatesPass;
|
||||||
|
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||||
|
|
||||||
|
class ResolveChildDefinitionsPassTest extends TestCase
|
||||||
|
{
|
||||||
|
public function testProcess()
|
||||||
|
{
|
||||||
|
$container = new ContainerBuilder();
|
||||||
|
$container->register('parent', 'foo')->setArguments(array('moo', 'b'))->setProperty('foo', 'moo');
|
||||||
|
$container->setDefinition('child', new ChildDefinition('parent'))
|
||||||
|
->replaceArgument(0, 'a')
|
||||||
|
->setProperty('foo', 'bar')
|
||||||
|
->setClass('bar')
|
||||||
|
;
|
||||||
|
|
||||||
|
$this->process($container);
|
||||||
|
|
||||||
|
$def = $container->getDefinition('child');
|
||||||
|
$this->assertNotInstanceOf(ChildDefinition::class, $def);
|
||||||
|
$this->assertEquals('bar', $def->getClass());
|
||||||
|
$this->assertEquals(array('a', 'b'), $def->getArguments());
|
||||||
|
$this->assertEquals(array('foo' => 'bar'), $def->getProperties());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testProcessAppendsMethodCallsAlways()
|
||||||
|
{
|
||||||
|
$container = new ContainerBuilder();
|
||||||
|
|
||||||
|
$container
|
||||||
|
->register('parent')
|
||||||
|
->addMethodCall('foo', array('bar'))
|
||||||
|
;
|
||||||
|
|
||||||
|
$container
|
||||||
|
->setDefinition('child', new ChildDefinition('parent'))
|
||||||
|
->addMethodCall('bar', array('foo'))
|
||||||
|
;
|
||||||
|
|
||||||
|
$this->process($container);
|
||||||
|
|
||||||
|
$def = $container->getDefinition('child');
|
||||||
|
$this->assertEquals(array(
|
||||||
|
array('foo', array('bar')),
|
||||||
|
array('bar', array('foo')),
|
||||||
|
), $def->getMethodCalls());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testProcessDoesNotCopyAbstract()
|
||||||
|
{
|
||||||
|
$container = new ContainerBuilder();
|
||||||
|
|
||||||
|
$container
|
||||||
|
->register('parent')
|
||||||
|
->setAbstract(true)
|
||||||
|
;
|
||||||
|
|
||||||
|
$container
|
||||||
|
->setDefinition('child', new ChildDefinition('parent'))
|
||||||
|
;
|
||||||
|
|
||||||
|
$this->process($container);
|
||||||
|
|
||||||
|
$def = $container->getDefinition('child');
|
||||||
|
$this->assertFalse($def->isAbstract());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testProcessDoesNotCopyShared()
|
||||||
|
{
|
||||||
|
$container = new ContainerBuilder();
|
||||||
|
|
||||||
|
$container
|
||||||
|
->register('parent')
|
||||||
|
->setShared(false)
|
||||||
|
;
|
||||||
|
|
||||||
|
$container
|
||||||
|
->setDefinition('child', new ChildDefinition('parent'))
|
||||||
|
;
|
||||||
|
|
||||||
|
$this->process($container);
|
||||||
|
|
||||||
|
$def = $container->getDefinition('child');
|
||||||
|
$this->assertTrue($def->isShared());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testProcessDoesNotCopyTags()
|
||||||
|
{
|
||||||
|
$container = new ContainerBuilder();
|
||||||
|
|
||||||
|
$container
|
||||||
|
->register('parent')
|
||||||
|
->addTag('foo')
|
||||||
|
;
|
||||||
|
|
||||||
|
$container
|
||||||
|
->setDefinition('child', new ChildDefinition('parent'))
|
||||||
|
;
|
||||||
|
|
||||||
|
$this->process($container);
|
||||||
|
|
||||||
|
$def = $container->getDefinition('child');
|
||||||
|
$this->assertEquals(array(), $def->getTags());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testProcessDoesNotCopyDecoratedService()
|
||||||
|
{
|
||||||
|
$container = new ContainerBuilder();
|
||||||
|
|
||||||
|
$container
|
||||||
|
->register('parent')
|
||||||
|
->setDecoratedService('foo')
|
||||||
|
;
|
||||||
|
|
||||||
|
$container
|
||||||
|
->setDefinition('child', new ChildDefinition('parent'))
|
||||||
|
;
|
||||||
|
|
||||||
|
$this->process($container);
|
||||||
|
|
||||||
|
$def = $container->getDefinition('child');
|
||||||
|
$this->assertNull($def->getDecoratedService());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testProcessDoesNotDropShared()
|
||||||
|
{
|
||||||
|
$container = new ContainerBuilder();
|
||||||
|
|
||||||
|
$container
|
||||||
|
->register('parent')
|
||||||
|
;
|
||||||
|
|
||||||
|
$container
|
||||||
|
->setDefinition('child', new ChildDefinition('parent'))
|
||||||
|
->setShared(false)
|
||||||
|
;
|
||||||
|
|
||||||
|
$this->process($container);
|
||||||
|
|
||||||
|
$def = $container->getDefinition('child');
|
||||||
|
$this->assertFalse($def->isShared());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testProcessHandlesMultipleInheritance()
|
||||||
|
{
|
||||||
|
$container = new ContainerBuilder();
|
||||||
|
|
||||||
|
$container
|
||||||
|
->register('parent', 'foo')
|
||||||
|
->setArguments(array('foo', 'bar', 'c'))
|
||||||
|
;
|
||||||
|
|
||||||
|
$container
|
||||||
|
->setDefinition('child2', new ChildDefinition('child1'))
|
||||||
|
->replaceArgument(1, 'b')
|
||||||
|
;
|
||||||
|
|
||||||
|
$container
|
||||||
|
->setDefinition('child1', new ChildDefinition('parent'))
|
||||||
|
->replaceArgument(0, 'a')
|
||||||
|
;
|
||||||
|
|
||||||
|
$this->process($container);
|
||||||
|
|
||||||
|
$def = $container->getDefinition('child2');
|
||||||
|
$this->assertEquals(array('a', 'b', 'c'), $def->getArguments());
|
||||||
|
$this->assertEquals('foo', $def->getClass());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSetLazyOnServiceHasParent()
|
||||||
|
{
|
||||||
|
$container = new ContainerBuilder();
|
||||||
|
|
||||||
|
$container->register('parent', 'stdClass');
|
||||||
|
|
||||||
|
$container->setDefinition('child1', new ChildDefinition('parent'))
|
||||||
|
->setLazy(true)
|
||||||
|
;
|
||||||
|
|
||||||
|
$this->process($container);
|
||||||
|
|
||||||
|
$this->assertTrue($container->getDefinition('child1')->isLazy());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSetLazyOnServiceIsParent()
|
||||||
|
{
|
||||||
|
$container = new ContainerBuilder();
|
||||||
|
|
||||||
|
$container->register('parent', 'stdClass')
|
||||||
|
->setLazy(true)
|
||||||
|
;
|
||||||
|
|
||||||
|
$container->setDefinition('child1', new ChildDefinition('parent'));
|
||||||
|
|
||||||
|
$this->process($container);
|
||||||
|
|
||||||
|
$this->assertTrue($container->getDefinition('child1')->isLazy());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSetAutowiredOnServiceHasParent()
|
||||||
|
{
|
||||||
|
$container = new ContainerBuilder();
|
||||||
|
|
||||||
|
$container->register('parent', 'stdClass')
|
||||||
|
->setAutowired(true)
|
||||||
|
;
|
||||||
|
|
||||||
|
$container->setDefinition('child1', new ChildDefinition('parent'))
|
||||||
|
->setAutowired(false)
|
||||||
|
;
|
||||||
|
|
||||||
|
$this->process($container);
|
||||||
|
|
||||||
|
$this->assertFalse($container->getDefinition('child1')->isAutowired());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSetAutowiredOnServiceIsParent()
|
||||||
|
{
|
||||||
|
$container = new ContainerBuilder();
|
||||||
|
|
||||||
|
$container->register('parent', 'stdClass')
|
||||||
|
->setAutowired(true)
|
||||||
|
;
|
||||||
|
|
||||||
|
$container->setDefinition('child1', new ChildDefinition('parent'));
|
||||||
|
|
||||||
|
$this->process($container);
|
||||||
|
|
||||||
|
$this->assertTrue($container->getDefinition('child1')->isAutowired());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testDeepDefinitionsResolving()
|
||||||
|
{
|
||||||
|
$container = new ContainerBuilder();
|
||||||
|
|
||||||
|
$container->register('parent', 'parentClass');
|
||||||
|
$container->register('sibling', 'siblingClass')
|
||||||
|
->setConfigurator(new ChildDefinition('parent'), 'foo')
|
||||||
|
->setFactory(array(new ChildDefinition('parent'), 'foo'))
|
||||||
|
->addArgument(new ChildDefinition('parent'))
|
||||||
|
->setProperty('prop', new ChildDefinition('parent'))
|
||||||
|
->addMethodCall('meth', array(new ChildDefinition('parent')))
|
||||||
|
;
|
||||||
|
|
||||||
|
$this->process($container);
|
||||||
|
|
||||||
|
$configurator = $container->getDefinition('sibling')->getConfigurator();
|
||||||
|
$this->assertSame('Symfony\Component\DependencyInjection\Definition', get_class($configurator));
|
||||||
|
$this->assertSame('parentClass', $configurator->getClass());
|
||||||
|
|
||||||
|
$factory = $container->getDefinition('sibling')->getFactory();
|
||||||
|
$this->assertSame('Symfony\Component\DependencyInjection\Definition', get_class($factory[0]));
|
||||||
|
$this->assertSame('parentClass', $factory[0]->getClass());
|
||||||
|
|
||||||
|
$argument = $container->getDefinition('sibling')->getArgument(0);
|
||||||
|
$this->assertSame('Symfony\Component\DependencyInjection\Definition', get_class($argument));
|
||||||
|
$this->assertSame('parentClass', $argument->getClass());
|
||||||
|
|
||||||
|
$properties = $container->getDefinition('sibling')->getProperties();
|
||||||
|
$this->assertSame('Symfony\Component\DependencyInjection\Definition', get_class($properties['prop']));
|
||||||
|
$this->assertSame('parentClass', $properties['prop']->getClass());
|
||||||
|
|
||||||
|
$methodCalls = $container->getDefinition('sibling')->getMethodCalls();
|
||||||
|
$this->assertSame('Symfony\Component\DependencyInjection\Definition', get_class($methodCalls[0][1][0]));
|
||||||
|
$this->assertSame('parentClass', $methodCalls[0][1][0]->getClass());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSetDecoratedServiceOnServiceHasParent()
|
||||||
|
{
|
||||||
|
$container = new ContainerBuilder();
|
||||||
|
|
||||||
|
$container->register('parent', 'stdClass');
|
||||||
|
|
||||||
|
$container->setDefinition('child1', new ChildDefinition('parent'))
|
||||||
|
->setDecoratedService('foo', 'foo_inner', 5)
|
||||||
|
;
|
||||||
|
|
||||||
|
$this->process($container);
|
||||||
|
|
||||||
|
$this->assertEquals(array('foo', 'foo_inner', 5), $container->getDefinition('child1')->getDecoratedService());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testDecoratedServiceCopiesDeprecatedStatusFromParent()
|
||||||
|
{
|
||||||
|
$container = new ContainerBuilder();
|
||||||
|
$container->register('deprecated_parent')
|
||||||
|
->setDeprecated(true)
|
||||||
|
;
|
||||||
|
|
||||||
|
$container->setDefinition('decorated_deprecated_parent', new ChildDefinition('deprecated_parent'));
|
||||||
|
|
||||||
|
$this->process($container);
|
||||||
|
|
||||||
|
$this->assertTrue($container->getDefinition('decorated_deprecated_parent')->isDeprecated());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testDecoratedServiceCanOverwriteDeprecatedParentStatus()
|
||||||
|
{
|
||||||
|
$container = new ContainerBuilder();
|
||||||
|
$container->register('deprecated_parent')
|
||||||
|
->setDeprecated(true)
|
||||||
|
;
|
||||||
|
|
||||||
|
$container->setDefinition('decorated_deprecated_parent', new ChildDefinition('deprecated_parent'))
|
||||||
|
->setDeprecated(false)
|
||||||
|
;
|
||||||
|
|
||||||
|
$this->process($container);
|
||||||
|
|
||||||
|
$this->assertFalse($container->getDefinition('decorated_deprecated_parent')->isDeprecated());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @group legacy
|
||||||
|
*/
|
||||||
|
public function testProcessMergeAutowiringTypes()
|
||||||
|
{
|
||||||
|
$container = new ContainerBuilder();
|
||||||
|
|
||||||
|
$container
|
||||||
|
->register('parent')
|
||||||
|
->addAutowiringType('Foo')
|
||||||
|
;
|
||||||
|
|
||||||
|
$container
|
||||||
|
->setDefinition('child', new ChildDefinition('parent'))
|
||||||
|
->addAutowiringType('Bar')
|
||||||
|
;
|
||||||
|
|
||||||
|
$this->process($container);
|
||||||
|
|
||||||
|
$childDef = $container->getDefinition('child');
|
||||||
|
$this->assertEquals(array('Foo', 'Bar'), $childDef->getAutowiringTypes());
|
||||||
|
|
||||||
|
$parentDef = $container->getDefinition('parent');
|
||||||
|
$this->assertSame(array('Foo'), $parentDef->getAutowiringTypes());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testProcessResolvesAliases()
|
||||||
|
{
|
||||||
|
$container = new ContainerBuilder();
|
||||||
|
|
||||||
|
$container->register('parent', 'ParentClass');
|
||||||
|
$container->setAlias('parent_alias', 'parent');
|
||||||
|
$container->setDefinition('child', new ChildDefinition('parent_alias'));
|
||||||
|
|
||||||
|
$this->process($container);
|
||||||
|
|
||||||
|
$def = $container->getDefinition('child');
|
||||||
|
$this->assertSame('ParentClass', $def->getClass());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testProcessSetsArguments()
|
||||||
|
{
|
||||||
|
$container = new ContainerBuilder();
|
||||||
|
|
||||||
|
$container->register('parent', 'ParentClass')->setArguments(array(0));
|
||||||
|
$container->setDefinition('child', (new ChildDefinition('parent'))->setArguments(array(
|
||||||
|
1,
|
||||||
|
'index_0' => 2,
|
||||||
|
'foo' => 3,
|
||||||
|
)));
|
||||||
|
|
||||||
|
$this->process($container);
|
||||||
|
|
||||||
|
$def = $container->getDefinition('child');
|
||||||
|
$this->assertSame(array(2, 1, 'foo' => 3), $def->getArguments());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSetAutoconfiguredOnServiceIsParent()
|
||||||
|
{
|
||||||
|
$container = new ContainerBuilder();
|
||||||
|
|
||||||
|
$container->register('parent', 'stdClass')
|
||||||
|
->setAutoconfigured(true)
|
||||||
|
;
|
||||||
|
|
||||||
|
$container->setDefinition('child1', new ChildDefinition('parent'));
|
||||||
|
|
||||||
|
$this->process($container);
|
||||||
|
|
||||||
|
$this->assertFalse($container->getDefinition('child1')->isAutoconfigured());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @group legacy
|
||||||
|
*/
|
||||||
|
public function testAliasExistsForBackwardsCompatibility()
|
||||||
|
{
|
||||||
|
$this->assertInstanceOf(ResolveChildDefinitionsPass::class, new ResolveDefinitionTemplatesPass());
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function process(ContainerBuilder $container)
|
||||||
|
{
|
||||||
|
$pass = new ResolveChildDefinitionsPass();
|
||||||
|
$pass->process($container);
|
||||||
|
}
|
||||||
|
}
|
@ -16,6 +16,9 @@ use Symfony\Component\DependencyInjection\ChildDefinition;
|
|||||||
use Symfony\Component\DependencyInjection\Compiler\ResolveDefinitionTemplatesPass;
|
use Symfony\Component\DependencyInjection\Compiler\ResolveDefinitionTemplatesPass;
|
||||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @group legacy
|
||||||
|
*/
|
||||||
class ResolveDefinitionTemplatesPassTest extends TestCase
|
class ResolveDefinitionTemplatesPassTest extends TestCase
|
||||||
{
|
{
|
||||||
public function testProcess()
|
public function testProcess()
|
||||||
|
@ -14,7 +14,7 @@ namespace Symfony\Component\DependencyInjection\Tests\Compiler;
|
|||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
use Symfony\Component\DependencyInjection\ChildDefinition;
|
use Symfony\Component\DependencyInjection\ChildDefinition;
|
||||||
use Symfony\Component\DependencyInjection\Compiler\ResolveInstanceofConditionalsPass;
|
use Symfony\Component\DependencyInjection\Compiler\ResolveInstanceofConditionalsPass;
|
||||||
use Symfony\Component\DependencyInjection\Compiler\ResolveDefinitionTemplatesPass;
|
use Symfony\Component\DependencyInjection\Compiler\ResolveChildDefinitionsPass;
|
||||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||||
|
|
||||||
class ResolveInstanceofConditionalsPassTest extends TestCase
|
class ResolveInstanceofConditionalsPassTest extends TestCase
|
||||||
@ -57,7 +57,7 @@ class ResolveInstanceofConditionalsPassTest extends TestCase
|
|||||||
$container->setDefinition('child', $def);
|
$container->setDefinition('child', $def);
|
||||||
|
|
||||||
(new ResolveInstanceofConditionalsPass())->process($container);
|
(new ResolveInstanceofConditionalsPass())->process($container);
|
||||||
(new ResolveDefinitionTemplatesPass())->process($container);
|
(new ResolveChildDefinitionsPass())->process($container);
|
||||||
|
|
||||||
$expected = array(
|
$expected = array(
|
||||||
array('foo', array('bar')),
|
array('foo', array('bar')),
|
||||||
@ -95,7 +95,7 @@ class ResolveInstanceofConditionalsPassTest extends TestCase
|
|||||||
));
|
));
|
||||||
|
|
||||||
(new ResolveInstanceofConditionalsPass())->process($container);
|
(new ResolveInstanceofConditionalsPass())->process($container);
|
||||||
(new ResolveDefinitionTemplatesPass())->process($container);
|
(new ResolveChildDefinitionsPass())->process($container);
|
||||||
|
|
||||||
$def = $container->getDefinition('foo');
|
$def = $container->getDefinition('foo');
|
||||||
$this->assertTrue($def->isAutowired());
|
$this->assertTrue($def->isAutowired());
|
||||||
@ -119,7 +119,7 @@ class ResolveInstanceofConditionalsPassTest extends TestCase
|
|||||||
->setFactory('autoconfigured_factory');
|
->setFactory('autoconfigured_factory');
|
||||||
|
|
||||||
(new ResolveInstanceofConditionalsPass())->process($container);
|
(new ResolveInstanceofConditionalsPass())->process($container);
|
||||||
(new ResolveDefinitionTemplatesPass())->process($container);
|
(new ResolveChildDefinitionsPass())->process($container);
|
||||||
|
|
||||||
$def = $container->getDefinition('normal_service');
|
$def = $container->getDefinition('normal_service');
|
||||||
// autowired thanks to the autoconfigured instanceof
|
// autowired thanks to the autoconfigured instanceof
|
||||||
@ -147,7 +147,7 @@ class ResolveInstanceofConditionalsPassTest extends TestCase
|
|||||||
;
|
;
|
||||||
|
|
||||||
(new ResolveInstanceofConditionalsPass())->process($container);
|
(new ResolveInstanceofConditionalsPass())->process($container);
|
||||||
(new ResolveDefinitionTemplatesPass())->process($container);
|
(new ResolveChildDefinitionsPass())->process($container);
|
||||||
|
|
||||||
$def = $container->getDefinition('normal_service');
|
$def = $container->getDefinition('normal_service');
|
||||||
$this->assertSame(array('duplicated_tag' => array(array(), array('and_attributes' => 1))), $def->getTags());
|
$this->assertSame(array('duplicated_tag' => array(array(), array('and_attributes' => 1))), $def->getTags());
|
||||||
@ -165,7 +165,7 @@ class ResolveInstanceofConditionalsPassTest extends TestCase
|
|||||||
->setAutowired(true);
|
->setAutowired(true);
|
||||||
|
|
||||||
(new ResolveInstanceofConditionalsPass())->process($container);
|
(new ResolveInstanceofConditionalsPass())->process($container);
|
||||||
(new ResolveDefinitionTemplatesPass())->process($container);
|
(new ResolveChildDefinitionsPass())->process($container);
|
||||||
|
|
||||||
$def = $container->getDefinition('normal_service');
|
$def = $container->getDefinition('normal_service');
|
||||||
$this->assertFalse($def->isAutowired());
|
$this->assertFalse($def->isAutowired());
|
||||||
|
Reference in New Issue
Block a user