Completed
Push — master ( 84d0c3...f99381 )
by Nicolas
08:21
created

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