diff --git a/src/Symfony/Component/DependencyInjection/Loader/FileLoader.php b/src/Symfony/Component/DependencyInjection/Loader/FileLoader.php index 40cf8f9ee4cae..dd8a39614468b 100644 --- a/src/Symfony/Component/DependencyInjection/Loader/FileLoader.php +++ b/src/Symfony/Component/DependencyInjection/Loader/FileLoader.php @@ -11,6 +11,7 @@ namespace Symfony\Component\DependencyInjection\Loader; +use Symfony\Component\DependencyInjection\Alias; use Symfony\Component\DependencyInjection\ChildDefinition; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Definition; @@ -58,12 +59,14 @@ public function registerClasses(Definition $prototype, $namespace, $resource, $e throw new InvalidArgumentException(sprintf('Namespace is not a valid PSR-4 prefix: %s.', $namespace)); } + $public = $prototype->isPublic(); $classes = $this->findClasses($namespace, $resource, $exclude); // prepare for deep cloning $prototype = serialize($prototype); foreach ($classes as $class) { - $this->setDefinition($class, unserialize($prototype)); + $this->setDefinition("$class.prototype", unserialize($prototype)->setClass($class)->setPublic(false)); + $this->container->setAlias($class, new Alias("$class.prototype", $public)); } } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Compiler/IntegrationTest.php b/src/Symfony/Component/DependencyInjection/Tests/Compiler/IntegrationTest.php index 15c827d8270df..98e121b65744d 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Compiler/IntegrationTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Compiler/IntegrationTest.php @@ -204,6 +204,12 @@ public function getYamlCompileTests() 'child_service', 'child_service_expected', ); + + yield array( + 'psr4_prototype_parent_child', + 'child_service', + 'child_service_expected', + ); } } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/integration/psr4_prototype_parent_child/expected.yml b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/integration/psr4_prototype_parent_child/expected.yml new file mode 100644 index 0000000000000..f4ee3b02c6a55 --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/integration/psr4_prototype_parent_child/expected.yml @@ -0,0 +1,7 @@ +services: + child_service_expected: + class: Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\Sub\Bar + autoconfigure: false + autowire: true + public: true + arguments: ['dummy_arg'] diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/integration/psr4_prototype_parent_child/main.yml b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/integration/psr4_prototype_parent_child/main.yml new file mode 100644 index 0000000000000..b34cf3f6679c3 --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/integration/psr4_prototype_parent_child/main.yml @@ -0,0 +1,16 @@ +services: + _defaults: + autoconfigure: true + autowire: true + public: false + + Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\Sub\: + resource: '../../../Prototype/Sub' + arguments: ['dummy_arg'] + + child_service: + class: Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\Sub\Bar + parent: Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\Sub\Bar.prototype + public: true + autowire: true + autoconfigure: false diff --git a/src/Symfony/Component/DependencyInjection/Tests/Loader/FileLoaderTest.php b/src/Symfony/Component/DependencyInjection/Tests/Loader/FileLoaderTest.php index 6544145c4e0e0..5e0b0fd1dfa20 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Loader/FileLoaderTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Loader/FileLoaderTest.php @@ -88,9 +88,10 @@ public function testRegisterClasses() $loader->registerClasses(new Definition(), 'Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\Sub\\', 'Prototype/%sub_dir%/*'); $this->assertEquals( - array('service_container', Bar::class), + array('service_container', Bar::class.'.prototype'), array_keys($container->getDefinitions()) ); + $this->assertSame(Bar::class.'.prototype', (string) $container->getAlias(Bar::class)); } public function testRegisterClassesWithExclude() diff --git a/src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php b/src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php index 9ffe5793e4764..4c8e6e56a147c 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php @@ -624,8 +624,9 @@ public function testPrototype() $ids = array_keys($container->getDefinitions()); sort($ids); - $this->assertSame(array(Prototype\Foo::class, Prototype\Sub\Bar::class, 'service_container'), $ids); - + $this->assertSame(array(Prototype\Foo::class.'.prototype', Prototype\Sub\Bar::class.'.prototype', 'service_container'), $ids); + $this->assertSame(Prototype\Foo::class.'.prototype', (string) $container->getAlias(Prototype\Foo::class)); + $this->assertSame(Prototype\Sub\Bar::class.'.prototype', (string) $container->getAlias(Prototype\Sub\Bar::class)); $resources = $container->getResources(); $fixturesDir = dirname(__DIR__).DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR; diff --git a/src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php b/src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php index b8ac2fc0e9296..856f342577c3c 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php @@ -376,7 +376,9 @@ public function testPrototype() $ids = array_keys($container->getDefinitions()); sort($ids); - $this->assertSame(array(Prototype\Foo::class, Prototype\Sub\Bar::class, 'service_container'), $ids); + $this->assertSame(array(Prototype\Foo::class.'.prototype', Prototype\Sub\Bar::class.'.prototype', 'service_container'), $ids); + $this->assertSame(Prototype\Foo::class.'.prototype', (string) $container->getAlias(Prototype\Foo::class)); + $this->assertSame(Prototype\Sub\Bar::class.'.prototype', (string) $container->getAlias(Prototype\Sub\Bar::class)); $resources = $container->getResources();