From 49fc6778ee6cb37bd83265d555ca28c59b4ca792 Mon Sep 17 00:00:00 2001 From: Valentin Date: Fri, 17 Nov 2017 12:50:06 +0300 Subject: [PATCH] Throw on service:method factory notation in PHP-based DI configuration --- .../Loader/Configurator/Traits/FactoryTrait.php | 8 ++++++++ .../Fixtures/config/factory_short_notation.php | 9 +++++++++ .../Tests/Loader/PhpFileLoaderTest.php | 13 +++++++++++++ 3 files changed, 30 insertions(+) create mode 100644 src/Symfony/Component/DependencyInjection/Tests/Fixtures/config/factory_short_notation.php diff --git a/src/Symfony/Component/DependencyInjection/Loader/Configurator/Traits/FactoryTrait.php b/src/Symfony/Component/DependencyInjection/Loader/Configurator/Traits/FactoryTrait.php index 71c15906cc..12e8859ca1 100644 --- a/src/Symfony/Component/DependencyInjection/Loader/Configurator/Traits/FactoryTrait.php +++ b/src/Symfony/Component/DependencyInjection/Loader/Configurator/Traits/FactoryTrait.php @@ -11,6 +11,8 @@ namespace Symfony\Component\DependencyInjection\Loader\Configurator\Traits; +use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; + trait FactoryTrait { /** @@ -22,6 +24,12 @@ trait FactoryTrait */ final public function factory($factory) { + if (is_string($factory) && 1 === substr_count($factory, ':')) { + $factoryParts = explode(':', $factory); + + throw new InvalidArgumentException(sprintf('Invalid factory "%s": the `service:method` notation is not available when using PHP-based DI configuration. Use "[ref(\'%s\'), \'%s\']" instead.', $factory, $factoryParts[0], $factoryParts[1])); + } + $this->definition->setFactory(static::processValue($factory, true)); return $this; diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/config/factory_short_notation.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/config/factory_short_notation.php new file mode 100644 index 0000000000..5b37793ea2 --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/config/factory_short_notation.php @@ -0,0 +1,9 @@ +services() + ->set('service', \stdClass::class) + ->factory('factory:method'); +}; diff --git a/src/Symfony/Component/DependencyInjection/Tests/Loader/PhpFileLoaderTest.php b/src/Symfony/Component/DependencyInjection/Tests/Loader/PhpFileLoaderTest.php index a8f10d9ef0..8c84dc7e80 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Loader/PhpFileLoaderTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Loader/PhpFileLoaderTest.php @@ -89,4 +89,17 @@ class PhpFileLoaderTest extends TestCase $loader->load($fixtures.'/config/services_autoconfigure_with_parent.php'); $container->compile(); } + + /** + * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException + * @expectedExceptionMessage Invalid factory "factory:method": the `service:method` notation is not available when using PHP-based DI configuration. Use "[ref('factory'), 'method']" instead. + */ + public function testFactoryShortNotationNotAllowed() + { + $fixtures = realpath(__DIR__.'/../Fixtures'); + $container = new ContainerBuilder(); + $loader = new PhpFileLoader($container, new FileLocator()); + $loader->load($fixtures.'/config/factory_short_notation.php'); + $container->compile(); + } }