|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace AlecRabbit\Spinner\Container; |
|
6
|
|
|
|
|
7
|
|
|
use AlecRabbit\Spinner\Container\Contract\IServiceSpawner; |
|
8
|
|
|
use AlecRabbit\Spinner\Container\Exception\ClassDoesNotExistException; |
|
9
|
|
|
use AlecRabbit\Spinner\Container\Exception\SpawnFailedException; |
|
10
|
|
|
use AlecRabbit\Spinner\Container\Exception\UnableToCreateInstanceException; |
|
11
|
|
|
use AlecRabbit\Spinner\Container\Exception\UnableToExtractTypeException; |
|
12
|
|
|
use Psr\Container\ContainerExceptionInterface; |
|
13
|
|
|
use Psr\Container\ContainerInterface; |
|
14
|
|
|
use Psr\Container\NotFoundExceptionInterface; |
|
15
|
|
|
use ReflectionClass; |
|
16
|
|
|
use ReflectionException; |
|
17
|
|
|
use ReflectionIntersectionType; |
|
|
|
|
|
|
18
|
|
|
use ReflectionNamedType; |
|
19
|
|
|
use ReflectionUnionType; |
|
20
|
|
|
use Throwable; |
|
21
|
|
|
|
|
22
|
|
|
final class ServiceSpawner implements IServiceSpawner |
|
23
|
|
|
{ |
|
24
|
|
|
public function __construct( |
|
25
|
|
|
protected ContainerInterface $container, |
|
26
|
|
|
) { |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function spawn(string|callable|object $definition): object |
|
30
|
|
|
{ |
|
31
|
|
|
try { |
|
32
|
|
|
return match (true) { |
|
33
|
|
|
is_callable($definition) => $this->spawnByCallable($definition), |
|
|
|
|
|
|
34
|
|
|
is_string($definition) => $this->spawnByClassConstructor($definition), |
|
|
|
|
|
|
35
|
|
|
default => $definition, // return object as is |
|
36
|
|
|
}; |
|
37
|
|
|
} catch (Throwable $e) { |
|
38
|
|
|
throw new SpawnFailedException( |
|
39
|
|
|
sprintf( |
|
40
|
|
|
'Could not spawn object with callable.%s', |
|
41
|
|
|
sprintf( |
|
42
|
|
|
' [%s]: "%s".', |
|
43
|
|
|
get_debug_type($e), |
|
44
|
|
|
$e->getMessage(), |
|
45
|
|
|
), |
|
46
|
|
|
), |
|
47
|
|
|
previous: $e, |
|
48
|
|
|
); |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
private function spawnByCallable(callable $definition): object |
|
53
|
|
|
{ |
|
54
|
|
|
/** @psalm-suppress MixedReturnStatement */ |
|
55
|
|
|
return $definition($this->container); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param class-string $definition |
|
|
|
|
|
|
60
|
|
|
* |
|
61
|
|
|
* @throws ContainerExceptionInterface |
|
62
|
|
|
* @throws NotFoundExceptionInterface |
|
63
|
|
|
* @throws ReflectionException |
|
64
|
|
|
*/ |
|
65
|
|
|
private function spawnByClassConstructor(string $definition): object |
|
66
|
|
|
{ |
|
67
|
|
|
return match (true) { |
|
68
|
|
|
class_exists($definition) => $this->createInstanceByReflection($definition), |
|
69
|
|
|
default => throw new ClassDoesNotExistException('Class does not exist: ' . $definition), |
|
70
|
|
|
}; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param class-string $class |
|
|
|
|
|
|
75
|
|
|
* |
|
76
|
|
|
* @throws ContainerExceptionInterface |
|
77
|
|
|
* @throws NotFoundExceptionInterface |
|
78
|
|
|
* @throws ReflectionException |
|
79
|
|
|
*/ |
|
80
|
|
|
private function createInstanceByReflection(string $class): object |
|
81
|
|
|
{ |
|
82
|
|
|
$reflection = new ReflectionClass($class); |
|
83
|
|
|
|
|
84
|
|
|
$constructorParameters = $reflection->getConstructor()?->getParameters(); |
|
85
|
|
|
if ($constructorParameters) { |
|
|
|
|
|
|
86
|
|
|
$parameters = []; |
|
87
|
|
|
foreach ($constructorParameters as $parameter) { |
|
88
|
|
|
$name = $parameter->getName(); |
|
89
|
|
|
$type = $parameter->getType(); |
|
90
|
|
|
if ($type === null) { |
|
91
|
|
|
throw new UnableToExtractTypeException('Unable to extract type for parameter name: $' . $name); |
|
92
|
|
|
} |
|
93
|
|
|
if ($this->needsService($type)) { |
|
94
|
|
|
$parameters[$name] = $this->getServiceFromContainer($type->getName()); |
|
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
return new $class(...$parameters); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
try { |
|
101
|
|
|
return new $class(); |
|
102
|
|
|
} catch (Throwable $e) { |
|
103
|
|
|
throw new UnableToCreateInstanceException('Unable to create instance of ' . $class, previous: $e); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @throws ContainerExceptionInterface |
|
109
|
|
|
*/ |
|
110
|
|
|
private function needsService(ReflectionIntersectionType|ReflectionNamedType|ReflectionUnionType $type): bool |
|
111
|
|
|
{ |
|
112
|
|
|
return match (true) { |
|
113
|
|
|
// assumes that all non-builtin types are services |
|
114
|
|
|
$type instanceof ReflectionNamedType => !$type->isBuiltin(), |
|
115
|
|
|
default => throw new UnableToExtractTypeException( |
|
116
|
|
|
sprintf( |
|
117
|
|
|
'Only %s is supported.', |
|
118
|
|
|
ReflectionNamedType::class, |
|
119
|
|
|
) |
|
120
|
|
|
), |
|
121
|
|
|
}; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @throws ContainerExceptionInterface |
|
126
|
|
|
* @throws NotFoundExceptionInterface |
|
127
|
|
|
*/ |
|
128
|
|
|
private function getServiceFromContainer(string $id): object |
|
129
|
|
|
{ |
|
130
|
|
|
return $this->container->get($id); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths