From 99083df15038fb8562228545ece99f281c3672fa Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Tue, 10 Jan 2017 08:04:52 +0100 Subject: [PATCH] [DI] Deprecate case insentivity of service identifiers --- UPGRADE-3.3.md | 4 + .../Component/DependencyInjection/Alias.php | 7 +- .../DependencyInjection/CHANGELOG.md | 4 +- .../Compiler/FactoryReturnTypePass.php | 11 +-- .../Compiler/ResolveClassPass.php | 4 +- .../DependencyInjection/Container.php | 47 +++++++++-- .../DependencyInjection/ContainerBuilder.php | 77 ++++++++----------- .../DependencyInjection/Dumper/PhpDumper.php | 22 ++++++ .../DependencyInjection/Reference.php | 7 +- .../Tests/Compiler/AutowirePassTest.php | 4 +- .../Tests/ContainerTest.php | 36 ++++++++- .../Tests/CrossCheckTest.php | 5 ++ .../Tests/Dumper/PhpDumperTest.php | 9 +++ .../Tests/Fixtures/containers/container33.php | 18 +++++ .../Tests/Fixtures/php/services33.php | 66 ++++++++++++++++ .../Tests/ReferenceTest.php | 6 -- 16 files changed, 254 insertions(+), 73 deletions(-) create mode 100644 src/Symfony/Component/DependencyInjection/Tests/Fixtures/containers/container33.php create mode 100644 src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services33.php diff --git a/UPGRADE-3.3.md b/UPGRADE-3.3.md index adef6db1cf5ed..8e319e8a3114d 100644 --- a/UPGRADE-3.3.md +++ b/UPGRADE-3.3.md @@ -10,6 +10,10 @@ ClassLoader DependencyInjection ------------------- + * The `Reference` and `Alias` classes do not make service identifiers lowercase anymore. + + * Case insensitivity of service identifiers is deprecated and will be removed in 4.0. + * Using the `PhpDumper` with an uncompiled `ContainerBuilder` is deprecated and will not be supported anymore in 4.0. diff --git a/src/Symfony/Component/DependencyInjection/Alias.php b/src/Symfony/Component/DependencyInjection/Alias.php index eaf7f00ccd446..8eba71973a087 100644 --- a/src/Symfony/Component/DependencyInjection/Alias.php +++ b/src/Symfony/Component/DependencyInjection/Alias.php @@ -22,7 +22,12 @@ class Alias */ public function __construct($id, $public = true) { - $this->id = strtolower($id); + if (!is_string($id)) { + $type = is_object($id) ? get_class($id) : gettype($id); + $id = (string) $id; + @trigger_error(sprintf('Non-string identifiers are deprecated since Symfony 3.3 and won\'t be supported in 4.0 for Alias to "%s" ("%s" given.) Cast it to string beforehand.', $id, $type), E_USER_DEPRECATED); + } + $this->id = $id; $this->public = $public; } diff --git a/src/Symfony/Component/DependencyInjection/CHANGELOG.md b/src/Symfony/Component/DependencyInjection/CHANGELOG.md index b329162636cc2..58dcbf7920854 100644 --- a/src/Symfony/Component/DependencyInjection/CHANGELOG.md +++ b/src/Symfony/Component/DependencyInjection/CHANGELOG.md @@ -4,10 +4,12 @@ CHANGELOG 3.3.0 ----- + * deprecated case insensitivity of service identifiers * added "iterator" argument type for lazy iteration over a set of values and services * added "closure-proxy" argument type for turning services' methods into lazy callables * added file-wide configurable defaults for service attributes "public", "tags", - "autowire" and a new "inherit-tags" + "autowire" and "inherit-tags" + * added "inherit-tags" service attribute to control tags' inheritance from parent context * made the "class" attribute optional, using the "id" as fallback * using the `PhpDumper` with an uncompiled `ContainerBuilder` is deprecated and will not be supported anymore in 4.0 diff --git a/src/Symfony/Component/DependencyInjection/Compiler/FactoryReturnTypePass.php b/src/Symfony/Component/DependencyInjection/Compiler/FactoryReturnTypePass.php index e772036152726..351ba365766a3 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/FactoryReturnTypePass.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/FactoryReturnTypePass.php @@ -51,12 +51,13 @@ public function process(ContainerBuilder $container) private function updateDefinition(ContainerBuilder $container, $id, Definition $definition, array $resolveClassPassChanges, array $previous = array()) { // circular reference - if (isset($previous[$id])) { + $lcId = strtolower($id); + if (isset($previous[$lcId])) { return; } $factory = $definition->getFactory(); - if (null === $factory || (!isset($resolveClassPassChanges[$id]) && null !== $definition->getClass())) { + if (null === $factory || (!isset($resolveClassPassChanges[$lcId]) && null !== $definition->getClass())) { return; } @@ -69,9 +70,9 @@ private function updateDefinition(ContainerBuilder $container, $id, Definition $ } } else { if ($factory[0] instanceof Reference) { - $previous[$id] = true; + $previous[$lcId] = true; $factoryDefinition = $container->findDefinition((string) $factory[0]); - $this->updateDefinition($container, strtolower($factory[0]), $factoryDefinition, $resolveClassPassChanges, $previous); + $this->updateDefinition($container, $factory[0], $factoryDefinition, $resolveClassPassChanges, $previous); $class = $factoryDefinition->getClass(); } else { $class = $factory[0]; @@ -96,7 +97,7 @@ private function updateDefinition(ContainerBuilder $container, $id, Definition $ } } - if (null !== $returnType && (!isset($resolveClassPassChanges[$id]) || $returnType !== $resolveClassPassChanges[$id])) { + if (null !== $returnType && (!isset($resolveClassPassChanges[$lcId]) || $returnType !== $resolveClassPassChanges[$lcId])) { @trigger_error(sprintf('Relying on its factory\'s return-type to define the class of service "%s" is deprecated since Symfony 3.3 and won\'t work in 4.0. Set the "class" attribute to "%s" on the service definition instead.', $id, $returnType), E_USER_DEPRECATED); } $definition->setClass($returnType); diff --git a/src/Symfony/Component/DependencyInjection/Compiler/ResolveClassPass.php b/src/Symfony/Component/DependencyInjection/Compiler/ResolveClassPass.php index a33fc2d76ddd8..22f194c798808 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/ResolveClassPass.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/ResolveClassPass.php @@ -31,8 +31,8 @@ public function process(ContainerBuilder $container) continue; } if (preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+(?:\\\\[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+)*+$/', $id)) { - $this->changes[$id] = $container->getCaseSensitiveId($id); - $definition->setClass($this->changes[$id]); + $this->changes[strtolower($id)] = $id; + $definition->setClass($id); } } } diff --git a/src/Symfony/Component/DependencyInjection/Container.php b/src/Symfony/Component/DependencyInjection/Container.php index da84d18acffa3..cd144ead493d2 100644 --- a/src/Symfony/Component/DependencyInjection/Container.php +++ b/src/Symfony/Component/DependencyInjection/Container.php @@ -70,6 +70,11 @@ class Container implements ResettableContainerInterface protected $aliases = array(); protected $loading = array(); + /** + * @internal + */ + protected $normalizedIds = array(); + private $underscoreMap = array('_' => '', '.' => '_', '\\' => '_'); private $envCache = array(); @@ -164,7 +169,7 @@ public function setParameter($name, $value) */ public function set($id, $service) { - $id = strtolower($id); + $id = $this->normalizeId($id); if ('service_container' === $id) { throw new InvalidArgumentException('You cannot set service "service_container".'); @@ -215,8 +220,8 @@ public function has($id) return true; } - if (--$i && $id !== $lcId = strtolower($id)) { - $id = $lcId; + if (--$i && $id !== $normalizedId = $this->normalizeId($id)) { + $id = $normalizedId; continue; } @@ -254,7 +259,7 @@ public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE // Attempt to retrieve the service by checking first aliases then // available services. Service IDs are case insensitive, however since // this method can be called thousands of times during a request, avoid - // calling strtolower() unless necessary. + // calling $this->normalizeId($id) unless necessary. for ($i = 2;;) { if ('service_container' === $id) { return $this; @@ -273,8 +278,8 @@ public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE if (isset($this->methodMap[$id])) { $method = $this->methodMap[$id]; - } elseif (--$i && $id !== $lcId = strtolower($id)) { - $id = $lcId; + } elseif (--$i && $id !== $normalizedId = $this->normalizeId($id)) { + $id = $normalizedId; continue; } elseif (!$this->methodMap && !$this instanceof ContainerBuilder && __CLASS__ !== static::class && method_exists($this, $method = 'get'.strtr($id, $this->underscoreMap).'Service')) { // We only check the convention-based factory in a compiled container (i.e. a child class other than a ContainerBuilder, @@ -329,7 +334,7 @@ public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE */ public function initialized($id) { - $id = strtolower($id); + $id = $this->normalizeId($id); if ('service_container' === $id) { return false; @@ -426,6 +431,34 @@ protected function getEnv($name) return $this->envCache[$name] = $this->getParameter("env($name)"); } + /** + * Returns the case sensitive id used at registration time. + * + * @param string $id + * + * @return string + * + * @internal + */ + public function normalizeId($id) + { + if (!is_string($id)) { + $type = is_object($id) ? get_class($id) : gettype($id); + $id = (string) $id; + @trigger_error(sprintf('Non-string service identifiers are deprecated since Symfony 3.3 and won\'t be supported in 4.0 for service "%s" ("%s" given.) Cast it to string beforehand.', $id, $type), E_USER_DEPRECATED); + } + if (isset($this->normalizedIds[$normalizedId = strtolower($id)])) { + $normalizedId = $this->normalizedIds[$normalizedId]; + if ($id !== $normalizedId) { + @trigger_error(sprintf('Service identifiers will be made case sensitive in Symfony 4.0. Using "%s" instead of "%s" is deprecated since version 3.3.', $id, $normalizedId), E_USER_DEPRECATED); + } + } else { + $normalizedId = $this->normalizedIds[$normalizedId] = $id; + } + + return $normalizedId; + } + private function __clone() { } diff --git a/src/Symfony/Component/DependencyInjection/ContainerBuilder.php b/src/Symfony/Component/DependencyInjection/ContainerBuilder.php index 044d72cb5426d..ee342f13a6a7c 100644 --- a/src/Symfony/Component/DependencyInjection/ContainerBuilder.php +++ b/src/Symfony/Component/DependencyInjection/ContainerBuilder.php @@ -103,11 +103,6 @@ class ContainerBuilder extends Container implements TaggedContainerInterface */ private $envCounters = array(); - /** - * @var array a map of case less to case sensitive ids - */ - private $caseSensitiveIds = array(); - /** * Sets the track resources flag. * @@ -372,8 +367,7 @@ public function getCompiler() */ public function set($id, $service) { - $caseSensitiveId = $id; - $id = strtolower($caseSensitiveId); + $id = $this->normalizeId($id); if ($this->isFrozen() && (isset($this->definitions[$id]) && !$this->definitions[$id]->isSynthetic())) { // setting a synthetic service on a frozen container is alright @@ -381,9 +375,6 @@ public function set($id, $service) } unset($this->definitions[$id], $this->aliasDefinitions[$id]); - if ($id !== $caseSensitiveId) { - $this->caseSensitiveIds[$id] = $caseSensitiveId; - } parent::set($id, $service); } @@ -395,7 +386,7 @@ public function set($id, $service) */ public function removeDefinition($id) { - unset($this->definitions[strtolower($id)]); + unset($this->definitions[$this->normalizeId($id)]); } /** @@ -407,7 +398,7 @@ public function removeDefinition($id) */ public function has($id) { - $id = strtolower($id); + $id = $this->normalizeId($id); return isset($this->definitions[$id]) || isset($this->aliasDefinitions[$id]) || parent::has($id); } @@ -429,7 +420,7 @@ public function has($id) */ public function get($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE) { - $id = strtolower($id); + $id = $this->normalizeId($id); if ($service = parent::get($id, ContainerInterface::NULL_ON_INVALID_REFERENCE)) { return $service; @@ -637,11 +628,10 @@ public function setAliases(array $aliases) */ public function setAlias($alias, $id) { - $caseSensitiveAlias = $alias; - $alias = strtolower($caseSensitiveAlias); + $alias = $this->normalizeId($alias); if (is_string($id)) { - $id = new Alias($id); + $id = new Alias($this->normalizeId($id)); } elseif (!$id instanceof Alias) { throw new InvalidArgumentException('$id must be a string, or an Alias object.'); } @@ -651,9 +641,6 @@ public function setAlias($alias, $id) } unset($this->definitions[$alias]); - if ($alias !== $caseSensitiveAlias) { - $this->caseSensitiveIds[$alias] = $caseSensitiveAlias; - } $this->aliasDefinitions[$alias] = $id; } @@ -665,7 +652,7 @@ public function setAlias($alias, $id) */ public function removeAlias($alias) { - unset($this->aliasDefinitions[strtolower($alias)]); + unset($this->aliasDefinitions[$this->normalizeId($alias)]); } /** @@ -677,7 +664,7 @@ public function removeAlias($alias) */ public function hasAlias($id) { - return isset($this->aliasDefinitions[strtolower($id)]); + return isset($this->aliasDefinitions[$this->normalizeId($id)]); } /** @@ -701,7 +688,7 @@ public function getAliases() */ public function getAlias($id) { - $id = strtolower($id); + $id = $this->normalizeId($id); if (!isset($this->aliasDefinitions[$id])) { throw new InvalidArgumentException(sprintf('The service alias "%s" does not exist.', $id)); @@ -791,13 +778,9 @@ public function setDefinition($id, Definition $definition) throw new BadMethodCallException('Adding definition to a frozen container is not allowed'); } - $caseSensitiveId = $id; - $id = strtolower($caseSensitiveId); + $id = $this->normalizeId($id); unset($this->aliasDefinitions[$id]); - if ($id !== $caseSensitiveId) { - $this->caseSensitiveIds[$id] = $caseSensitiveId; - } return $this->definitions[$id] = $definition; } @@ -811,7 +794,7 @@ public function setDefinition($id, Definition $definition) */ public function hasDefinition($id) { - return isset($this->definitions[strtolower($id)]); + return isset($this->definitions[$this->normalizeId($id)]); } /** @@ -825,7 +808,7 @@ public function hasDefinition($id) */ public function getDefinition($id) { - $id = strtolower($id); + $id = $this->normalizeId($id); if (!isset($this->definitions[$id])) { throw new ServiceNotFoundException($id); @@ -847,7 +830,7 @@ public function getDefinition($id) */ public function findDefinition($id) { - $id = strtolower($id); + $id = $this->normalizeId($id); while (isset($this->aliasDefinitions[$id])) { $id = (string) $this->aliasDefinitions[$id]; @@ -856,22 +839,6 @@ public function findDefinition($id) return $this->getDefinition($id); } - /** - * Returns the case sensitive id used at registration time. - * - * @param string $id - * - * @return string - * - * @internal - */ - public function getCaseSensitiveId($id) - { - $id = strtolower($id); - - return isset($this->caseSensitiveIds[$id]) ? $this->caseSensitiveIds[$id] : $id; - } - /** * Creates a service for a service definition. * @@ -1188,6 +1155,22 @@ public function getEnvCounters() return $this->envCounters; } + /** + * @internal + */ + public function getNormalizedIds() + { + $normalizedIds = array(); + + foreach ($this->normalizedIds as $k => $v) { + if ($v !== (string) $k) { + $normalizedIds[$k] = $v; + } + } + + return $normalizedIds; + } + /** * Returns the Service Conditionals. * @@ -1247,7 +1230,7 @@ private function callMethod($service, $call) private function shareService(Definition $definition, $service, $id) { if (null !== $id && $definition->isShared()) { - $this->services[strtolower($id)] = $service; + $this->services[$this->normalizeId($id)] = $service; } } diff --git a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php index dd97b2a68b412..a4d6698d9e288 100644 --- a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php +++ b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php @@ -835,6 +835,7 @@ public function __construct() EOF; + $code .= $this->addNormalizedIds(); $code .= $this->addMethodMap(); $code .= $this->addPrivateServices(); $code .= $this->addAliases(); @@ -870,6 +871,7 @@ public function __construct() } $code .= "\n \$this->services = array();\n"; + $code .= $this->addNormalizedIds(); $code .= $this->addMethodMap(); $code .= $this->addAliases(); @@ -921,6 +923,26 @@ public function isFrozen() EOF; } + /** + * Adds the normalizedIds property definition. + * + * @return string + */ + private function addNormalizedIds() + { + if (!$normalizedIds = $this->container->getNormalizedIds()) { + return ''; + } + + $code = " \$this->normalizedIds = array(\n"; + ksort($normalizedIds); + foreach ($normalizedIds as $id => $normalizedId) { + $code .= ' '.$this->export($id).' => '.$this->export($normalizedId).",\n"; + } + + return $code." );\n"; + } + /** * Adds the methodMap property definition. * diff --git a/src/Symfony/Component/DependencyInjection/Reference.php b/src/Symfony/Component/DependencyInjection/Reference.php index 3c8b314f5619f..ea3467b18b628 100644 --- a/src/Symfony/Component/DependencyInjection/Reference.php +++ b/src/Symfony/Component/DependencyInjection/Reference.php @@ -29,7 +29,12 @@ class Reference */ public function __construct($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE) { - $this->id = strtolower($id); + if (!is_string($id)) { + $type = is_object($id) ? get_class($id) : gettype($id); + $id = (string) $id; + @trigger_error(sprintf('Non-string identifiers are deprecated since Symfony 3.3 and won\'t be supported in 4.0 for Reference to "%s" ("%s" given.) Cast it to string beforehand.', $id, $type), E_USER_DEPRECATED); + } + $this->id = $id; $this->invalidBehavior = $invalidBehavior; } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php b/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php index 640bbbae91a16..bb7bed43becd9 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php @@ -211,13 +211,13 @@ public function testCreateDefinition() $pass->process($container); $this->assertCount(1, $container->getDefinition('coop_tilleuls')->getArguments()); - $this->assertEquals('autowired.symfony\component\dependencyinjection\tests\compiler\dunglas', $container->getDefinition('coop_tilleuls')->getArgument(0)); + $this->assertEquals('autowired.Symfony\Component\DependencyInjection\Tests\Compiler\Dunglas', $container->getDefinition('coop_tilleuls')->getArgument(0)); $dunglasDefinition = $container->getDefinition('autowired.Symfony\Component\DependencyInjection\Tests\Compiler\Dunglas'); $this->assertEquals(__NAMESPACE__.'\Dunglas', $dunglasDefinition->getClass()); $this->assertFalse($dunglasDefinition->isPublic()); $this->assertCount(1, $dunglasDefinition->getArguments()); - $this->assertEquals('autowired.symfony\component\dependencyinjection\tests\compiler\lille', $dunglasDefinition->getArgument(0)); + $this->assertEquals('autowired.Symfony\Component\DependencyInjection\Tests\Compiler\Lille', $dunglasDefinition->getArgument(0)); $lilleDefinition = $container->getDefinition('autowired.Symfony\Component\DependencyInjection\Tests\Compiler\Lille'); $this->assertEquals(__NAMESPACE__.'\Lille', $lilleDefinition->getClass()); diff --git a/src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php b/src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php index 20c171c798d90..8620fc4d7b124 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php @@ -11,6 +11,7 @@ namespace Symfony\Component\DependencyInjection\Tests; +use Symfony\Component\DependencyInjection\Alias; use Symfony\Component\DependencyInjection\Container; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; @@ -167,7 +168,6 @@ public function testGet() $sc = new ProjectServiceContainer(); $sc->set('foo', $foo = new \stdClass()); $this->assertSame($foo, $sc->get('foo'), '->get() returns the service for the given id'); - $this->assertSame($foo, $sc->get('Foo'), '->get() returns the service for the given id, and converts id to lowercase'); $this->assertSame($sc->__bar, $sc->get('bar'), '->get() returns the service for the given id'); $this->assertSame($sc->__foo_bar, $sc->get('foo_bar'), '->get() returns the service if a get*Method() is defined'); $this->assertSame($sc->__foo_baz, $sc->get('foo.baz'), '->get() returns the service if a get*Method() is defined'); @@ -184,6 +184,40 @@ public function testGet() $this->assertNull($sc->get('', ContainerInterface::NULL_ON_INVALID_REFERENCE), '->get() returns null if the service is empty'); } + /** + * @group legacy + * @expectedDeprecation Service identifiers will be made case sensitive in Symfony 4.0. Using "Foo" instead of "foo" is deprecated since version 3.3. + */ + public function testGetInsensitivity() + { + $sc = new ProjectServiceContainer(); + $sc->set('foo', $foo = new \stdClass()); + $this->assertSame($foo, $sc->get('Foo'), '->get() returns the service for the given id, and converts id to lowercase'); + } + + /** + * @group legacy + * @expectedDeprecation Non-string service identifiers are deprecated since Symfony 3.3 and won't be supported in 4.0 for service "foo" ("Symfony\Component\DependencyInjection\Alias" given.) Cast it to string beforehand. + * @expectedDeprecation Service identifiers will be made case sensitive in Symfony 4.0. Using "Foo" instead of "foo" is deprecated since version 3.3. + */ + public function testNonStringNormalizeId() + { + $sc = new ProjectServiceContainer(); + $this->assertSame('foo', $sc->normalizeId(new Alias('foo'))); + $this->assertSame('foo', $sc->normalizeId('Foo')); + } + + /** + * @group legacy + * @expectedDeprecation Service identifiers will be made case sensitive in Symfony 4.0. Using "foo" instead of "Foo" is deprecated since version 3.3. + */ + public function testNormalizeIdKeepsCase() + { + $sc = new ProjectServiceContainer(); + $sc->normalizeId('Foo', true); + $this->assertSame('Foo', $sc->normalizeId('foo')); + } + /** * @group legacy * @expectedDeprecation Generating a dumped container without populating the method map is deprecated since 3.2 and will be unsupported in 4.0. Update your dumper to generate the method map. diff --git a/src/Symfony/Component/DependencyInjection/Tests/CrossCheckTest.php b/src/Symfony/Component/DependencyInjection/Tests/CrossCheckTest.php index 423c5db2ec96a..0af07967a8da6 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/CrossCheckTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/CrossCheckTest.php @@ -55,6 +55,11 @@ public function testCrossCheck($fixture, $type) $this->assertEquals($container2->getDefinitions(), $container1->getDefinitions(), 'loading a dump from a previously loaded container returns the same container'); $this->assertEquals($container2->getParameterBag()->all(), $container1->getParameterBag()->all(), '->getParameterBag() returns the same value for both containers'); + $r = new \ReflectionProperty(ContainerBuilder::class, 'normalizedIds'); + $r->setAccessible(true); + $r->setValue($container2, array()); + $r->setValue($container1, array()); + $this->assertEquals(serialize($container2), serialize($container1), 'loading a dump from a previously loaded container returns the same container'); $services1 = array(); diff --git a/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php b/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php index 7e58f6e4c5dc8..6efc2e9a3900c 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php @@ -484,4 +484,13 @@ public function testClosureProxyPhp71() $res = $container->getResources(); $this->assertSame(realpath(self::$fixturesPath.'/containers/container32.php'), array_pop($res)->getResource()); } + + public function testNormalizedId() + { + $container = include self::$fixturesPath.'/containers/container33.php'; + $container->compile(); + $dumper = new PhpDumper($container); + + $this->assertStringEqualsFile(self::$fixturesPath.'/php/services33.php', $dumper->dump()); + } } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/containers/container33.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/containers/container33.php new file mode 100644 index 0000000000000..482558cb0d352 --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/containers/container33.php @@ -0,0 +1,18 @@ +register(Foo::class); + +return $container; diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services33.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services33.php new file mode 100644 index 0000000000000..c34e153b94912 --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services33.php @@ -0,0 +1,66 @@ +services = array(); + $this->normalizedIds = array( + 'symfony\\component\\dependencyinjection\\tests\\fixtures\\container33\\foo' => 'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\Container33\\Foo', + ); + $this->methodMap = array( + 'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\Container33\\Foo' => 'getSymfony_Component_DependencyInjection_Tests_Fixtures_Container33_FooService', + ); + + $this->aliases = array(); + } + + /** + * {@inheritdoc} + */ + public function compile() + { + throw new LogicException('You cannot compile a dumped frozen container.'); + } + + /** + * {@inheritdoc} + */ + public function isFrozen() + { + return true; + } + + /** + * Gets the 'Symfony\Component\DependencyInjection\Tests\Fixtures\Container33\Foo' service. + * + * This service is shared. + * This method always returns the same instance of the service. + * + * @return \Symfony\Component\DependencyInjection\Tests\Fixtures\Container33\Foo A Symfony\Component\DependencyInjection\Tests\Fixtures\Container33\Foo instance + */ + protected function getSymfony_Component_DependencyInjection_Tests_Fixtures_Container33_FooService() + { + return $this->services['Symfony\Component\DependencyInjection\Tests\Fixtures\Container33\Foo'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\Container33\Foo(); + } +} diff --git a/src/Symfony/Component/DependencyInjection/Tests/ReferenceTest.php b/src/Symfony/Component/DependencyInjection/Tests/ReferenceTest.php index 6800267c96b10..a5446f61e2a3b 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/ReferenceTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/ReferenceTest.php @@ -20,10 +20,4 @@ public function testConstructor() $ref = new Reference('foo'); $this->assertEquals('foo', (string) $ref, '__construct() sets the id of the reference, which is used for the __toString() method'); } - - public function testCaseInsensitive() - { - $ref = new Reference('FooBar'); - $this->assertEquals('foobar', (string) $ref, 'the id is lowercased as the container is case insensitive'); - } }