Completed
Push — master ( 9620dc...f09c52 )
by Nicolas
04:04 queued 12s
created

Module::loadTranslationsFrom()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Nwidart\Modules;
4
5
use Illuminate\Cache\CacheManager;
6
use Illuminate\Container\Container;
7
use Illuminate\Filesystem\Filesystem;
8
use Illuminate\Support\Arr;
9
use Illuminate\Support\Str;
10
use Illuminate\Support\Traits\Macroable;
11
use Illuminate\Translation\Translator;
12
use Nwidart\Modules\Contracts\ActivatorInterface;
13
14
abstract class Module
15
{
16
    use Macroable;
17
18
    /**
19
     * The laravel|lumen application instance.
20
     *
21
     * @var \Illuminate\Contracts\Foundation\Application|\Laravel\Lumen\Application
22
     */
23
    protected $app;
24
25
    /**
26
     * The module name.
27
     *
28
     * @var
29
     */
30
    protected $name;
31
32
    /**
33
     * The module path.
34
     *
35
     * @var string
36
     */
37
    protected $path;
38
39
    /**
40
     * @var array of cached Json objects, keyed by filename
41
     */
42
    protected $moduleJson = [];
43
    /**
44
     * @var CacheManager
45
     */
46
    private $cache;
47
    /**
48
     * @var Filesystem
49
     */
50
    private $files;
51
    /**
52
     * @var Translator
53
     */
54
    private $translator;
55
56
    /**
57
     * The constructor.
58
     * @param Container $app
59
     * @param $name
60
     * @param $path
61
     */
62 177 View Code Duplication
    public function __construct(Container $app, string $name, $path)
63
    {
64 177
        $this->name = $name;
65 177
        $this->path = $path;
66 177
        $this->cache = $app['cache'];
67 177
        $this->files = $app['files'];
68 177
        $this->translator = $app['translator'];
69 177
        $this->activator = $app[ActivatorInterface::class];
70 177
        $this->app = $app;
0 ignored issues
show
Documentation Bug introduced by
It seems like $app of type object<Illuminate\Container\Container> is incompatible with the declared type object<Illuminate\Contra...avel\Lumen\Application> of property $app.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

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