1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Ray\Di; |
6
|
|
|
|
7
|
|
|
use Ray\Compiler\CompileVisitor; |
|
|
|
|
8
|
|
|
|
9
|
|
|
use function assert; |
10
|
|
|
use function sprintf; |
11
|
|
|
|
12
|
|
|
final class DependencyProvider implements DependencyInterface |
13
|
|
|
{ |
14
|
|
|
/** @var string */ |
15
|
|
|
public $context; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Provider dependency |
19
|
|
|
* |
20
|
|
|
* @var Dependency |
21
|
|
|
*/ |
22
|
|
|
private $dependency; |
23
|
|
|
|
24
|
|
|
/** @var bool */ |
25
|
|
|
private $isSingleton = false; |
26
|
|
|
|
27
|
|
|
/** @var ?mixed */ |
28
|
|
|
private $instance; |
29
|
|
|
|
30
|
|
|
public function __construct(Dependency $dependency, string $context) |
31
|
|
|
{ |
32
|
|
|
$this->dependency = $dependency; |
33
|
|
|
$this->context = $context; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return list<string> |
|
|
|
|
38
|
|
|
*/ |
39
|
|
|
public function __sleep() |
40
|
|
|
{ |
41
|
|
|
return ['context', 'dependency', 'isSingleton']; |
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function __toString(): string |
45
|
|
|
{ |
46
|
|
|
return sprintf( |
47
|
|
|
'(provider) %s', |
48
|
|
|
(string) $this->dependency |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
|
|
public function register(array &$container, Bind $bind): void |
56
|
|
|
{ |
57
|
|
|
$container[(string) $bind] = $bind->getBound(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
|
|
public function inject(Container $container) |
64
|
|
|
{ |
65
|
|
|
if ($this->isSingleton && $this->instance) { |
66
|
|
|
return $this->instance; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$provider = $this->dependency->inject($container); |
70
|
|
|
assert($provider instanceof ProviderInterface); |
71
|
|
|
if ($provider instanceof SetContextInterface) { |
72
|
|
|
$this->setContext($provider); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$this->instance = $provider->get(); |
76
|
|
|
|
77
|
|
|
return $this->instance; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
*/ |
83
|
|
|
public function setScope($scope): void |
84
|
|
|
{ |
85
|
|
|
if ($scope !== Scope::SINGLETON) { |
86
|
|
|
return; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$this->isSingleton = true; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function setContext(SetContextInterface $provider): void |
93
|
|
|
{ |
94
|
|
|
$provider->setContext($this->context); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function isSingleton(): bool |
98
|
|
|
{ |
99
|
|
|
return $this->isSingleton; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function accept(VisitorInterface $visitor): string |
103
|
|
|
{ |
104
|
|
|
assert($visitor instanceof CompileVisitor); |
105
|
|
|
|
106
|
|
|
return $visitor->visitProvider( |
|
|
|
|
107
|
|
|
$this->dependency, |
108
|
|
|
$this->context, |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
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