Completed
Push — master ( cc7b64...53b465 )
by Nicolas
11s
created

Module::getComposerAttr()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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