Completed
Push — master ( 758c52...a36606 )
by Nicolas
05:12
created

Module::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 3
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 43
    public function __construct(Application $app, $name, $path)
41
    {
42 43
        $this->app = $app;
43 43
        $this->name = $name;
44 43
        $this->path = realpath($path);
45 43
    }
46
47
    /**
48
     * Get laravel instance.
49
     *
50
     * @return \Illuminate\Foundation\Application
51
     */
52
    public function getLaravel()
53
    {
54
        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 28
    public function getLowerName()
73
    {
74 28
        return strtolower($this->name);
75
    }
76
77
    /**
78
     * Get name in studly case.
79
     *
80
     * @return string
81
     */
82 22
    public function getStudlyName()
83
    {
84 22
        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 1
    }
96
97
    /**
98
     * Get alias.
99
     *
100
     * @return string
101
     */
102 1
    public function getAlias()
103
    {
104 1
        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 path.
119
     *
120
     * @return string
121
     */
122 36
    public function getPath()
123
    {
124 36
        return $this->path;
125
    }
126
127
    /**
128
     * Set path.
129
     *
130
     * @param string $path
131
     *
132
     * @return $this
133
     */
134
    public function setPath($path)
135
    {
136
        $this->path = $path;
137
138
        return $this;
139
    }
140
141
    /**
142
     * Bootstrap the application events.
143
     */
144 1
    public function boot()
145
    {
146 1
        $this->registerTranslation();
147
148 1
        $this->fireEvent('boot');
149 1
    }
150
151
    /**
152
     * Register module's translation.
153
     *
154
     * @return void
155
     */
156 1
    protected function registerTranslation()
157
    {
158 1
        $lowerName = $this->getLowerName();
159
160 1
        $langPath = $this->getPath() . "/Resources/lang";
161
162 1
        if (is_dir($langPath)) {
163 1
            $this->loadTranslationsFrom($langPath, $lowerName);
164 1
        }
165 1
    }
166
167
    /**
168
     * Get json contents.
169
     *
170
     * @return Json
171
     */
172 15
    public function json($file = null)
173
    {
174 15
        if (is_null($file)) {
175 14
            $file = 'module.json';
176 14
        }
177
178 15
        return new Json($this->getPath() . '/' . $file, $this->app['files']);
179
    }
180
181
    /**
182
     * Get a specific data from json file by given the key.
183
     *
184
     * @param $key
185
     * @param null $default
186
     *
187
     * @return mixed
188
     */
189 12
    public function get($key, $default = null)
190
    {
191 12
        return $this->json()->get($key, $default);
192
    }
193
194
    /**
195
     * Get a specific data from composer.json file by given the key.
196
     *
197
     * @param $key
198
     * @param null $default
199
     *
200
     * @return mixed
201
     */
202 1
    public function getComposerAttr($key, $default = null)
203
    {
204 1
        return $this->json('composer.json')->get($key, $default);
205
    }
206
207
    /**
208
     * Register the module.
209
     */
210
    public function register()
211
    {
212
        $this->registerAliases();
213
214
        $this->registerProviders();
215
216
        $this->registerFiles();
217
218
        $this->fireEvent('register');
219
    }
220
221
    /**
222
     * Register the module event.
223
     *
224
     * @param string $event
225
     */
226 1
    protected function fireEvent($event)
227
    {
228 1
        $this->app['events']->fire(sprintf('modules.%s.'.$event, $this->getLowerName()), [$this]);
229 1
    }
230
231
    /**
232
     * Register the aliases from this module.
233
     */
234
    protected function registerAliases()
235
    {
236
        $loader = AliasLoader::getInstance();
237
        foreach ($this->get('aliases', []) as $aliasName => $aliasClass) {
238
            $loader->alias($aliasName, $aliasClass);
239
        }
240
    }
241
242
    /**
243
     * Register the service providers from this module.
244
     */
245
    protected function registerProviders()
246
    {
247
        foreach ($this->get('providers', []) as $provider) {
248
            $this->app->register($provider);
249
        }
250
    }
251
252
    /**
253
     * Register the files from this module.
254
     */
255
    protected function registerFiles()
256
    {
257
        foreach ($this->get('files', []) as $file) {
258
            include $this->path.'/'.$file;
259
        }
260
    }
261
262
    /**
263
     * Handle call __toString.
264
     *
265
     * @return string
266
     */
267 2
    public function __toString()
268
    {
269 2
        return $this->getStudlyName();
270
    }
271
272
    /**
273
     * Determine whether the given status same with the current module status.
274
     *
275
     * @param $status
276
     *
277
     * @return bool
278
     */
279 9
    public function isStatus($status)
280
    {
281 9
        return $this->get('active', 0) === $status;
282
    }
283
284
    /**
285
     * Determine whether the current module activated.
286
     *
287
     * @return bool
288
     */
289 1
    public function enabled()
290
    {
291 1
        return $this->active();
292
    }
293
294
    /**
295
     * Alternate for "enabled" method.
296
     *
297
     * @return bool
298
     */
299 5
    public function active()
300
    {
301 5
        return $this->isStatus(1);
302
    }
303
304
    /**
305
     * Determine whether the current module not activated.
306
     *
307
     * @return bool
308
     */
309 1
    public function notActive()
310
    {
311 1
        return !$this->active();
312
    }
313
314
    /**
315
     * Alias for "notActive" method.
316
     *
317
     * @return bool
318
     */
319 1
    public function disabled()
320
    {
321 1
        return !$this->enabled();
322
    }
323
324
    /**
325
     * Set active state for current module.
326
     *
327
     * @param $active
328
     *
329
     * @return bool
330
     */
331 2
    public function setActive($active)
332
    {
333 2
        return $this->json()->set('active', $active)->save();
334
    }
335
336
    /**
337
     * Disable the current module.
338
     *
339
     * @return bool
340
     */
341 1
    public function disable()
342
    {
343 1
        $this->app['events']->fire('module.disabling', [$this]);
344
345 1
        $this->setActive(0);
346
347 1
        $this->app['events']->fire('module.disabled', [$this]);
348 1
    }
349
350
    /**
351
     * Enable the current module.
352
     */
353 1
    public function enable()
354
    {
355 1
        $this->app['events']->fire('module.enabling', [$this]);
356
357 1
        $this->setActive(1);
358
359 1
        $this->app['events']->fire('module.enabled', [$this]);
360 1
    }
361
362
    /**
363
     * Delete the current module.
364
     *
365
     * @return bool
366
     */
367 1
    public function delete()
368
    {
369 1
        return $this->json()->getFilesystem()->deleteDirectory($this->getPath());
370
    }
371
372
    /**
373
     * Get extra path.
374
     *
375
     * @param $path
376
     *
377
     * @return string
378
     */
379 3
    public function getExtraPath($path)
380
    {
381 3
        return $this->getPath().'/'.$path;
382
    }
383
384
    /**
385
     * Handle call to __get method.
386
     *
387
     * @param $key
388
     *
389
     * @return mixed
390
     */
391
    public function __get($key)
392
    {
393
        return $this->get($key);
394
    }
395
}
396