10000 [DI] fix definition and usage of AbstractArgument by nicolas-grekas · Pull Request #36545 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DI] fix definition and usage of AbstractArgument #36545

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
Apr 24, 2020
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 @@ -16,29 +16,26 @@
*/
final class AbstractArgument
{
private $serviceId;
private $argKey;
private $text;
private $context;

public function __construct(string $serviceId, string $argKey, string $text = '')
public function __construct(string $text = '')
{
$this->serviceId = $serviceId;
$this->argKey = $argKey;
$this->text = $text;
$this->text = trim($text, '. ');
}

public function getServiceId(): string
public function setContext(string $context): void
{
return $this->serviceId;
$this->context = $context.' is abstract'.('' === $this->text ? '' : ': ');
}

public function getArgumentKey(): string
public function getText(): string
{
return $this->argKey;
return $this->text;
}

public function getText(): string
public function getTextWithContext(): string
{
return $this->text;
return $this->context.$this->text.'.';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Argument\AbstractArgument;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\LazyProxy\ProxyHelper;
Expand All @@ -28,6 +29,10 @@ class ResolveNamedArgumentsPass extends AbstractRecursivePass
*/
protected function processValue($value, bool $isRoot = false)
{
if ($value instanceof AbstractArgument && $value->getText().'.' === $value->getTextWithContext()) {
$value->setContext(sprintf('A value found in service "%s"', $this->currentId));
}

if (!$value instanceof Definition) {
return parent::processValue($value, $isRoot);
}
Expand All @@ -41,6 +46,10 @@ protected function processValue($value, bool $isRoot = false)
$resolvedArguments = [];

foreach ($arguments as $key => $argument) {
if ($argument instanceof AbstractArgument && $argument->getText().'.' === $argument->getTextWithContext()) {
$argument->setContext(sprintf('Argument '.(\is_int($key) ? 1 + $key : '"%3$s"').' of '.('__construct' === $method ? 'service "%s"' : 'method call "%s::%s()"'), $this->currentId, $method, $key));
}

if (\is_int($key)) {
$resolvedArguments[$key] = $argument;
continue;
Expand Down Expand Up @@ -107,6 +116,12 @@ protected function processValue($value, bool $isRoot = false)
$value->setMethodCalls($calls);
}

foreach ($value->getProperties() as $key => $argument) {
if ($argument instanceof AbstractArgument && $argument->getText().'.' === $argument->getTextWithContext()) {
$argument->setContext(sprintf('Property "%s" of service "%s"', $key, $this->currentId));
}
}

return parent::processValue($value, $isRoot);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1219,7 +1219,7 @@ private function doResolveServices($value, array &$inlineServices = [], bool $is
} elseif ($value instanceof Expression) {
$value = $this->getExpressionLanguage()->evaluate($value 10000 , ['container' => $this]);
} elseif ($value instanceof AbstractArgument) {
throw new RuntimeException(sprintf('Argument "%s" of service "%s" is abstract%s, did you forget to define it?', $value->getArgumentKey(), $value->getServiceId(), $value->getText() ? ' ('.$value->getText().')' : ''));
throw new RuntimeException($value->getTextWithContext());
}

return $value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1787,7 +1787,7 @@ private function dumpValue($value, bool $interpolate = true): string
return $code;
}
} elseif ($value instanceof AbstractArgument) {
throw new RuntimeException(sprintf('Argument "%s" of service "%s" is abstract%s, did you forget to define it?', $value->getArgumentKey(), $value->getServiceId(), $value->getText() ? ' ('.$value->getText().')' : ''));
throw new RuntimeException($value->getTextWithContext());
} elseif (\is_object($value) || \is_resource($value)) {
throw new RuntimeException('Unable to dump a service container if a parameter is an object or a resource.');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,10 +320,6 @@ private function convertParameters(array $parameters, string $type, \DOMElement
$text = $this->document->createTextNode(self::phpToXml(base64_encode($value)));
$element->appendChild($text);
} elseif ($value instanceof AbstractArgument) {
$argKey = $value->getArgumentKey();
if (!is_numeric($argKey)) {
$element->setAttribute('key', $argKey);
}
$element->setAttribute('type', 'abstract');
$text = $this->document->createTextNode(self::phpToXml($value->getText()));
$element->appendChild($text);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -537,8 +537,7 @@ private function getArgumentsAsPhp(\DOMElement $node, string $name, string $file
$arguments[$key] = $value;
break;
case 'abstract':
$serviceId = $node->getAttribute('id');
$arguments[$key] = new AbstractArgument($serviceId, (string) $key, $arg->nodeValue);
$arguments[$key] = new AbstractArgument($arg->nodeValue);
break;
case 'string':
$arguments[$key] = $arg->nodeValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ private function parseDefinition(string $id, $service, string $file, array $defa
}

if (isset($service['arguments'])) {
$definition->setArguments($this->resolveServices($service['arguments'], $file, false, $id));
$definition->setArguments($this->resolveServices($service['arguments'], $file));
}

if (isset($service['properties'])) {
Expand Down Expand Up @@ -721,7 +721,7 @@ private function validate($content, string $file): ?array
*
* @return array|string|Reference|ArgumentInterface
*/
private function resolveServices($value, string $file, bool $isParameter = false, string $serviceId = '', string $argKey = '')
private function resolveServices($value, string $file, bool $isParameter = false)
{
if ($value instanceof TaggedValue) {
$argument = $value->getValue();
Expand Down Expand Up @@ -795,15 +795,15 @@ private function resolveServices($value, string $file, bool $isParameter = false
return new Reference($id);
}
if ('abstract' === $value->getTag()) {
return new AbstractArgument($serviceId, $argKey, $value->getValue());
return new AbstractArgument($value->getValue());
}

throw new InvalidArgumentException(sprintf('Unsupported tag "!%s".', $value->getTag()));
}

if (\is_array($value)) {
foreach ($value as $k => $v) {
$value[$k] = $this->resolveServices($v, $file, $isParameter, $serviceId, $k);
$value[$k] = $this->resolveServices($v, $file, $isParameter);
}
} elseif (\is_string($value) && 0 === strpos($value, '@=')) {
if (!class_exists(Expression::class)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ class AbstractArgumentTest extends TestCase
{
public function testAbstractArgumentGetters()
{
$argument = new AbstractArgument('foo', '$bar', 'should be defined by Pass');
$this->assertSame('foo', $argument->getServiceId());
$this->assertSame('$bar', $argument->getArgumentKey());
$argument = new AbstractArgument('should be defined by Pass');
$this->assertSame('should be defined by Pass', $argument->getText());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -552,11 +552,14 @@ public function testCreateServiceWithExpression()
public function testCreateServiceWithAbstractArgument()
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Argument "$baz" of service "foo" is abstract (should be defined by Pass), did you forget to define it?');
$this->expectExceptionMessage('Argument "$baz" of service "foo" is abstract: should be defined by Pass.');

$builder = new ContainerBuilder();
$builder->register('foo', FooWithAbstractArgument::class)
->addArgument(new AbstractArgument('foo', '$baz', 'should be defined by Pass'));
->setArgument('$baz', new AbstractArgument('should be defined by Pass'))
->setPublic(true);

$builder->compile();

$builder->get('foo');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1372,12 +1372,12 @@ public function testMultipleDeprecatedAliasesWorking()
public function testDumpServiceWithAbstractArgument()
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Argument "$baz" of service "Symfony\Component\DependencyInjection\Tests\Fixtures\FooWithAbstractArgument" is abstract (should be defined by Pass), did you forget to define it?');
$this->expectExceptionMessage('Argument "$baz" of service "Symfony\Component\DependencyInjection\Tests\Fixtures\FooWithAbstractArgument" is abstract: should be defined by Pass.');

$container = new ContainerBuilder();

$container->register(FooWithAbstractArgument::class, FooWithAbstractArgument::class)
->setArgument('$baz', new AbstractArgument(FooWithAbstractArgument::class, '$baz', 'should be defined by Pass'))
->setArgument('$baz', new AbstractArgument('should be defined by Pass'))
->setArgument('$bar', 'test')
->setPublic(true);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ public function testDumpServiceWithAbstractArgument()
{
$container = new ContainerBuilder();
$container->register(FooWithAbstractArgument::class, FooWithAbstractArgument::class)
->setArgument('$baz', new AbstractArgument(FooWithAbstractArgument::class, '$baz', 'should be defined by Pass'))
->setArgument('$baz', new AbstractArgument('should be defined by Pass'))
->setArgument('$bar', 'test');

$dumper = new XmlDumper($container);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public function testDumpServiceWithAbstractArgument()
{
$container = new ContainerBuilder();
$container->register(FooWithAbstractArgument::class, FooWithAbstractArgument::class)
->setArgument('$baz', new AbstractArgument(FooWithAbstractArgument::class, '$baz', 'should be defined by Pass'))
->setArgument('$baz', new AbstractArgument('should be defined by Pass'))
->setArgument('$bar', 'test');

$dumper = new YamlDumper($container);
Expand Down
0