Completed
Pull Request — master (#322)
by Mathieu
03:43
created

Module::getPriority()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 0
cts 0
cp 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Nwidart\Modules;
4
5
use Illuminate\Container\Container;
6
use Illuminate\Filesystem\Filesystem;
7
use Illuminate\Foundation\AliasLoader;
8
use Illuminate\Foundation\ProviderRepository;
9
use Illuminate\Support\ServiceProvider;
10
use Illuminate\Support\Str;
11
use Illuminate\Support\Traits\Macroable;
12
13
class Module extends ServiceProvider
14
{
15
    use Macroable;
16
17
    /**
18
     * The laravel|lumen application instance.
19
     *
20
     * @var Illuminate\Contracts\Foundation\Application|Laravel\Lumen\Application
21
     */
22
    protected $app;
23
24
    /**
25
     * The module name.
26
     *
27
     * @var
28
     */
29
    protected $name;
30
31
    /**
32
     * The module path.
33
     *
34
     * @var string
35
     */
36
    protected $path;
37
38
    /**
39
     * @var array of cached Json objects, keyed by filename
40
     */
41
    protected $moduleJson = [];
42
43
    /**
44
     * The constructor.
45
     *
46
     * @param Container $app
47
     * @param $name
48
     * @param $path
49
     */
50 86
    public function __construct(Container $app, $name, $path)
51
    {
52 86
        parent::__construct($app);
0 ignored issues
show
Documentation introduced by
$app is of type object<Illuminate\Container\Container>, but the function expects a object<Illuminate\Contra...Foundation\Application>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
53 86
        $this->name = $name;
54 86
        $this->path = realpath($path);
55 86
    }
56
57
    /**
58
     * Get laravel instance.
59
     *
60
     * @return Illuminate\Contracts\Foundation\Application|Laravel\Lumen\Application
61
     */
62 1
    public function getLaravel()
63
    {
64 1
        return $this->app;
65
    }
66
67
    /**
68
     * Get name.
69
     *
70
     * @return string
71
     */
72 3
    public function getName()
73
    {
74 3
        return $this->name;
75
    }
76
77
    /**
78
     * Get name in lower case.
79
     *
80
     * @return string
81
     */
82 68
    public function getLowerName()
83
    {
84 68
        return strtolower($this->name);
85
    }
86
87
    /**
88
     * Get name in studly case.
89
     *
90
     * @return string
91
     */
92 55
    public function getStudlyName()
93
    {
94 55
        return Str::studly($this->name);
95
    }
96
97
  /**
98
   * Get name in snake case.
99
   *
100
   * @return string
101
   */
102
    public function getSnakeName()
103
    {
104
      return Str::snake($this->name);
105
    }
106
107
    /**
108
     * Get description.
109
     *
110
     * @return string
111
     */
112 1
    public function getDescription()
113
    {
114 1
        return $this->get('description');
115
    }
116
117
    /**
118
     * Get alias.
119
     *
120
     * @return string
121
     */
122 3
    public function getAlias()
123
    {
124 3
        return $this->get('alias');
125
    }
126
127
    /**
128
     * Get priority.
129
     *
130
     * @return string
131
     */
132
    public function getPriority()
133
    {
134
        return $this->get('priority');
135
    }
136
137
    /**
138
     * Get module requirements.
139
     *
140
     * @return array
141
     */
142 2
    public function getRequires()
143
    {
144 2
        return $this->get('requires');
145
    }
146
147
    /**
148
     * Get path.
149
     *
150
     * @return string
151
     */
152 77
    public function getPath()
153
    {
154 77
        return $this->path;
155
    }
156
157
    /**
158
     * Set path.
159
     *
160
     * @param string $path
161
     *
162
     * @return $this
163
     */
164
    public function setPath($path)
165
    {
166
        $this->path = $path;
167
168
        return $this;
169
    }
170
171
    /**
172
     * Bootstrap the application events.
173
     */
174 1
    public function boot()
175
    {
176 1
        if (config('modules.register.translations', true) === true) {
177 1
            $this->registerTranslation();
178
        }
179
180 1
        $this->fireEvent('boot');
181 1
    }
182
183
    /**
184
     * Register module's translation.
185
     *
186
     * @return void
187
     */
188 1
    protected function registerTranslation()
189
    {
190 1
        $lowerName = $this->getLowerName();
191
192 1
        $langPath = $this->getPath() . "/Resources/lang";
193
194 1
        if (is_dir($langPath)) {
195 1
            $this->loadTranslationsFrom($langPath, $lowerName);
196
        }
197 1
    }
198
199
    /**
200
     * Get json contents from the cache, setting as needed.
201
     *
202
     * @param $file
203
     *
204
     * @return Json
205
     */
206 22
    public function json($file = null)
207
    {
208 22
        if ($file === null) {
209 21
            $file = 'module.json';
210
        }
211
212 22
        return array_get($this->moduleJson, $file, function () use ($file) {
213 22
            return $this->moduleJson[$file] = new Json($this->getPath() . '/' . $file, $this->app['files']);
214 22
        });
215
    }
216
217
    /**
218
     * Get a specific data from json file by given the key.
219
     *
220
     * @param $key
221
     * @param null $default
222
     *
223
     * @return mixed
224
     */
225 16
    public function get($key, $default = null)
226
    {
227 16
        return $this->json()->get($key, $default);
228
    }
229
230
    /**
231
     * Get a specific data from composer.json file by given the key.
232
     *
233
     * @param $key
234
     * @param null $default
235
     *
236
     * @return mixed
237
     */
238 1
    public function getComposerAttr($key, $default = null)
239
    {
240 1
        return $this->json('composer.json')->get($key, $default);
241
    }
242
243
    /**
244
     * Register the module.
245
     */
246
    public function register()
247
    {
248
        $this->registerAliases();
249
250
        $this->registerProviders();
251
252
        $this->registerFiles();
253
254
        $this->fireEvent('register');
255
    }
256
257
    /**
258
     * Register the module event.
259
     *
260
     * @param string $event
261
     */
262 5
    protected function fireEvent($event)
263
    {
264 5
        $this->app['events']->fire(sprintf('modules.%s.' . $event, $this->getLowerName()), [$this]);
265 5
    }
266
267
    /**
268
     * Register the aliases from this module.
269
     */
270
    protected function registerAliases()
271
    {
272
        $loader = AliasLoader::getInstance();
273
        foreach ($this->get('aliases', []) as $aliasName => $aliasClass) {
274
            $loader->alias($aliasName, $aliasClass);
275
        }
276
    }
277
278
    /**
279
     * Register the service providers from this module.
280
     */
281
    protected function registerProviders()
282
    {
283
      (new ProviderRepository($this->app, new Filesystem, $this->getCachedServicesPath()))
284
        ->load($this->get('providers', []));
285
    }
286
287
  /**
288
   * Get the path to the cached *_module.php file.
289
   *
290
   * @return string
291
   */
292
    public function getCachedServicesPath()
293
    {
294
      return Str::replaceLast('services.php', $this->getSnakeName().'_module.php', $this->app->getCachedServicesPath());
295
    }
296
297
    /**
298
     * Register the files from this module.
299
     */
300
    protected function registerFiles()
301
    {
302
        foreach ($this->get('files', []) as $file) {
303
            include $this->path . '/' . $file;
304
        }
305
    }
306
307
    /**
308
     * Handle call __toString.
309
     *
310
     * @return string
311
     */
312 2
    public function __toString()
313
    {
314 2
        return $this->getStudlyName();
315
    }
316
317
    /**
318
     * Determine whether the given status same with the current module status.
319
     *
320
     * @param $status
321
     *
322
     * @return bool
323
     */
324 9
    public function isStatus($status)
325
    {
326 9
        return $this->get('active', 0) === $status;
327
    }
328
329
    /**
330
     * Determine whether the current module activated.
331
     *
332
     * @return bool
333
     */
334 1
    public function enabled()
335
    {
336 1
        return $this->active();
337
    }
338
339
    /**
340
     * Alternate for "enabled" method.
341
     *
342
     * @return bool
343
     */
344 5
    public function active()
345
    {
346 5
        return $this->isStatus(1);
347
    }
348
349
    /**
350
     * Determine whether the current module not activated.
351
     *
352
     * @return bool
353
     */
354 1
    public function notActive()
355
    {
356 1
        return !$this->active();
357
    }
358
359
    /**
360
     * Alias for "notActive" method.
361
     *
362
     * @return bool
363
     */
364 1
    public function disabled()
365
    {
366 1
        return !$this->enabled();
367
    }
368
369
    /**
370
     * Set active state for current module.
371
     *
372
     * @param $active
373
     *
374
     * @return bool
375
     */
376 4
    public function setActive($active)
377
    {
378 4
        return $this->json()->set('active', $active)->save();
379
    }
380
381
    /**
382
     * Disable the current module.
383
     */
384 2
    public function disable()
385
    {
386 2
        $this->fireEvent('disabling');
387
388 2
        $this->setActive(0);
389
390 2
        $this->fireEvent('disabled');
391 2
    }
392
393
    /**
394
     * Enable the current module.
395
     */
396 2
    public function enable()
397
    {
398 2
        $this->fireEvent('enabling');
399
400 2
        $this->setActive(1);
401
402 2
        $this->fireEvent('enabled');
403 2
    }
404
405
    /**
406
     * Delete the current module.
407
     *
408
     * @return bool
409
     */
410 2
    public function delete()
411
    {
412 2
        return $this->json()->getFilesystem()->deleteDirectory($this->getPath());
413
    }
414
415
    /**
416
     * Get extra path.
417
     *
418
     * @param $path
419
     *
420
     * @return string
421
     */
422 3
    public function getExtraPath($path)
423
    {
424 3
        return $this->getPath() . '/' . $path;
425
    }
426
427
    /**
428
     * Handle call to __get method.
429
     *
430
     * @param $key
431
     *
432
     * @return mixed
433
     */
434
    public function __get($key)
435
    {
436
        return $this->get($key);
437
    }
438
}
439