8000 [DI] Resolve iterator argument as plain array if required by ro0NL · Pull Request #22204 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DI] Resolve iterator argument as plain array if required #22204

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

Closed
wants to merge 1 commit into from
Closed
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 8000 load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
*/
abstract class AbstractRecursivePass implements CompilerPassInterface
{
/**
* @var ContainerBuilder
*/
protected $container;
protected $currentId;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public function __construct()
new CheckDefinitionValidityPass(),
new RegisterServiceSubscribersPass(),
new ResolveNamedArgumentsPass(),
new ResolveArrayIterablesPass(),
new AutowirePass(),
new ResolveReferencesToAliasesPass(),
new ResolveInvalidReferencesPass(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\LazyProxy\ProxyHelper;

/**
* Resolves iterator arguments needed to be an array by definition.
*
* @author Roland Franssen <franssen.rolandd@gmail.com>
*/
class ResolveArrayIterablesPass extends AbstractRecursivePass
{
/**
* {@inheritdoc}
*/
protected function processValue($value, $isRoot = false)
{
if (!$value instanceof Definition || !$reflectionClass = $this->container->getReflectionClass($value->getClass())) {
return parent::processValue($value, $isRoot);
}

$calls = $value->getMethodCalls();
$calls[] = array('__construct', $value->getArguments());

foreach ($calls as $i => $call) {
list($method, $arguments) = $call;

if (!$reflectionClass->hasMethod($method)) {
continue;
}

$reflectionMethod = $reflectionClass->getMethod($method);
$reflectionParams = $reflectionMethod->getParameters();

foreach ($arguments as $key => $argument) {
if (!$argument instanceof IteratorArgument || !isset($reflectionParams[$key])) {
continue;
}

if ('array' === ProxyHelper::getTypeHint($reflectionMethod, $reflectionParams[$key])) {
$calls[$i][1][$key] = $argument->getValues();
}
}
}

$value->setArguments(array_pop($calls)[1]);
$value->setMethodCalls($calls);

return $value;
}
}
0