8000 Throw on service:method factory notation in PHP-based DI configuration · symfony/symfony@49fc677 · GitHub
[go: up one dir, main page]

Skip to content

Commit 49fc677

Browse files
committed
Throw on service:method factory notation in PHP-based DI configuration
1 parent 4fadbcd commit 49fc677

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

src/Symfony/Component/DependencyInjection/Loader/Configurator/Traits/FactoryTrait.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\DependencyInjection\Loader\Configurator\Traits;
1313

14+
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
15+
1416
trait FactoryTrait
1517
{
1618
/**
@@ -22,6 +24,12 @@ trait FactoryTrait
2224
*/
2325
final public function factory($factory)
2426
{
27+
if (is_string($factory) && 1 === substr_count($factory, ':')) {
28+
$factoryParts = explode(':', $factory);
29+
30+
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]));
31+
}
32+
2533
$this->definition->setFactory(static::processValue($factory, true));
2634

2735
return $this;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
4+
5+
return function (ContainerConfigurator $c) {
6+
$c->services()
7+
->set('service', \stdClass::class)
8+
->factory('factory:method');
9+
};

src/Symfony/Component/DependencyInjection/Tests/Loader/PhpFileLoaderTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,17 @@ public function testAutoConfigureAndChildDefinitionNotAllowed()
8989
$loader->load($fixtures.'/config/services_autoconfigure_with_parent.php');
9090
$container->compile();
9191
}
92+
93+
/**
94+
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
95+
* @expectedExceptionMessage Invalid factory "factory:method": the `service:method` notation is not available when using PHP-based DI configuration. Use "[ref('factory'), 'method']" instead.
96+
*/
97+
public function testFactoryShortNotationNotAllowed()
98+
{
99+
$fixtures = realpath(__DIR__.'/../Fixtures');
100+
$container = new ContainerBuilder();
101+
$loader = new PhpFileLoader($container, new FileLocator());
102+
$loader->load($fixtures.'/config/factory_short_notation.php');
103+
$container->compile();
104+
}
92105
}

0 commit comments

Comments
 (0)
0