|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace MichaelRubel\EnhancedContainer\Core; |
|
6
|
|
|
|
|
7
|
|
|
use MichaelRubel\EnhancedContainer\Bind; |
|
8
|
|
|
use MichaelRubel\EnhancedContainer\Traits\HelpsProxies; |
|
9
|
|
|
|
|
10
|
|
|
class BindingBuilder implements Bind |
|
11
|
|
|
{ |
|
12
|
|
|
use HelpsProxies; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var \Closure|string|array |
|
16
|
|
|
*/ |
|
17
|
|
|
private \Closure|string|array $contextualImplementation; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* BindingBuilder constructor. |
|
21
|
|
|
* |
|
22
|
|
|
* @param object|string $abstract |
|
23
|
|
|
*/ |
|
24
|
|
|
public function __construct( |
|
25
|
|
|
private object | string $abstract |
|
26
|
|
|
) { |
|
27
|
|
|
$this->abstract = $this->convertToNamespace($this->abstract); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Method binding. |
|
32
|
|
|
* |
|
33
|
|
|
* @param string|null $method |
|
34
|
|
|
* @param \Closure|null $override |
|
35
|
|
|
* |
|
36
|
|
|
* @return $this|null |
|
37
|
|
|
*/ |
|
38
|
|
|
public function method(string $method = null, \Closure $override = null): self|null |
|
|
|
|
|
|
39
|
|
|
{ |
|
40
|
|
|
// try to resolve implementation |
|
41
|
|
|
// for actual abstract type |
|
42
|
|
|
$this->resolve(); |
|
43
|
|
|
|
|
44
|
|
|
if (is_null($method) || is_null($override)) { |
|
45
|
|
|
return $this; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
return $this->{$method}($override); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Basic "bind". |
|
53
|
|
|
* |
|
54
|
|
|
* @param \Closure|string|null $concrete |
|
55
|
|
|
* @param bool $shared |
|
56
|
|
|
* |
|
57
|
|
|
* @return $this |
|
58
|
|
|
*/ |
|
59
|
|
|
public function to(\Closure|string $concrete = null, bool $shared = false): self |
|
60
|
|
|
{ |
|
61
|
|
|
app()->bind($this->abstract, $concrete, $shared); |
|
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
return $this; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Basic "bind", binds itself. |
|
68
|
|
|
* |
|
69
|
|
|
* @return void |
|
70
|
|
|
*/ |
|
71
|
|
|
public function itself(): void |
|
72
|
|
|
{ |
|
73
|
|
|
app()->bind($this->abstract); |
|
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Singleton. |
|
78
|
|
|
* |
|
79
|
|
|
* @param \Closure|string|null $concrete |
|
80
|
|
|
* |
|
81
|
|
|
* @return void |
|
82
|
|
|
*/ |
|
83
|
|
|
public function singleton(\Closure|string $concrete = null): void |
|
84
|
|
|
{ |
|
85
|
|
|
app()->singleton($this->abstract, $concrete); |
|
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Scoped instance. |
|
90
|
|
|
* |
|
91
|
|
|
* @param \Closure|string|null $concrete |
|
92
|
|
|
* |
|
93
|
|
|
* @return void |
|
94
|
|
|
*/ |
|
95
|
|
|
public function scoped(\Closure|string $concrete = null): void |
|
96
|
|
|
{ |
|
97
|
|
|
app()->scoped($this->abstract, $concrete); |
|
|
|
|
|
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Enables contextual binding. |
|
102
|
|
|
* |
|
103
|
|
|
* @return $this |
|
104
|
|
|
*/ |
|
105
|
|
|
public function contextual(\Closure|string|array $implementation): self |
|
106
|
|
|
{ |
|
107
|
|
|
$this->contextualImplementation = $implementation; |
|
108
|
|
|
|
|
109
|
|
|
return $this; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Contextual binding. |
|
114
|
|
|
* |
|
115
|
|
|
* @param array|string $concrete |
|
116
|
|
|
* |
|
117
|
|
|
* @return void |
|
118
|
|
|
*/ |
|
119
|
|
|
public function for(array|string $concrete): void |
|
120
|
|
|
{ |
|
121
|
|
|
app()->when($concrete) |
|
122
|
|
|
->needs($this->abstract) |
|
|
|
|
|
|
123
|
|
|
->give($this->contextualImplementation); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Extend the abstract type. |
|
128
|
|
|
* |
|
129
|
|
|
* @param \Closure $closure |
|
130
|
|
|
* |
|
131
|
|
|
* @return BindingBuilder |
|
132
|
|
|
*/ |
|
133
|
|
|
public function extend(\Closure $closure): self |
|
134
|
|
|
{ |
|
135
|
|
|
app()->extend($this->abstract, $closure); |
|
|
|
|
|
|
136
|
|
|
|
|
137
|
|
|
return $this; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Try to resolve an abstract. |
|
142
|
|
|
* |
|
143
|
|
|
* @return mixed |
|
144
|
|
|
*/ |
|
145
|
|
|
public function resolve(): mixed |
|
146
|
|
|
{ |
|
147
|
|
|
return rescue( |
|
148
|
|
|
fn () => $this->abstract = $this->convertToNamespace( |
|
149
|
|
|
resolve($this->abstract) |
|
|
|
|
|
|
150
|
|
|
), |
|
151
|
|
|
null, |
|
152
|
|
|
false |
|
153
|
|
|
); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Bind the method to the container. |
|
158
|
|
|
* |
|
159
|
|
|
* @param string $method |
|
160
|
|
|
* @param array $parameters |
|
161
|
|
|
* |
|
162
|
|
|
* @return void |
|
163
|
|
|
*/ |
|
164
|
|
|
public function __call(string $method, array $parameters): void |
|
165
|
|
|
{ |
|
166
|
|
|
app()->bindMethod([ |
|
167
|
|
|
$this->abstract, |
|
168
|
|
|
$method, |
|
169
|
|
|
], current($parameters)); |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|
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