1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Spiral\Core\Internal\Proxy; |
6
|
|
|
|
7
|
|
|
use Psr\Container\ContainerInterface; |
8
|
|
|
use Spiral\Core\Container; |
9
|
|
|
use Spiral\Core\ContainerScope; |
10
|
|
|
use Spiral\Core\Exception\Container\ContainerException; |
11
|
|
|
use Spiral\Core\Exception\Container\RecursiveProxyException; |
12
|
|
|
use Spiral\Core\Internal\Introspector; |
13
|
|
|
use Spiral\Core\Internal\Proxy; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @internal |
17
|
|
|
*/ |
18
|
|
|
final class Resolver |
19
|
|
|
{ |
20
|
484 |
|
public static function resolve( |
21
|
|
|
string $alias, |
22
|
|
|
\Stringable|string|null $context = null, |
23
|
|
|
?ContainerInterface $c = null, |
24
|
|
|
): object { |
25
|
484 |
|
$c ??= ContainerScope::getContainer() ?? throw new ContainerException('Proxy is out of scope.'); |
26
|
|
|
|
27
|
|
|
try { |
28
|
|
|
/** @psalm-suppress TooManyArguments */ |
29
|
481 |
|
$result = $c->get($alias, $context) ?? throw new ContainerException( |
|
|
|
|
30
|
481 |
|
'Resolved `null` from the container.', |
31
|
481 |
|
); |
32
|
|
|
} catch (\Throwable $e) { |
33
|
|
|
$scope = self::getScope($c); |
34
|
|
|
throw new ContainerException( |
35
|
|
|
$scope === null |
36
|
|
|
? "Unable to resolve `{$alias}` in a Proxy." |
37
|
|
|
: \sprintf('Unable to resolve `%s` in a Proxy in `%s` scope.', $alias, \implode('.', $scope)), |
38
|
|
|
previous: $e, |
39
|
|
|
); |
40
|
|
|
} |
41
|
|
|
|
42
|
481 |
|
if (!Proxy::isProxy($result)) { |
43
|
478 |
|
return $result; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* If we got a Proxy again, that we should retry with the new context |
48
|
|
|
* to try to get the instance from the Proxy Fallback Factory. |
49
|
|
|
* If there is no the Proxy Fallback Factory, {@see RecursiveProxyException} will be thrown. |
50
|
|
|
*/ |
51
|
|
|
try { |
52
|
|
|
/** @psalm-suppress TooManyArguments */ |
53
|
25 |
|
$result = $c->get($alias, new RetryContext($context)); |
54
|
5 |
|
} catch (RecursiveProxyException $e) { |
55
|
1 |
|
throw new RecursiveProxyException($e->alias, $e->bindingScope, self::getScope($c)); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
// If Container returned a Proxy after the retry, then we have a recursion. |
59
|
21 |
|
return Proxy::isProxy($result) |
60
|
1 |
|
? throw new RecursiveProxyException($alias, null, self::getScope($c)) |
61
|
20 |
|
: $result; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return list<non-empty-string|null>|null |
|
|
|
|
66
|
|
|
*/ |
67
|
2 |
|
private static function getScope(ContainerInterface $c): ?array |
68
|
|
|
{ |
69
|
2 |
|
if (!$c instanceof Container) { |
70
|
|
|
if (!Proxy::isProxy($c)) { |
71
|
|
|
return null; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$c = null; |
75
|
|
|
} |
76
|
|
|
|
77
|
2 |
|
return \array_reverse(\array_map( |
78
|
2 |
|
static fn (?string $name): string => $name ?? 'null', |
79
|
2 |
|
Introspector::scopeNames($c), |
80
|
2 |
|
)); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.