1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Contains the Concord class. |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) 2016 Attila Fulop |
6
|
|
|
* @author Attila Fulop |
7
|
|
|
* @license MIT |
8
|
|
|
* @since 2016-08-14 |
9
|
|
|
* |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
namespace Konekt\Concord; |
14
|
|
|
|
15
|
|
|
use Illuminate\Contracts\Foundation\Application; |
16
|
|
|
use Illuminate\Foundation\AliasLoader; |
17
|
|
|
use Illuminate\Support\Collection; |
18
|
|
|
use InvalidArgumentException; |
19
|
|
|
use Konekt\Concord\Contracts\Concord as ConcordContract; |
20
|
|
|
use Konekt\Concord\Contracts\Convention; |
21
|
|
|
use Konekt\Concord\Events\EnumWasRegistered; |
22
|
|
|
use Konekt\Concord\Events\HelperWasRegistered; |
23
|
|
|
use Konekt\Concord\Events\ModelWasRegistered; |
24
|
|
|
use Konekt\Concord\Events\RequestWasRegistered; |
25
|
|
|
use Route; |
26
|
|
|
|
27
|
|
|
class Concord implements ConcordContract |
28
|
|
|
{ |
29
|
|
|
const VERSION = '0.9.10'; |
30
|
|
|
|
31
|
|
|
/** @var Collection */ |
32
|
|
|
protected $modules; |
33
|
|
|
|
34
|
|
|
/** @var array */ |
35
|
|
|
protected $models = []; |
36
|
|
|
|
37
|
|
|
/** @var array */ |
38
|
|
|
protected $enums = []; |
39
|
|
|
|
40
|
|
|
/** @var array */ |
41
|
|
|
protected $requests = []; |
42
|
|
|
|
43
|
|
|
/** @var array */ |
44
|
|
|
protected $implicitModules = []; |
45
|
|
|
|
46
|
|
|
/** @var Loader */ |
|
|
|
|
47
|
|
|
protected $loader; |
48
|
|
|
|
49
|
|
|
/** @var Application */ |
50
|
|
|
protected $app; |
51
|
|
|
|
52
|
|
|
/** @var array */ |
53
|
|
|
protected $shorts = []; |
54
|
|
|
|
55
|
|
|
/** @var Convention */ |
56
|
|
|
private $convention; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Concord class constructor |
60
|
|
|
* |
61
|
|
|
* @param Application $app |
62
|
|
|
* @param Convention $convention |
63
|
|
|
*/ |
64
|
|
|
public function __construct(Application $app, Convention $convention) |
65
|
|
|
{ |
66
|
|
|
$this->modules = Collection::make(); |
67
|
|
|
$this->app = $app; |
68
|
|
|
$this->convention = $convention; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @inheritdoc |
73
|
|
|
*/ |
74
|
|
|
public function registerModule($moduleClass, $config = []) |
75
|
|
|
{ |
76
|
|
|
$this->mergeModuleConfig(concord_module_id($moduleClass), $config); |
77
|
|
|
|
78
|
|
|
$module = $this->app->register($moduleClass); |
79
|
|
|
|
80
|
|
|
$this->modules->put($module->getId(), $module); |
|
|
|
|
81
|
|
|
$implicit = isset($config['implicit']) ? $config['implicit'] : false; |
82
|
|
|
|
83
|
|
|
if ($implicit) { |
84
|
|
|
$this->implicitModules[get_class($module)] = true; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @inheritdoc |
90
|
|
|
*/ |
91
|
|
|
public function registerHelper($name, $class) |
92
|
|
|
{ |
93
|
|
|
$this->app->singleton('concord.helpers.' . $name, $class); |
94
|
|
|
|
95
|
|
|
event(new HelperWasRegistered($name, $class)); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @inheritdoc |
100
|
|
|
*/ |
101
|
|
|
public function getModules($includeImplicits = false) : Collection |
102
|
|
|
{ |
103
|
|
|
if ($includeImplicits) { |
104
|
|
|
return $this->modules; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$implicitModules = $this->implicitModules; |
108
|
|
|
|
109
|
|
|
return $this->modules->reject(function ($module) use ($implicitModules) { |
110
|
|
|
return array_key_exists(get_class($module), $implicitModules); |
111
|
|
|
}); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @inheritdoc |
116
|
|
|
*/ |
117
|
|
|
public function registerAlias($alias, $concrete) |
118
|
|
|
{ |
119
|
|
|
AliasLoader::getInstance()->alias($alias, $concrete); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @inheritDoc |
124
|
|
|
*/ |
125
|
|
|
public function registerModel(string $abstract, string $concrete, $registerRouteModel = true) |
126
|
|
|
{ |
127
|
|
|
if (!is_subclass_of($concrete, $abstract, true)) { |
128
|
|
|
throw new InvalidArgumentException("Class {$concrete} must extend or implement {$abstract}. "); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$this->models[$abstract] = $concrete; |
132
|
|
|
$this->app->alias($concrete, $abstract); |
133
|
|
|
$this->registerShort($abstract, 'model'); |
134
|
|
|
|
135
|
|
|
// Route models can't resolve models by interface |
136
|
|
|
// so we're registering them explicitly |
137
|
|
|
if ($registerRouteModel) { |
138
|
|
|
Route::model(shorten($abstract), $concrete); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$this->resetProxy($this->getConvention()->proxyForModelContract($abstract)); |
142
|
|
|
event(new ModelWasRegistered($abstract, $concrete, $registerRouteModel)); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @inheritDoc |
147
|
|
|
*/ |
148
|
|
|
public function model(string $abstract) |
149
|
|
|
{ |
150
|
|
|
return array_get($this->models, $abstract); |
|
|
|
|
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @inheritdoc |
155
|
|
|
*/ |
156
|
|
|
public function getModelBindings() : Collection |
157
|
|
|
{ |
158
|
|
|
return collect($this->models); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @inheritdoc |
163
|
|
|
*/ |
164
|
|
|
public function getConvention(): Convention |
165
|
|
|
{ |
166
|
|
|
return $this->convention; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @inheritDoc |
171
|
|
|
*/ |
172
|
|
|
public function registerEnum(string $abstract, string $concrete) |
173
|
|
|
{ |
174
|
|
|
if (!is_subclass_of($concrete, $abstract, true)) { |
175
|
|
|
throw new InvalidArgumentException("Class {$concrete} must extend or implement {$abstract}. "); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
$this->enums[$abstract] = $concrete; |
179
|
|
|
$this->app->alias($concrete, $abstract); |
180
|
|
|
$this->registerShort($abstract, 'enum'); |
181
|
|
|
|
182
|
|
|
$this->resetProxy($this->getConvention()->proxyForEnumContract($abstract)); |
183
|
|
|
event(new EnumWasRegistered($abstract, $concrete)); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @inheritDoc |
188
|
|
|
*/ |
189
|
|
|
public function registerRequest(string $abstract, string $concrete) |
190
|
|
|
{ |
191
|
|
|
if (!is_subclass_of($concrete, $abstract, true)) { |
192
|
|
|
throw new InvalidArgumentException("Class {$concrete} must extend or implement {$abstract}. "); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
$this->requests[$abstract] = $concrete; |
196
|
|
|
$this->app->alias($concrete, $abstract); |
197
|
|
|
$this->registerShort($abstract, 'request'); |
198
|
|
|
|
199
|
|
|
event(new RequestWasRegistered($abstract, $concrete)); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @inheritDoc |
204
|
|
|
*/ |
205
|
|
|
public function enum(string $abstract) |
206
|
|
|
{ |
207
|
|
|
return array_get($this->enums, $abstract); |
|
|
|
|
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @inheritDoc |
212
|
|
|
*/ |
213
|
|
|
public function getEnumBindings(): Collection |
214
|
|
|
{ |
215
|
|
|
return collect($this->enums); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @inheritdoc |
220
|
|
|
*/ |
221
|
|
|
public function getRequestBindings(): Collection |
222
|
|
|
{ |
223
|
|
|
return collect($this->requests); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @inheritdoc |
228
|
|
|
*/ |
229
|
|
|
public function helper($name, $arguments = []) |
230
|
|
|
{ |
231
|
|
|
return $this->app->make('concord.helpers.' . $name, $arguments); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* @inheritdoc |
236
|
|
|
*/ |
237
|
|
|
public function getVersion(): string |
238
|
|
|
{ |
239
|
|
|
return self::VERSION; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @inheritdoc |
244
|
|
|
*/ |
245
|
|
|
public function short($name) |
246
|
|
|
{ |
247
|
|
|
return array_get($this->shorts, "$name.class"); |
|
|
|
|
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Resets the proxy class to ensure no stale instance gets stuck |
252
|
|
|
* |
253
|
|
|
* @param string $proxyClass |
254
|
|
|
*/ |
255
|
|
|
protected function resetProxy($proxyClass) |
256
|
|
|
{ |
257
|
|
|
$proxyClass::__reset(); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Register a model/enum/request shorthand |
263
|
|
|
* |
264
|
|
|
* @param string $abstract |
265
|
|
|
* @param string $type |
266
|
|
|
*/ |
267
|
|
|
protected function registerShort($abstract, $type) |
268
|
|
|
{ |
269
|
|
|
$this->shorts[shorten($abstract)] = [ |
270
|
|
|
'type' => $type, |
271
|
|
|
'class' => $abstract |
272
|
|
|
]; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* Merge the module's config with the existing config |
277
|
|
|
* |
278
|
|
|
* @param string $key |
279
|
|
|
* @param array $config |
280
|
|
|
*/ |
281
|
|
|
protected function mergeModuleConfig(string $key, array $config) |
282
|
|
|
{ |
283
|
|
|
if (!empty($config)) { |
284
|
|
|
$current = $this->app['config']->get($key); |
285
|
|
|
if (is_array($current)) { |
286
|
|
|
$this->app['config']->set($key, array_merge($current, $config)); |
287
|
|
|
} else { |
288
|
|
|
$this->app['config']->set($key, $config); |
289
|
|
|
} |
290
|
|
|
} |
291
|
|
|
} |
292
|
|
|
} |
293
|
|
|
|
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