Skip to content

[DependencyInjection] Prevent service:method factory notation in PHP config #24732

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

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

use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;

trait FactoryTrait
{
/**
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

return function (ContainerConfigurator $c) {
$c->services()
->set('service', \stdClass::class)
->factory('factory:method');
};
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,17 @@ public function testAutoConfigureAndChildDefinitionNotAllowed()
$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();
}
}