Throw on service:method factory notation in PHP-based DI configuration
This commit is contained in:
parent
4fadbcdc58
commit
49fc6778ee
@ -11,6 +11,8 @@
|
|||||||
|
|
||||||
namespace Symfony\Component\DependencyInjection\Loader\Configurator\Traits;
|
namespace Symfony\Component\DependencyInjection\Loader\Configurator\Traits;
|
||||||
|
|
||||||
|
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
|
||||||
|
|
||||||
trait FactoryTrait
|
trait FactoryTrait
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
@ -22,6 +24,12 @@ trait FactoryTrait
|
|||||||
*/
|
*/
|
||||||
final public function factory($factory)
|
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));
|
$this->definition->setFactory(static::processValue($factory, true));
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
|
@ -0,0 +1,9 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
|
||||||
|
|
||||||
|
return function (ContainerConfigurator $c) {
|
||||||
|
$c->services()
|
||||||
|
->set('service', \stdClass::class)
|
||||||
|
->factory('factory:method');
|
||||||
|
};
|
@ -89,4 +89,17 @@ class PhpFileLoaderTest extends TestCase
|
|||||||
$loader->load($fixtures.'/config/services_autoconfigure_with_parent.php');
|
$loader->load($fixtures.'/config/services_autoconfigure_with_parent.php');
|
||||||
$container->compile();
|
$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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user