From 838d9ca6c01b0054e0a7dc8872dcb253905b6a48 Mon Sep 17 00:00:00 2001 From: Hugo Hamon Date: Mon, 27 Feb 2017 15:50:16 +0100 Subject: [PATCH 1/3] [DependencyInjection] add missing dumped private services list in a container frozen constructor. --- .../DependencyInjection/Dumper/PhpDumper.php | 1 + .../Tests/Dumper/PhpDumperTest.php | 13 +++ .../Fixtures/php/services_private_frozen.php | 97 +++++++++++++++++++ 3 files changed, 111 insertions(+) create mode 100644 src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_private_frozen.php diff --git a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php index f827ac013c..6c3631333a 100644 --- a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php +++ b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php @@ -855,6 +855,7 @@ EOF; $code .= "\n \$this->services = array();\n"; $code .= $this->addMethodMap(); + $code .= $this->addPrivateServices(); $code .= $this->addAliases(); $code .= <<<'EOF' diff --git a/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php b/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php index c05b37e2da..c87a12576d 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php @@ -392,4 +392,17 @@ class PhpDumperTest extends TestCase $dumper->setProxyDumper(new DummyProxyDumper()); $dumper->dump(); } + + public function testDumpContainerBuilderWithFrozenConstructorIncludingPrivateServices() + { + $container = new ContainerBuilder(); + $container->register('foo_service', 'stdClass')->setArguments(array(new Reference('baz_service'))); + $container->register('bar_service', 'stdClass')->setArguments(array(new Reference('baz_service'))); + $container->register('baz_service', 'stdClass')->setPublic(false); + $container->compile(); + + $dumper = new PhpDumper($container); + + $this->assertStringEqualsFile(self::$fixturesPath.'/php/services_private_frozen.php', $dumper->dump()); + } } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_private_frozen.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_private_frozen.php new file mode 100644 index 0000000000..f14141b8d1 --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_private_frozen.php @@ -0,0 +1,97 @@ +services = array(); + $this->methodMap = array( + 'bar_service' => 'getBarServiceService', + 'baz_service' => 'getBazServiceService', + 'foo_service' => 'getFooServiceService', + ); + $this->privates = array( + 'baz_service' => true, + ); + + $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 'bar_service' service. + * + * This service is shared. + * This method always returns the same instance of the service. + * + * @return \stdClass A stdClass instance + */ + protected function getBarServiceService() + { + return $this->services['bar_service'] = new \stdClass(${($_ = isset($this->services['baz_service']) ? $this->services['baz_service'] : $this->getBazServiceService()) && false ?: '_'}); + } + + /** + * Gets the 'foo_service' service. + * + * This service is shared. + * This method always returns the same instance of the service. + * + * @return \stdClass A stdClass instance + */ + protected function getFooServiceService() + { + return $this->services['foo_service'] = new \stdClass(${($_ = isset($this->services['baz_service']) ? $this->services['baz_service'] : $this->getBazServiceService()) && false ?: '_'}); + } + + /** + * Gets the 'baz_service' service. + * + * This service is shared. + * This method always returns the same instance of the service. + * + * This service is private. + * If you want to be able to request this service from the container directly, + * make it public, otherwise you might end up with broken code. + * + * @return \stdClass A stdClass instance + */ + protected function getBazServiceService() + { + return $this->services['baz_service'] = new \stdClass(); + } +} From 45f0b16b78766dd49fe064e8837d8b16733b7dd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20Ostroluck=C3=BD?= Date: Fri, 24 Feb 2017 23:07:14 +0100 Subject: [PATCH 2/3] [Serializer] Reduce nesting in YamlFileLoader --- .../Mapping/Loader/YamlFileLoader.php | 46 +++++++++---------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/src/Symfony/Component/Serializer/Mapping/Loader/YamlFileLoader.php b/src/Symfony/Component/Serializer/Mapping/Loader/YamlFileLoader.php index ebe2a6e4b4..66bed137a4 100644 --- a/src/Symfony/Component/Serializer/Mapping/Loader/YamlFileLoader.php +++ b/src/Symfony/Component/Serializer/Mapping/Loader/YamlFileLoader.php @@ -60,39 +60,39 @@ class YamlFileLoader extends FileLoader $this->classes = $classes; } - if (isset($this->classes[$classMetadata->getName()])) { - $yaml = $this->classes[$classMetadata->getName()]; + if (!isset($this->classes[$classMetadata->getName()])) { + return false; + } - if (isset($yaml['attributes']) && is_array($yaml['attributes'])) { - $attributesMetadata = $classMetadata->getAttributesMetadata(); + $yaml = $this->classes[$classMetadata->getName()]; - foreach ($yaml['attributes'] as $attribute => $data) { - if (isset($attributesMetadata[$attribute])) { - $attributeMetadata = $attributesMetadata[$attribute]; - } else { - $attributeMetadata = new AttributeMetadata($attribute); - $classMetadata->addAttributeMetadata($attributeMetadata); + if (isset($yaml['attributes']) && is_array($yaml['attributes'])) { + $attributesMetadata = $classMetadata->getAttributesMetadata(); + + foreach ($yaml['attributes'] as $attribute => $data) { + if (isset($attributesMetadata[$attribute])) { + $attributeMetadata = $attributesMetadata[$attribute]; + } else { + $attributeMetadata = new AttributeMetadata($attribute); + $classMetadata->addAttributeMetadata($attributeMetadata); + } + + if (isset($data['groups'])) { + if (!is_array($data['groups'])) { + throw new MappingException('The "groups" key must be an array of strings in "%s" for the attribute "%s" of the class "%s".', $this->file, $attribute, $classMetadata->getName()); } - if (isset($data['groups'])) { - if (!is_array($data['groups'])) { - throw new MappingException('The "groups" key must be an array of strings in "%s" for the attribute "%s" of the class "%s".', $this->file, $attribute, $classMetadata->getName()); + foreach ($data['groups'] as $group) { + if (!is_string($group)) { + throw new MappingException('Group names must be strings in "%s" for the attribute "%s" of the class "%s".', $this->file, $attribute, $classMetadata->getName()); } - foreach ($data['groups'] as $group) { - if (!is_string($group)) { - throw new MappingException('Group names must be strings in "%s" for the attribute "%s" of the class "%s".', $this->file, $attribute, $classMetadata->getName()); - } - - $attributeMetadata->addGroup($group); - } + $attributeMetadata->addGroup($group); } } } - - return true; } - return false; + return true; } } From e473aa8bc1154972312b51a0ba44f94d484737a2 Mon Sep 17 00:00:00 2001 From: Romain Neutron Date: Tue, 28 Feb 2017 13:44:06 +0100 Subject: [PATCH 3/3] [2.7] Fix issues reported by static analyse --- src/Symfony/Bridge/Twig/TwigEngine.php | 2 +- .../Bundle/WebProfilerBundle/Profiler/TemplateManager.php | 2 +- .../HttpKernel/EventListener/TestSessionListener.php | 1 + .../Component/Serializer/Mapping/Loader/YamlFileLoader.php | 4 ++-- 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Bridge/Twig/TwigEngine.php b/src/Symfony/Bridge/Twig/TwigEngine.php index 1ac9d0102e..760461b5be 100644 --- a/src/Symfony/Bridge/Twig/TwigEngine.php +++ b/src/Symfony/Bridge/Twig/TwigEngine.php @@ -112,7 +112,7 @@ class TwigEngine implements EngineInterface, StreamingEngineInterface * @param string|TemplateReferenceInterface|\Twig_Template $name A template name or an instance of * TemplateReferenceInterface or \Twig_Template * - * @return \Twig_TemplateInterface A \Twig_TemplateInterface instance + * @return \Twig_Template A \Twig_Template instance * * @throws \InvalidArgumentException if the template does not exist */ diff --git a/src/Symfony/Bundle/WebProfilerBundle/Profiler/TemplateManager.php b/src/Symfony/Bundle/WebProfilerBundle/Profiler/TemplateManager.php index 91938e9be5..ff5dce56f9 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/Profiler/TemplateManager.php +++ b/src/Symfony/Bundle/WebProfilerBundle/Profiler/TemplateManager.php @@ -67,7 +67,7 @@ class TemplateManager * * @param Profile $profile * - * @return Twig_Template[] + * @return \Twig_Template[] * * @deprecated not used anymore internally */ diff --git a/src/Symfony/Component/HttpKernel/EventListener/TestSessionListener.php b/src/Symfony/Component/HttpKernel/EventListener/TestSessionListener.php index 8fc8e57579..19a61f169b 100644 --- a/src/Symfony/Component/HttpKernel/EventListener/TestSessionListener.php +++ b/src/Symfony/Component/HttpKernel/EventListener/TestSessionListener.php @@ -12,6 +12,7 @@ namespace Symfony\Component\HttpKernel\EventListener; use Symfony\Component\HttpFoundation\Cookie; +use Symfony\Component\HttpFoundation\Session\SessionInterface; use Symfony\Component\HttpKernel\KernelEvents; use Symfony\Component\HttpKernel\Event\FilterResponseEvent; use Symfony\Component\HttpKernel\Event\GetResponseEvent; diff --git a/src/Symfony/Component/Serializer/Mapping/Loader/YamlFileLoader.php b/src/Symfony/Component/Serializer/Mapping/Loader/YamlFileLoader.php index 66bed137a4..be2de7b6f1 100644 --- a/src/Symfony/Component/Serializer/Mapping/Loader/YamlFileLoader.php +++ b/src/Symfony/Component/Serializer/Mapping/Loader/YamlFileLoader.php @@ -79,12 +79,12 @@ class YamlFileLoader extends FileLoader if (isset($data['groups'])) { if (!is_array($data['groups'])) { - throw new MappingException('The "groups" key must be an array of strings in "%s" for the attribute "%s" of the class "%s".', $this->file, $attribute, $classMetadata->getName()); + throw new MappingException(sprintf('The "groups" key must be an array of strings in "%s" for the attribute "%s" of the class "%s".', $this->file, $attribute, $classMetadata->getName())); } foreach ($data['groups'] as $group) { if (!is_string($group)) { - throw new MappingException('Group names must be strings in "%s" for the attribute "%s" of the class "%s".', $this->file, $attribute, $classMetadata->getName()); + throw new MappingException(sprintf('Group names must be strings in "%s" for the attribute "%s" of the class "%s".', $this->file, $attribute, $classMetadata->getName())); } $attributeMetadata->addGroup($group);