[DI] Deprecate string keys in arguments

This commit is contained in:
Nicolas Grekas 2017-01-30 12:03:52 +01:00 committed by Kévin Dunglas
parent 2ce36a6074
commit 8a126c8537
No known key found for this signature in database
GPG Key ID: 4D04EBEF06AAF3A6
4 changed files with 17 additions and 11 deletions

View File

@ -213,12 +213,14 @@ class ChildDefinition extends Definition
*/
public function replaceArgument($index, $value)
{
if (!is_int($index)) {
if (is_int($index)) {
$this->arguments['index_'.$index] = $value;
} elseif (0 === strpos($index, '$')) {
$this->arguments[$index] = $value;
} else {
throw new InvalidArgumentException('$index must be an integer.');
}
$this->arguments['index_'.$index] = $value;
return $this;
}
}

View File

@ -145,11 +145,12 @@ class ResolveDefinitionTemplatesPass extends AbstractRecursivePass
continue;
}
if (0 !== strpos($k, 'index_')) {
if (0 === strpos($k, 'index_')) {
$index = (int) substr($k, strlen('index_'));
} elseif (0 !== strpos($k, '$')) {
throw new RuntimeException(sprintf('Invalid argument key "%s" found.', $k));
}
$index = (int) substr($k, strlen('index_'));
$def->replaceArgument($index, $v);
}

View File

@ -43,9 +43,14 @@ class ResolveNamedArgumentsPass extends AbstractRecursivePass
list($method, $arguments) = $call;
$method = $parameterBag->resolveValue($method);
$parameters = null;
$resolvedArguments = array();
foreach ($arguments as $key => $argument) {
if (is_int($key) || '' === $key || '$' !== $key[0]) {
if (!is_int($key)) {
@trigger_error(sprintf('Using key "%s" for defining arguments of method "%s" for service "%s" is deprecated since Symfony 3.3 and will throw an exception in 4.0. Use no keys or $named arguments instead.', $key, $method, $this->currentId), E_USER_DEPRECATED);
}
$resolvedArguments[] = $argument;
continue;
}
@ -53,8 +58,7 @@ class ResolveNamedArgumentsPass extends AbstractRecursivePass
foreach ($parameters as $j => $p) {
if ($key === '$'.$p->name) {
unset($arguments[$key]);
$arguments[$j] = $argument;
$resolvedArguments[$j] = $argument;
continue 2;
}
@ -63,9 +67,9 @@ class ResolveNamedArgumentsPass extends AbstractRecursivePass
throw new InvalidArgumentException(sprintf('Unable to resolve service "%s": method "%s::%s" has no argument named "%s". Check your service definition.', $this->currentId, $class, $method, $key));
}
if ($arguments !== $call[1]) {
ksort($arguments);
$calls[$i][1] = $arguments;
if ($resolvedArguments !== $call[1]) {
ksort($resolvedArguments);
$calls[$i][1] = $resolvedArguments;
}
}

View File

@ -20,7 +20,6 @@ use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Parser as YamlParser;
use Symfony\Component\Yaml\Tag\TaggedValue;