1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | /* |
||
6 | * This file is part of Biurad opensource projects. |
||
7 | * |
||
8 | * PHP version 7.2 and above required |
||
9 | * |
||
10 | * @author Divine Niiquaye Ibok <[email protected]> |
||
11 | * @copyright 2019 Biurad Group (https://biurad.com/) |
||
12 | * @license https://opensource.org/licenses/BSD-3-Clause License |
||
13 | * |
||
14 | * For the full copyright and license information, please view the LICENSE |
||
15 | * file that was distributed with this source code. |
||
16 | */ |
||
17 | |||
18 | namespace Biurad\DependencyInjection; |
||
19 | |||
20 | use Nette\DI\Resolver as NetteResolver; |
||
21 | use Nette\Utils\Reflection; |
||
22 | use ReflectionFunctionAbstract; |
||
23 | |||
24 | /** |
||
25 | * Services resolver |
||
26 | * |
||
27 | * @internal |
||
28 | */ |
||
29 | class Resolver extends NetteResolver |
||
30 | { |
||
31 | /** |
||
32 | * Add missing arguments using autowiring. |
||
33 | * |
||
34 | * @param ReflectionFunctionAbstract $method |
||
35 | * @param mixed[] $arguments |
||
36 | * @param (callable(string $type, bool $single): object|object[]|null) $getter |
||
0 ignored issues
–
show
Documentation
Bug
introduced
by
![]() |
|||
37 | * |
||
38 | * @return mixed[] |
||
39 | */ |
||
40 | public static function autowireArguments( |
||
41 | ReflectionFunctionAbstract $method, |
||
42 | array $arguments, |
||
43 | callable $getter |
||
44 | ): array { |
||
45 | $optCount = 0; |
||
46 | $num = -1; |
||
47 | $res = []; |
||
48 | |||
49 | foreach ($method->getParameters() as $num => $param) { |
||
50 | $paramName = $param->name; |
||
51 | if (!$param->isVariadic() && array_key_exists($paramName, $arguments)) { |
||
52 | $res[$num] = $arguments[$paramName]; |
||
53 | unset($arguments[$paramName], $arguments[$num]); |
||
54 | } elseif (array_key_exists($num, $arguments)) { |
||
55 | $res[$num] = $arguments[$num]; |
||
56 | unset($arguments[$num]); |
||
57 | } else { |
||
58 | $res[$num] = self::autowireArgument($param, $getter); |
||
59 | } |
||
60 | |||
61 | $optCount = $param->isOptional() && $res[$num] === ($param->isDefaultValueAvailable() ? Reflection::getParameterDefaultValue($param) : null) |
||
62 | ? $optCount + 1 |
||
63 | : 0; |
||
64 | } |
||
65 | |||
66 | // extra parameters |
||
67 | while (array_key_exists(++$num, $arguments)) { |
||
68 | $res[$num] = $arguments[$num]; |
||
69 | unset($arguments[$num]); |
||
70 | $optCount = 0; |
||
71 | } |
||
72 | |||
73 | if ($optCount) { |
||
74 | $res = array_slice($res, 0, -$optCount); |
||
75 | } |
||
76 | |||
77 | return $res; |
||
78 | } |
||
79 | } |
||
80 |