Completed
Push — master ( 6e27f1...d5fec2 )
by Nicolas
03:36
created

Module::getAlias()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 144
    public function __construct(Container $app, $name, $path)
48
    {
49 144
        parent::__construct($app);
50 144
        $this->name = $name;
51 144
        $this->path = $path;
52 144
    }
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 108
    public function getLowerName()
80
    {
81 108
        return strtolower($this->name);
82
    }
83
84
    /**
85
     * Get name in studly case.
86
     *
87
     * @return string
88
     */
89 94
    public function getStudlyName()
90
    {
91 94
        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 128
    public function getPath()
150
    {
151 128
        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
        if ($this->isLoadFilesOnBoot()) {
178
            $this->registerFiles();
179
        }
180
181 2
        $this->fireEvent('boot');
182 2
    }
183
184
    /**
185
     * Register module's translation.
186
     *
187
     * @return void
188
     */
189 2
    protected function registerTranslation()
190
    {
191 2
        $lowerName = $this->getLowerName();
192
193 2
        $langPath = $this->getPath() . '/Resources/lang';
194
195 2
        if (is_dir($langPath)) {
196 2
            $this->loadTranslationsFrom($langPath, $lowerName);
197
        }
198 2
    }
199
200
    /**
201
     * Get json contents from the cache, setting as needed.
202
     *
203
     * @param string $file
204
     *
205
     * @return Json
206
     */
207 34
    public function json($file = null) : Json
208
    {
209 34
        if ($file === null) {
210 32
            $file = 'module.json';
211
        }
212
213
        return array_get($this->moduleJson, $file, function () use ($file) {
214 34
            return $this->moduleJson[$file] = new Json($this->getPath() . '/' . $file, $this->app['files']);
215 34
        });
216
    }
217
218
    /**
219
     * Get a specific data from json file by given the key.
220
     *
221
     * @param string $key
222
     * @param null $default
223
     *
224
     * @return mixed
225
     */
226 24
    public function get(string $key, $default = null)
227
    {
228 24
        return $this->json()->get($key, $default);
229
    }
230
231
    /**
232
     * Get a specific data from composer.json file by given the key.
233
     *
234
     * @param $key
235
     * @param null $default
236
     *
237
     * @return mixed
238
     */
239 2
    public function getComposerAttr($key, $default = null)
240
    {
241 2
        return $this->json('composer.json')->get($key, $default);
242
    }
243
244
    /**
245
     * Register the module.
246
     */
247
    public function register()
248
    {
249
        $this->registerAliases();
250
251
        $this->registerProviders();
252
253
        if ($this->isLoadFilesOnBoot() === false) {
254
            $this->registerFiles();
255
        }
256
257
        $this->fireEvent('register');
258
    }
259
260
    /**
261
     * Register the module event.
262
     *
263
     * @param string $event
264
     */
265 8
    protected function fireEvent($event)
266
    {
267 8
        $this->app['events']->fire(sprintf('modules.%s.' . $event, $this->getLowerName()), [$this]);
268 8
    }
269
    /**
270
     * Register the aliases from this module.
271
     */
272
    abstract public function registerAliases();
273
274
    /**
275
     * Register the service providers from this module.
276
     */
277
    abstract public function registerProviders();
278
279
    /**
280
     * Get the path to the cached *_module.php file.
281
     *
282
     * @return string
283
     */
284
    abstract public function getCachedServicesPath();
285
286
    /**
287
     * Register the files from this module.
288
     */
289
    protected function registerFiles()
290
    {
291
        foreach ($this->get('files', []) as $file) {
292
            include $this->path . '/' . $file;
293
        }
294
    }
295
296
    /**
297
     * Handle call __toString.
298
     *
299
     * @return string
300
     */
301 3
    public function __toString()
302
    {
303 3
        return $this->getStudlyName();
304
    }
305
306
    /**
307
     * Determine whether the given status same with the current module status.
308
     *
309
     * @param $status
310
     *
311
     * @return bool
312
     */
313 11
    public function isStatus($status) : bool
314
    {
315 11
        return $this->get('active', 0) === $status;
316
    }
317
318
    /**
319
     * Determine whether the current module activated.
320
     *
321
     * @return bool
322
     */
323 6
    public function enabled() : bool
324
    {
325 6
        return $this->isStatus(1);
326
    }
327
328
    /**
329
     *  Determine whether the current module not disabled.
330
     *
331
     * @return bool
332
     */
333 2
    public function disabled() : bool
334
    {
335 2
        return !$this->enabled();
336
    }
337
338
    /**
339
     * Set active state for current module.
340
     *
341
     * @param $active
342
     *
343
     * @return bool
344
     */
345 6
    public function setActive($active)
346
    {
347 6
        return $this->json()->set('active', $active)->save();
348
    }
349
350
    /**
351
     * Disable the current module.
352
     */
353 3
    public function disable()
354
    {
355 3
        $this->fireEvent('disabling');
356
357 3
        $this->setActive(0);
358
359 3
        $this->fireEvent('disabled');
360 3
    }
361
362
    /**
363
     * Enable the current module.
364
     */
365 3
    public function enable()
366
    {
367 3
        $this->fireEvent('enabling');
368
369 3
        $this->setActive(1);
370
371 3
        $this->fireEvent('enabled');
372 3
    }
373
374
    /**
375
     * Delete the current module.
376
     *
377
     * @return bool
378
     */
379 2
    public function delete()
380
    {
381 2
        return $this->json()->getFilesystem()->deleteDirectory($this->getPath());
382
    }
383
384
    /**
385
     * Get extra path.
386
     *
387
     * @param string $path
388
     *
389
     * @return string
390
     */
391 3
    public function getExtraPath(string $path) : string
392
    {
393 3
        return $this->getPath() . '/' . $path;
394
    }
395
396
    /**
397
     * Handle call to __get method.
398
     *
399
     * @param $key
400
     *
401
     * @return mixed
402
     */
403
    public function __get($key)
404
    {
405
        return $this->get($key);
406
    }
407
408
    /**
409
     * Check if can load files of module on boot method.
410
     *
411
     * @return bool
412
     */
413 2
    protected function isLoadFilesOnBoot()
414
    {
415 2
        return config('modules.register.files', 'register') === 'boot' &&
416
            // force register method if option == boot && app is AsgardCms
417 2
            !class_exists('\Modules\Core\Foundation\AsgardCms');
418
    }
419
}
420