Spaces:
Runtime error
Runtime error
File size: 4,623 Bytes
7a66774 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | <?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\Attribute\AutowireInline;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\VarExporter\ProxyHelper;
/**
* Inspects existing autowired services for {@see AutowireInline} attributes and registers the definitions for reuse.
*
* @author Ismail Özgün Turan <oezguen.turan@dadadev.com>
*/
class ResolveAutowireInlineAttributesPass extends AbstractRecursivePass
{
protected bool $skipScalars = true;
private int $counter;
protected function processValue(mixed $value, bool $isRoot = false): mixed
{
$value = parent::processValue($value, $isRoot);
if (!$value instanceof Definition || !$value->isAutowired() || !$value->getClass() || $value->hasTag('container.ignore_attributes')) {
return $value;
}
if ($isRoot) {
$this->counter = 0;
}
$isChildDefinition = $value instanceof ChildDefinition;
try {
$constructor = $this->getConstructor($value, false);
} catch (RuntimeException) {
return $value;
}
if ($constructor) {
$arguments = $this->registerAutowireInlineAttributes($constructor, $value->getArguments(), $isChildDefinition);
if ($arguments !== $value->getArguments()) {
$value->setArguments($arguments);
}
}
$methodCalls = $value->getMethodCalls();
foreach ($methodCalls as $i => $call) {
[$method, $arguments] = $call;
try {
$method = $this->getReflectionMethod($value, $method);
} catch (RuntimeException) {
continue;
}
$arguments = $this->registerAutowireInlineAttributes($method, $arguments, $isChildDefinition);
if ($arguments !== $call[1]) {
$methodCalls[$i][1] = $arguments;
}
}
if ($methodCalls !== $value->getMethodCalls()) {
$value->setMethodCalls($methodCalls);
}
return $value;
}
private function registerAutowireInlineAttributes(\ReflectionFunctionAbstract $method, array $arguments, bool $isChildDefinition): array
{
$parameters = $method->getParameters();
if ($method->isVariadic()) {
array_pop($parameters);
}
$paramResolverContainer = new ContainerBuilder($this->container->getParameterBag());
foreach ($parameters as $index => $parameter) {
if ($isChildDefinition) {
$index = 'index_'.$index;
}
if (\array_key_exists('$'.$parameter->name, $arguments) || (\array_key_exists($index, $arguments) && '' !== $arguments[$index])) {
$attribute = \array_key_exists('$'.$parameter->name, $arguments) ? $arguments['$'.$parameter->name] : $arguments[$index];
if (!$attribute instanceof AutowireInline) {
continue;
}
} elseif (!$attribute = $parameter->getAttributes(AutowireInline::class, \ReflectionAttribute::IS_INSTANCEOF)[0] ?? null) {
continue;
} else {
$attribute = $attribute->newInstance();
}
$type = ProxyHelper::exportType($parameter, true);
if (!$type && isset($arguments[$index])) {
continue;
}
$definition = $attribute->buildDefinition($attribute->value, $type, $parameter);
$paramResolverContainer->setDefinition('.autowire_inline', $definition);
(new ResolveParameterPlaceHoldersPass(false, false))->process($paramResolverContainer);
$id = '.autowire_inline.'.$this->currentId.'.'.++$this->counter;
$this->container->setDefinition($id, $definition);
$arguments[$isChildDefinition ? '$'.$parameter->name : $index] = new Reference($id);
if ($definition->isAutowired()) {
$this->processValue($definition);
}
}
return $arguments;
}
}
|