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
|
460 |
|
public static function resolve( |
21
|
|
|
string $alias, |
22
|
|
|
\Stringable|string|null $context = null, |
23
|
|
|
?ContainerInterface $c = null, |
24
|
|
|
): object { |
25
|
460 |
|
$c ??= ContainerScope::getContainer() ?? throw new ContainerException('Proxy is out of scope.'); |
26
|
|
|
|
27
|
|
|
try { |
28
|
|
|
/** @psalm-suppress TooManyArguments */ |
29
|
457 |
|
$result = $c->get($alias, $context) ?? throw new ContainerException( |
|
|
|
|
30
|
457 |
|
'Resolved `null` from the container.', |
31
|
457 |
|
); |
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
|
|
|
: "Unable to resolve `{$alias}` in a Proxy in `{$scope}` scope.", |
38
|
|
|
previous: $e, |
39
|
|
|
); |
40
|
|
|
} |
41
|
|
|
|
42
|
457 |
|
if (Proxy::isProxy($result)) { |
43
|
1 |
|
$scope = self::getScope($c); |
44
|
1 |
|
throw new RecursiveProxyException( |
45
|
1 |
|
$scope === null |
46
|
|
|
? "Recursive proxy detected for `{$alias}`." |
47
|
1 |
|
: "Recursive proxy detected for `{$alias}` in `{$scope}` scope.", |
48
|
1 |
|
); |
49
|
|
|
} |
50
|
|
|
|
51
|
456 |
|
return $result; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return non-empty-string|null |
|
|
|
|
56
|
|
|
*/ |
57
|
1 |
|
private static function getScope(ContainerInterface $c): ?string |
58
|
|
|
{ |
59
|
1 |
|
if (!$c instanceof Container) { |
60
|
|
|
if (!Proxy::isProxy($c)) { |
61
|
|
|
return null; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$c = null; |
65
|
|
|
} |
66
|
|
|
|
67
|
1 |
|
return \implode('.', \array_reverse(\array_map( |
68
|
1 |
|
static fn (?string $name): string => $name ?? 'null', |
69
|
1 |
|
Introspector::scopeNames($c), |
70
|
1 |
|
))); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
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.