1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jumilla\Addomnipot\Laravel; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Foundation\Application; |
6
|
|
|
use Illuminate\Contracts\Config\Repository; |
7
|
|
|
use Jumilla\Addomnipot\Laravel\Repository\ConfigLoader; |
8
|
|
|
use RuntimeException; |
9
|
|
|
|
10
|
|
|
class Addon |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @param string $path |
14
|
|
|
* |
15
|
|
|
* @return static |
16
|
|
|
*/ |
17
|
9 |
|
public static function create($path) |
18
|
|
|
{ |
19
|
9 |
|
$pathComponents = explode('/', $path); |
20
|
|
|
|
21
|
9 |
|
$name = $pathComponents[count($pathComponents) - 1]; |
22
|
|
|
|
23
|
9 |
|
$addonConfig = static::loadConfig($path, $name); |
24
|
|
|
|
25
|
8 |
|
$config = ConfigLoader::load($path.'/'.array_get($addonConfig, 'paths.config', 'config')); |
26
|
|
|
|
27
|
8 |
|
$config->set('addon', $addonConfig); |
28
|
|
|
|
29
|
8 |
|
return new static($name, $path, $config); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param string $path |
34
|
|
|
* |
35
|
|
|
* @return array |
36
|
|
|
*/ |
37
|
9 |
|
protected static function loadConfig($path, $name) |
38
|
|
|
{ |
39
|
9 |
|
if (file_exists($path.'/addon.php')) { |
40
|
8 |
|
$config = require $path.'/addon.php'; |
41
|
9 |
|
} elseif (file_exists($path.'/addon.json')) { |
42
|
|
|
$config = json_decode(file_get_contents($path.'/addon.json'), true); |
43
|
|
|
|
44
|
|
|
if ($config === null) { |
45
|
|
|
throw new RuntimeException("Invalid json format at '$path/addon.json'."); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
// compatible v4 addon |
49
|
1 |
|
elseif (file_exists($path.'/config/addon.php')) { |
50
|
|
|
$config = require $path.'/config/addon.php'; |
51
|
|
|
} else { |
52
|
1 |
|
throw new RuntimeException("No such config file for addon '$name', need 'addon.php' or 'addon.json'."); |
53
|
|
|
} |
54
|
|
|
|
55
|
8 |
|
return $config; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var string |
60
|
|
|
*/ |
61
|
|
|
protected $name; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var string |
65
|
|
|
*/ |
66
|
|
|
protected $path; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @var \Illuminate\Contracts\Config\Repository |
70
|
|
|
*/ |
71
|
|
|
protected $config; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @var \Illuminate\Contracts\Foundation\Application |
75
|
|
|
*/ |
76
|
|
|
protected $app; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string $name |
80
|
|
|
* @param string $path |
81
|
|
|
* @param \Illuminate\Contracts\Config\Repository $config |
82
|
|
|
*/ |
83
|
18 |
|
public function __construct($name, $path, Repository $config) |
84
|
|
|
{ |
85
|
18 |
|
$this->name = $name; |
86
|
18 |
|
$this->path = $path; |
87
|
18 |
|
$this->config = $config; |
88
|
18 |
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* get name. |
92
|
|
|
* |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
13 |
|
public function name() |
96
|
|
|
{ |
97
|
13 |
|
return $this->name; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* get fullpath. |
102
|
|
|
* |
103
|
|
|
* @param string $path |
104
|
|
|
* |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
9 |
|
public function path($path = null) |
108
|
|
|
{ |
109
|
9 |
|
if (func_num_args() == 0) { |
110
|
4 |
|
return $this->path; |
111
|
|
|
} else { |
112
|
6 |
|
return $this->path.'/'.$path; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* get relative path. |
118
|
|
|
* |
119
|
|
|
* @param \Illuminate\Contracts\Foundation\Application $app |
120
|
|
|
* |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
8 |
|
public function relativePath(Application $app) |
124
|
|
|
{ |
125
|
8 |
|
return substr($this->path, strlen($app->basePath()) + 1); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* get version. |
130
|
|
|
* |
131
|
|
|
* @return int |
132
|
|
|
*/ |
133
|
7 |
|
public function version() |
134
|
|
|
{ |
135
|
7 |
|
return $this->config('addon.version', 5); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* get PHP namespace. |
140
|
|
|
* |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
9 |
|
public function phpNamespace() |
144
|
|
|
{ |
145
|
9 |
|
return trim($this->config('addon.namespace', ''), '\\'); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* get config value. |
150
|
|
|
* |
151
|
|
|
* @param string $key |
152
|
|
|
* @param mixed $default |
153
|
|
|
* |
154
|
|
|
* @return mixed |
155
|
|
|
*/ |
156
|
13 |
|
public function config($key, $default = null) |
157
|
|
|
{ |
158
|
13 |
|
return $this->config->get($key, $default); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Get a lang resource name |
163
|
|
|
* |
164
|
|
|
* @param string $resource |
165
|
|
|
* |
166
|
|
|
* @return string |
167
|
|
|
*/ |
168
|
1 |
|
public function transName($resource) |
169
|
|
|
{ |
170
|
1 |
|
return $this->name.'::'.$resource; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Translate the given message. |
175
|
|
|
* |
176
|
|
|
* @param string $id |
|
|
|
|
177
|
|
|
* @param array $parameters |
|
|
|
|
178
|
|
|
* @param string $domain |
|
|
|
|
179
|
|
|
* @param string $locale |
|
|
|
|
180
|
|
|
* @return string |
181
|
|
|
*/ |
182
|
1 |
View Code Duplication |
public function trans() |
|
|
|
|
183
|
|
|
{ |
184
|
1 |
|
$args = func_get_args(); |
185
|
1 |
|
$args[0] = $this->transName($args[0]); |
186
|
|
|
|
187
|
1 |
|
return call_user_func_array([$this->app['translator'], 'trans'], $args); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Translates the given message based on a count. |
192
|
|
|
* |
193
|
|
|
* @param string $id |
|
|
|
|
194
|
|
|
* @param int $number |
|
|
|
|
195
|
|
|
* @param array $parameters |
|
|
|
|
196
|
|
|
* @param string $domain |
|
|
|
|
197
|
|
|
* @param string $locale |
|
|
|
|
198
|
|
|
* @return string |
199
|
|
|
*/ |
200
|
1 |
View Code Duplication |
public function transChoice() |
|
|
|
|
201
|
|
|
{ |
202
|
1 |
|
$args = func_get_args(); |
203
|
1 |
|
$args[0] = $this->transName($args[0]); |
204
|
|
|
|
205
|
1 |
|
return call_user_func_array([$this->app['translator'], 'transChoice'], $args); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Get a view resource name |
210
|
|
|
* |
211
|
|
|
* @param string $resource |
212
|
|
|
* |
213
|
|
|
* @return string |
214
|
|
|
*/ |
215
|
|
|
public function viewName($resource) |
216
|
|
|
{ |
217
|
|
|
return $this->name.'::'.$resource; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @param string $view |
222
|
|
|
* @param array $data |
223
|
|
|
* @param array $mergeData |
224
|
|
|
* |
225
|
|
|
* @return \Illuminate\View\View |
226
|
|
|
*/ |
227
|
|
|
public function view($view, $data = [], $mergeData = []) |
228
|
|
|
{ |
229
|
|
|
return $this->app['view']->make($this->viewname($view), $data, $mergeData); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Get a spec resource name |
234
|
|
|
* |
235
|
|
|
* @param string $resource |
236
|
|
|
* |
237
|
|
|
* @return string |
238
|
|
|
*/ |
239
|
|
|
public function specName($resource) |
240
|
|
|
{ |
241
|
|
|
return $this->name.'::'.$resource; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Get spec. |
246
|
|
|
* |
247
|
|
|
* @param string $path |
248
|
|
|
* |
249
|
|
|
* @return \Jumilla\Addomnipot\Laravel\Specs\InputSpec |
250
|
|
|
*/ |
251
|
|
|
public function spec($path) |
252
|
|
|
{ |
253
|
|
|
return $this->app[SpecFactory::class]->make($this->specName($path)); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* register addon. |
258
|
|
|
* |
259
|
|
|
* @param \Illuminate\Contracts\Foundation\Application $app |
260
|
|
|
*/ |
261
|
4 |
|
public function register(Application $app) |
262
|
|
|
{ |
263
|
4 |
|
$this->app = $app; |
264
|
|
|
|
265
|
4 |
|
$version = $this->version(); |
266
|
4 |
|
if ($version == 4) { |
267
|
1 |
|
$this->registerV4($app); |
268
|
4 |
|
} elseif ($version == 5) { |
269
|
3 |
|
$this->registerV5($app); |
270
|
3 |
|
} else { |
271
|
|
|
throw new RuntimeException($version.': Illigal addon version.'); |
272
|
|
|
} |
273
|
4 |
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* register addon version 4. |
277
|
|
|
* |
278
|
|
|
* @param \Illuminate\Contracts\Foundation\Application $app |
279
|
|
|
*/ |
280
|
1 |
|
protected function registerV4(Application $app) |
281
|
|
|
{ |
282
|
1 |
|
$this->config['paths'] = [ |
283
|
1 |
|
'assets' => 'assets', |
284
|
1 |
|
'lang' => 'lang', |
285
|
1 |
|
'migrations' => 'migrations', |
286
|
1 |
|
'seeds' => 'seeds', |
287
|
1 |
|
'specs' => 'specs', |
288
|
1 |
|
'views' => 'views', |
289
|
|
|
]; |
290
|
|
|
|
291
|
|
|
// regist service providers |
292
|
1 |
|
$providers = $this->config('addon.providers', []); |
293
|
1 |
|
foreach ($providers as $provider) { |
294
|
|
|
if (!starts_with($provider, '\\')) { |
295
|
|
|
$provider = sprintf('%s\%s', $this->phpNamespace(), $provider); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
$app->register($provider); |
299
|
1 |
|
} |
300
|
1 |
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* register addon version 5. |
304
|
|
|
* |
305
|
|
|
* @param \Illuminate\Contracts\Foundation\Application $app |
306
|
|
|
*/ |
307
|
3 |
|
protected function registerV5(Application $app) |
308
|
|
|
{ |
309
|
|
|
// regist service providers |
310
|
3 |
|
$providers = $this->config('addon.providers', []); |
311
|
3 |
|
foreach ($providers as $provider) { |
312
|
1 |
|
$app->register($provider); |
313
|
3 |
|
} |
314
|
3 |
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* boot addon. |
318
|
|
|
* |
319
|
|
|
* @param \Illuminate\Contracts\Foundation\Application $app |
320
|
|
|
*/ |
321
|
3 |
|
public function boot(Application $app) |
322
|
|
|
{ |
323
|
3 |
|
$this->registerPackage($app); |
324
|
|
|
|
325
|
3 |
|
$version = $this->version(); |
326
|
3 |
|
if ($version == 4) { |
327
|
1 |
|
$this->bootV4($app); |
328
|
3 |
|
} elseif ($version == 5) { |
329
|
2 |
|
$this->bootV5($app); |
330
|
2 |
|
} else { |
331
|
|
|
throw new RuntimeException($version.': Illigal addon version.'); |
332
|
|
|
} |
333
|
|
|
|
334
|
3 |
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* boot addon version 4. |
338
|
|
|
* |
339
|
|
|
* @param \Illuminate\Contracts\Foundation\Application $app |
340
|
|
|
*/ |
341
|
1 |
|
protected function bootV4(Application $app) |
342
|
|
|
{ |
343
|
1 |
|
$filenames = $this->config('addon.files'); |
344
|
|
|
|
345
|
1 |
|
$files = []; |
346
|
|
|
|
347
|
1 |
|
if ($filenames !== null) { |
348
|
|
|
foreach ($filenames as $filename) { |
349
|
|
|
$files[] = $this->path($filename); |
350
|
|
|
} |
351
|
|
|
} else { |
352
|
|
|
// load *.php on addon's root directory |
353
|
1 |
|
foreach ($app['files']->files($this->path) as $file) { |
354
|
|
|
if (ends_with($file, '.php')) { |
355
|
|
|
require $file; |
356
|
|
|
} |
357
|
1 |
|
} |
358
|
|
|
} |
359
|
|
|
|
360
|
1 |
|
$this->loadFiles($files); |
361
|
1 |
|
} |
362
|
|
|
|
363
|
|
|
/** |
364
|
|
|
* boot addon version 5. |
365
|
|
|
* |
366
|
|
|
* @param \Illuminate\Contracts\Foundation\Application $app |
367
|
|
|
*/ |
368
|
2 |
|
protected function bootV5(Application $app) |
|
|
|
|
369
|
|
|
{ |
370
|
2 |
|
$filenames = $this->config('addon.files'); |
371
|
|
|
|
372
|
2 |
|
$files = []; |
373
|
|
|
|
374
|
2 |
|
if ($filenames !== null) { |
375
|
1 |
|
foreach ($filenames as $filename) { |
376
|
1 |
|
$files[] = $this->path($filename); |
377
|
1 |
|
} |
378
|
1 |
|
} |
379
|
|
|
|
380
|
2 |
|
$this->loadFiles($files); |
381
|
2 |
|
} |
382
|
|
|
|
383
|
|
|
/** |
384
|
|
|
* load addon initial script files. |
385
|
|
|
* |
386
|
|
|
* @param array $files |
387
|
|
|
*/ |
388
|
3 |
|
protected function loadFiles(array $files) |
389
|
|
|
{ |
390
|
3 |
|
foreach ($files as $file) { |
391
|
1 |
|
if (!file_exists($file)) { |
392
|
|
|
$message = "Warning: PHP Script '$file' is nothing."; |
393
|
|
|
info($message); |
394
|
|
|
echo $message; |
395
|
|
|
continue; |
396
|
|
|
} |
397
|
|
|
|
398
|
1 |
|
require_once $file; |
399
|
3 |
|
} |
400
|
3 |
|
} |
401
|
|
|
/** |
402
|
|
|
* Register the package's component namespaces. |
403
|
|
|
* |
404
|
|
|
* @param \Illuminate\Contracts\Foundation\Application $app |
405
|
|
|
*/ |
406
|
3 |
|
protected function registerPackage(Application $app) |
407
|
|
|
{ |
408
|
3 |
|
$namespace = $this->name(); |
409
|
|
|
|
410
|
3 |
|
$lang = $this->path($this->config('addon.paths.lang', 'lang')); |
411
|
3 |
|
if (is_dir($lang)) { |
412
|
1 |
|
$app['translator']->addNamespace($namespace, $lang); |
413
|
1 |
|
} |
414
|
|
|
|
415
|
3 |
|
$view = $this->path($this->config('addon.paths.views', 'views')); |
416
|
3 |
|
if (is_dir($view)) { |
417
|
1 |
|
$app['view']->addNamespace($namespace, $view); |
418
|
1 |
|
} |
419
|
|
|
|
420
|
3 |
|
$spec = $this->path($this->config('addon.paths.specs', 'specs')); |
421
|
3 |
|
if (is_dir($spec)) { |
422
|
1 |
|
$app['specs']->addNamespace($namespace, $spec); |
423
|
1 |
|
} |
424
|
3 |
|
} |
425
|
|
|
} |
426
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.