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
|
5 |
|
public static function create($path) |
18
|
|
|
{ |
19
|
5 |
|
$pathComponents = explode('/', $path); |
20
|
|
|
|
21
|
5 |
|
$name = $pathComponents[count($pathComponents) - 1]; |
22
|
|
|
|
23
|
5 |
|
$addonConfig = static::loadConfig($path, $name); |
24
|
|
|
|
25
|
4 |
|
$config = ConfigLoader::load($path.'/'.array_get($addonConfig, 'paths.config', 'config')); |
26
|
|
|
|
27
|
4 |
|
$config->set('addon', $addonConfig); |
28
|
|
|
|
29
|
4 |
|
return new static($name, $path, $config); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param string $path |
34
|
|
|
* |
35
|
|
|
* @return array |
36
|
|
|
*/ |
37
|
5 |
|
protected static function loadConfig($path, $name) |
38
|
|
|
{ |
39
|
5 |
|
if (file_exists($path.'/addon.php')) { |
40
|
4 |
|
$config = require $path.'/addon.php'; |
41
|
5 |
|
} 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
|
2 |
|
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
|
4 |
|
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
|
14 |
|
public function __construct($name, $path, Repository $config) |
84
|
|
|
{ |
85
|
14 |
|
$this->name = $name; |
86
|
14 |
|
$this->path = $path; |
87
|
14 |
|
$this->config = $config; |
88
|
14 |
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* get name. |
92
|
|
|
* |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
5 |
|
public function name() |
96
|
|
|
{ |
97
|
5 |
|
return $this->name; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* get fullpath. |
102
|
|
|
* |
103
|
|
|
* @param string $path |
104
|
|
|
* |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
7 |
|
public function path($path = null) |
108
|
|
|
{ |
109
|
7 |
|
if (func_num_args() == 0) { |
110
|
3 |
|
return $this->path; |
111
|
|
|
} else { |
112
|
5 |
|
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
|
2 |
|
public function relativePath(Application $app) |
124
|
|
|
{ |
125
|
2 |
|
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
|
7 |
|
public function phpNamespace() |
144
|
|
|
{ |
145
|
7 |
|
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
|
11 |
|
public function config($key, $default = null) |
157
|
|
|
{ |
158
|
11 |
|
return $this->config->get($key, $default); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Translate the given message. |
163
|
|
|
* |
164
|
|
|
* @param string $id |
|
|
|
|
165
|
|
|
* @param array $parameters |
|
|
|
|
166
|
|
|
* @param string $domain |
|
|
|
|
167
|
|
|
* @param string $locale |
|
|
|
|
168
|
|
|
* @return string |
169
|
|
|
*/ |
170
|
1 |
View Code Duplication |
public function trans() |
|
|
|
|
171
|
|
|
{ |
172
|
1 |
|
$args = func_get_args(); |
173
|
1 |
|
$args[0] = $this->name.'::'.$args[0]; |
174
|
|
|
|
175
|
1 |
|
return call_user_func_array([$this->app['translator'], 'trans'], $args); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Translates the given message based on a count. |
180
|
|
|
* |
181
|
|
|
* @param string $id |
|
|
|
|
182
|
|
|
* @param int $number |
|
|
|
|
183
|
|
|
* @param array $parameters |
|
|
|
|
184
|
|
|
* @param string $domain |
|
|
|
|
185
|
|
|
* @param string $locale |
|
|
|
|
186
|
|
|
* @return string |
187
|
|
|
*/ |
188
|
1 |
View Code Duplication |
public function transChoice() |
|
|
|
|
189
|
|
|
{ |
190
|
1 |
|
$args = func_get_args(); |
191
|
1 |
|
$args[0] = $this->name.'::'.$args[0]; |
192
|
|
|
|
193
|
1 |
|
return call_user_func_array([$this->app['translator'], 'transChoice'], $args); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @param string $view |
198
|
|
|
* @param array $data |
199
|
|
|
* @param array $mergeData |
200
|
|
|
* |
201
|
|
|
* @return \Illuminate\View\View |
202
|
|
|
*/ |
203
|
|
|
public function view($view, $data = [], $mergeData = []) |
204
|
|
|
{ |
205
|
|
|
return $this->app['view']->make($this->name.'::'.$view, $data, $mergeData); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Get spec. |
210
|
|
|
* |
211
|
|
|
* @param string $path |
212
|
|
|
* |
213
|
|
|
* @return \Jumilla\Addomnipot\Laravel\Specs\InputSpec |
214
|
|
|
*/ |
215
|
|
|
public function spec($path) |
216
|
|
|
{ |
217
|
|
|
return $this->app[SpecFactory::class]->make($this->name.'::'.$path); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* register addon. |
222
|
|
|
* |
223
|
|
|
* @param \Illuminate\Contracts\Foundation\Application $app |
224
|
|
|
*/ |
225
|
4 |
|
public function register(Application $app) |
226
|
|
|
{ |
227
|
4 |
|
$version = $this->version(); |
228
|
4 |
|
if ($version == 4) { |
229
|
1 |
|
$this->registerV4($app); |
230
|
4 |
|
} elseif ($version == 5) { |
231
|
3 |
|
$this->registerV5($app); |
232
|
3 |
|
} else { |
233
|
|
|
throw new RuntimeException($version.': Illigal addon version.'); |
234
|
|
|
} |
235
|
4 |
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* register addon version 4. |
239
|
|
|
* |
240
|
|
|
* @param \Illuminate\Contracts\Foundation\Application $app |
241
|
|
|
*/ |
242
|
1 |
|
private function registerV4(Application $app) |
243
|
|
|
{ |
244
|
1 |
|
$this->app = $app; |
245
|
|
|
|
246
|
1 |
|
$this->config['paths'] = [ |
247
|
1 |
|
'assets' => 'assets', |
248
|
1 |
|
'lang' => 'lang', |
249
|
1 |
|
'migrations' => 'migrations', |
250
|
1 |
|
'seeds' => 'seeds', |
251
|
1 |
|
'specs' => 'specs', |
252
|
1 |
|
'views' => 'views', |
253
|
|
|
]; |
254
|
|
|
|
255
|
|
|
// regist service providers |
256
|
1 |
|
$providers = $this->config('addon.providers', []); |
257
|
1 |
|
foreach ($providers as $provider) { |
258
|
|
|
if (!starts_with($provider, '\\')) { |
259
|
|
|
$provider = sprintf('%s\%s', $this->phpNamespace(), $provider); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
$app->register($provider); |
263
|
1 |
|
} |
264
|
1 |
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* register addon version 5. |
268
|
|
|
* |
269
|
|
|
* @param \Illuminate\Contracts\Foundation\Application $app |
270
|
|
|
*/ |
271
|
3 |
|
private function registerV5(Application $app) |
272
|
|
|
{ |
273
|
3 |
|
$this->app = $app; |
274
|
|
|
|
275
|
|
|
// regist service providers |
276
|
3 |
|
$providers = $this->config('addon.providers', []); |
277
|
3 |
|
foreach ($providers as $provider) { |
278
|
1 |
|
$app->register($provider); |
279
|
3 |
|
} |
280
|
3 |
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* boot addon. |
284
|
|
|
* |
285
|
|
|
* @param \Illuminate\Contracts\Foundation\Application $app |
286
|
|
|
*/ |
287
|
3 |
|
public function boot(Application $app) |
288
|
|
|
{ |
289
|
3 |
|
$version = $this->version(); |
290
|
3 |
|
if ($version == 4) { |
291
|
1 |
|
$this->bootV4($app); |
292
|
3 |
|
} elseif ($version == 5) { |
293
|
2 |
|
$this->bootV5($app); |
294
|
2 |
|
} else { |
295
|
|
|
throw new RuntimeException($version.': Illigal addon version.'); |
296
|
|
|
} |
297
|
3 |
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* boot addon version 4. |
301
|
|
|
* |
302
|
|
|
* @param \Illuminate\Contracts\Foundation\Application $app |
303
|
|
|
*/ |
304
|
1 |
|
private function bootV4(Application $app) |
305
|
|
|
{ |
306
|
1 |
|
$filenames = $this->config('addon.files'); |
307
|
|
|
|
308
|
1 |
|
$files = []; |
309
|
|
|
|
310
|
1 |
|
if ($filenames !== null) { |
311
|
|
|
foreach ($filenames as $filename) { |
312
|
|
|
$files[] = $this->path($filename); |
313
|
|
|
} |
314
|
|
|
} else { |
315
|
|
|
// load *.php on addon's root directory |
316
|
1 |
|
foreach ($app['files']->files($this->path) as $file) { |
317
|
|
|
if (ends_with($file, '.php')) { |
318
|
|
|
require $file; |
319
|
|
|
} |
320
|
1 |
|
} |
321
|
|
|
} |
322
|
|
|
|
323
|
1 |
|
$this->loadFiles($files); |
324
|
1 |
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* boot addon version 5. |
328
|
|
|
* |
329
|
|
|
* @param \Illuminate\Contracts\Foundation\Application $app |
330
|
|
|
*/ |
331
|
2 |
|
private function bootV5(Application $app) |
|
|
|
|
332
|
|
|
{ |
333
|
2 |
|
$filenames = $this->config('addon.files'); |
334
|
|
|
|
335
|
2 |
|
$files = []; |
336
|
|
|
|
337
|
2 |
|
if ($filenames !== null) { |
338
|
1 |
|
foreach ($filenames as $filename) { |
339
|
1 |
|
$files[] = $this->path($filename); |
340
|
1 |
|
} |
341
|
1 |
|
} |
342
|
|
|
|
343
|
2 |
|
$this->loadFiles($files); |
344
|
2 |
|
} |
345
|
|
|
|
346
|
|
|
/** |
347
|
|
|
* load addon initial script files. |
348
|
|
|
* |
349
|
|
|
* @param array $files |
350
|
|
|
*/ |
351
|
3 |
|
private function loadFiles(array $files) |
352
|
|
|
{ |
353
|
3 |
|
foreach ($files as $file) { |
354
|
1 |
|
if (!file_exists($file)) { |
355
|
|
|
$message = "Warning: PHP Script '$file' is nothing."; |
356
|
|
|
info($message); |
357
|
|
|
echo $message; |
358
|
|
|
} |
359
|
|
|
|
360
|
1 |
|
include $file; |
361
|
3 |
|
} |
362
|
3 |
|
} |
363
|
|
|
} |
364
|
|
|
|
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.