Completed
Pull Request — master (#46)
by John
03:27
created

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