1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jumilla\Addomnipot\Laravel; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Foundation\Application; |
6
|
|
|
use Illuminate\Config\Repository; |
7
|
|
|
use Symfony\Component\Finder\Finder; |
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 |
|
$config = static::loadAddonConfig($path, $name); |
24
|
|
|
|
25
|
8 |
|
return new static($name, $path, $config); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param string $path |
30
|
|
|
* @param string $name |
31
|
|
|
* |
32
|
|
|
* @return array |
33
|
|
|
*/ |
34
|
9 |
|
protected static function loadAddonConfig($path, $name) |
35
|
|
|
{ |
36
|
9 |
|
if (file_exists($path.'/addon.php')) { |
37
|
8 |
|
$config = require $path.'/addon.php'; |
38
|
8 |
|
} else { |
39
|
1 |
|
throw new RuntimeException("No such config file for addon '$name', need 'addon.php'."); |
40
|
|
|
} |
41
|
|
|
|
42
|
8 |
|
$version = array_get($config, 'version', 5); |
43
|
8 |
|
if ($version != 5) { |
44
|
|
|
throw new RuntimeException($version.': Illigal addon version.'); |
45
|
|
|
} |
46
|
|
|
|
47
|
8 |
|
return $config; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
protected $name; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var string |
57
|
|
|
*/ |
58
|
|
|
protected $path; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var \Illuminate\Contracts\Config\Repository |
62
|
|
|
*/ |
63
|
|
|
protected $config; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @var \Illuminate\Contracts\Foundation\Application |
67
|
|
|
*/ |
68
|
|
|
protected $app; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string $name |
72
|
|
|
* @param string $path |
73
|
|
|
* @param array $config |
74
|
|
|
*/ |
75
|
16 |
|
public function __construct($name, $path, array $config) |
76
|
|
|
{ |
77
|
16 |
|
$this->name = $name; |
78
|
16 |
|
$this->path = $path; |
79
|
16 |
|
$this->config = new Repository(); |
80
|
16 |
|
$this->config->set('addon', $config); |
81
|
16 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* get name. |
85
|
|
|
* |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
11 |
|
public function name() |
89
|
|
|
{ |
90
|
11 |
|
return $this->name; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* get fullpath. |
95
|
|
|
* |
96
|
|
|
* @param string $path |
97
|
|
|
* |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
8 |
|
public function path($path = null) |
101
|
|
|
{ |
102
|
8 |
|
if (func_num_args() == 0) { |
103
|
3 |
|
return $this->path; |
104
|
|
|
} else { |
105
|
7 |
|
return $this->path.'/'.$path; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* get relative path. |
111
|
|
|
* |
112
|
|
|
* @param \Illuminate\Contracts\Foundation\Application $app |
113
|
|
|
* |
114
|
|
|
* @return string |
115
|
|
|
*/ |
116
|
8 |
|
public function relativePath(Application $app) |
117
|
|
|
{ |
118
|
8 |
|
return substr($this->path, strlen($app->basePath()) + 1); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* get version. |
123
|
|
|
* |
124
|
|
|
* @return int |
125
|
|
|
*/ |
126
|
2 |
|
public function version() |
127
|
|
|
{ |
128
|
2 |
|
return $this->config('addon.version', 5); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* get PHP namespace. |
133
|
|
|
* |
134
|
|
|
* @return string |
135
|
|
|
*/ |
136
|
8 |
|
public function phpNamespace() |
137
|
|
|
{ |
138
|
8 |
|
return trim($this->config('addon.namespace', ''), '\\'); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* get config value. |
143
|
|
|
* |
144
|
|
|
* @param string $key |
145
|
|
|
* @param mixed $default |
146
|
|
|
* |
147
|
|
|
* @return mixed |
148
|
|
|
*/ |
149
|
11 |
|
public function config($key, $default = null) |
150
|
|
|
{ |
151
|
11 |
|
return $this->config->get($key, $default); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Get a lang resource name |
156
|
|
|
* |
157
|
|
|
* @param string $resource |
158
|
|
|
* |
159
|
|
|
* @return string |
160
|
|
|
*/ |
161
|
1 |
|
public function transName($resource) |
162
|
|
|
{ |
163
|
1 |
|
return $this->name.'::'.$resource; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Translate the given message. |
168
|
|
|
* |
169
|
|
|
* @param string $id |
|
|
|
|
170
|
|
|
* @param array $parameters |
|
|
|
|
171
|
|
|
* @param string $domain |
|
|
|
|
172
|
|
|
* @param string $locale |
|
|
|
|
173
|
|
|
* @return string |
174
|
|
|
*/ |
175
|
1 |
View Code Duplication |
public function trans() |
|
|
|
|
176
|
|
|
{ |
177
|
1 |
|
$args = func_get_args(); |
178
|
1 |
|
$args[0] = $this->transName($args[0]); |
179
|
|
|
|
180
|
1 |
|
return call_user_func_array([$this->app['translator'], 'trans'], $args); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Translates the given message based on a count. |
185
|
|
|
* |
186
|
|
|
* @param string $id |
|
|
|
|
187
|
|
|
* @param int $number |
|
|
|
|
188
|
|
|
* @param array $parameters |
|
|
|
|
189
|
|
|
* @param string $domain |
|
|
|
|
190
|
|
|
* @param string $locale |
|
|
|
|
191
|
|
|
* @return string |
192
|
|
|
*/ |
193
|
1 |
View Code Duplication |
public function transChoice() |
|
|
|
|
194
|
|
|
{ |
195
|
1 |
|
$args = func_get_args(); |
196
|
1 |
|
$args[0] = $this->transName($args[0]); |
197
|
|
|
|
198
|
1 |
|
return call_user_func_array([$this->app['translator'], 'transChoice'], $args); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Get a view resource name |
203
|
|
|
* |
204
|
|
|
* @param string $resource |
205
|
|
|
* |
206
|
|
|
* @return string |
207
|
|
|
*/ |
208
|
|
|
public function viewName($resource) |
209
|
|
|
{ |
210
|
|
|
return $this->name.'::'.$resource; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @param string $view |
215
|
|
|
* @param array $data |
216
|
|
|
* @param array $mergeData |
217
|
|
|
* |
218
|
|
|
* @return \Illuminate\View\View |
219
|
|
|
*/ |
220
|
|
|
public function view($view, $data = [], $mergeData = []) |
221
|
|
|
{ |
222
|
|
|
return $this->app['view']->make($this->viewname($view), $data, $mergeData); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Get a spec resource name |
227
|
|
|
* |
228
|
|
|
* @param string $resource |
229
|
|
|
* |
230
|
|
|
* @return string |
231
|
|
|
*/ |
232
|
|
|
public function specName($resource) |
233
|
|
|
{ |
234
|
|
|
return $this->name.'::'.$resource; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Get spec. |
239
|
|
|
* |
240
|
|
|
* @param string $path |
241
|
|
|
* |
242
|
|
|
* @return \Jumilla\Addomnipot\Laravel\Specs\InputSpec |
243
|
|
|
*/ |
244
|
|
|
public function spec($path) |
245
|
|
|
{ |
246
|
|
|
return $this->app[SpecFactory::class]->make($this->specName($path)); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* register addon. |
251
|
|
|
* |
252
|
|
|
* @param \Illuminate\Contracts\Foundation\Application $app |
253
|
|
|
*/ |
254
|
3 |
|
public function register(Application $app) |
255
|
|
|
{ |
256
|
3 |
|
$this->app = $app; |
257
|
|
|
|
258
|
|
|
// prepare helper functions |
259
|
3 |
|
$this->loadFiles($this->config('addon.files', [])); |
260
|
|
|
|
261
|
|
|
// load config |
262
|
3 |
|
$this->loadConfigurationFiles($this->path($this->config('addon.paths.config', 'config'))); |
263
|
|
|
|
264
|
|
|
// regist service providers |
265
|
3 |
|
$providers = $this->config('addon.providers', []); |
266
|
3 |
|
foreach ($providers as $provider) { |
267
|
1 |
|
$app->register($provider); |
268
|
3 |
|
} |
269
|
3 |
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* boot addon. |
273
|
|
|
* |
274
|
|
|
* @param \Illuminate\Contracts\Foundation\Application $app |
275
|
|
|
*/ |
276
|
2 |
|
public function boot(Application $app) |
277
|
|
|
{ |
278
|
2 |
|
$this->registerPackage($app); |
279
|
2 |
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Load the configuration items from all of the files. |
283
|
|
|
* |
284
|
|
|
* @param string $directoryPath |
285
|
|
|
*/ |
286
|
3 |
|
protected function loadConfigurationFiles($directoryPath) |
287
|
|
|
{ |
288
|
3 |
|
foreach ($this->getConfigurationFiles($directoryPath) as $group => $path) { |
289
|
|
|
$this->config->set($group, require $path); |
290
|
3 |
|
} |
291
|
3 |
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* Get all of the configuration files for the directory. |
295
|
|
|
* |
296
|
|
|
* @param string $directoryPath |
297
|
|
|
* |
298
|
|
|
* @return array |
299
|
|
|
*/ |
300
|
3 |
|
protected function getConfigurationFiles($directoryPath) |
301
|
|
|
{ |
302
|
3 |
|
$files = []; |
303
|
|
|
|
304
|
3 |
|
if (is_dir($directoryPath)) { |
305
|
1 |
|
foreach (Finder::create()->files()->in($directoryPath) as $file) { |
306
|
|
|
$group = basename($file->getRealPath(), '.php'); |
307
|
|
|
$files[$group] = $file->getRealPath(); |
308
|
1 |
|
} |
309
|
1 |
|
} |
310
|
|
|
|
311
|
3 |
|
return $files; |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* load addon initial script files. |
316
|
|
|
* |
317
|
|
|
* @param array $files |
318
|
|
|
*/ |
319
|
3 |
|
protected function loadFiles(array $files) |
320
|
|
|
{ |
321
|
3 |
|
foreach ($files as $filename) { |
322
|
1 |
|
$path = $this->path($filename); |
323
|
|
|
|
324
|
1 |
|
if (!file_exists($path)) { |
325
|
|
|
$message = "Warning: PHP Script '$path' is nothing."; |
326
|
|
|
info($message); |
327
|
|
|
echo $message; |
328
|
|
|
continue; |
329
|
|
|
} |
330
|
|
|
|
331
|
1 |
|
require_once $path; |
332
|
3 |
|
} |
333
|
3 |
|
} |
334
|
|
|
/** |
335
|
|
|
* Register the package's component namespaces. |
336
|
|
|
* |
337
|
|
|
* @param \Illuminate\Contracts\Foundation\Application $app |
338
|
|
|
*/ |
339
|
2 |
|
protected function registerPackage(Application $app) |
340
|
|
|
{ |
341
|
2 |
|
$namespace = $this->name(); |
342
|
|
|
|
343
|
2 |
|
$lang = $this->path($this->config('addon.paths.lang', 'lang')); |
344
|
2 |
|
if (is_dir($lang)) { |
345
|
1 |
|
$app['translator']->addNamespace($namespace, $lang); |
346
|
1 |
|
} |
347
|
|
|
|
348
|
2 |
|
$view = $this->path($this->config('addon.paths.views', 'views')); |
349
|
2 |
|
if (is_dir($view)) { |
350
|
1 |
|
$app['view']->addNamespace($namespace, $view); |
351
|
1 |
|
} |
352
|
|
|
|
353
|
2 |
|
$spec = $this->path($this->config('addon.paths.specs', 'specs')); |
354
|
2 |
|
if (is_dir($spec)) { |
355
|
1 |
|
$app['specs']->addNamespace($namespace, $spec); |
356
|
1 |
|
} |
357
|
2 |
|
} |
358
|
|
|
} |
359
|
|
|
|
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.