Completed
Pull Request — master (#1151)
by
unknown
02:23
created

Module::getLaravel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Nwidart\Modules\Laravel;
5
6
use Illuminate\Cache\CacheManager;
7
use Illuminate\Container\Container;
8
use Illuminate\Filesystem\Filesystem;
9
use Illuminate\Foundation\AliasLoader;
10
use Illuminate\Foundation\ProviderRepository;
11
use Illuminate\Support\Arr;
12
use Illuminate\Support\Str;
13
use Illuminate\Translation\Translator;
14
use Nwidart\Modules\Contracts\ActivatorInterface;
15
use Nwidart\Modules\Entities\ModuleEntity;
16 3
use Nwidart\Modules\Json;
17
use Nwidart\Modules\Module as BaseModule;
18
19
class Module extends BaseModule
20 3
{
21
22
    /**
23
     * The laravel|lumen application instance.
24 3
     *
25
     * @var \Illuminate\Contracts\Foundation\Application|\Laravel\Lumen\Application
26
     */
27
    protected $app;
28
29
    /**
30 2
     * The module name.
31
     *
32 2
     * @var
33 2
     */
34 2
    public $name;
35
36
    /**
37
     * The module path.
38
     *
39
     * @var string
40
     */
41
    protected $path;
42
43
    /**
44
     * @var array of cached Json objects, keyed by filename
45
     */
46
    protected $moduleJson = [];
47
    /**
48
     * @var CacheManager
49
     */
50
    private $cache;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
51
    /**
52
     * @var Filesystem
53
     */
54
    private $files;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
55
    /**
56
     * @var Translator
57
     */
58
    private $translator;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
59
    /**
60
     * @var ActivatorInterface
61
     */
62
    private $activator;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
63
64
    private $id;
65
66
    private $attributes;
67
68
    /**
69
     * The constructor.
70
     *
71
     * @param Container $app
72
     * @param string    $name
73
     * @param           $path
74
     * @param array      $attributes
75
     */
76
    public function __construct(Container $app, string $name, $path, $attributes = [])
77
    {
78
        parent::__construct($app, $name, $path);
79
        $this->attributes = $attributes;
80
        $this->id = isset($this->attributes['id']) ? $this->attributes['id'] : false;
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function getCachedServicesPath(): string
87
    {
88
        return Str::replaceLast('services.php', $this->getSnakeName() . '_module.php', $this->app->getCachedServicesPath());
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function registerProviders(): void
95
    {
96
        (new ProviderRepository($this->app, new Filesystem(), $this->getCachedServicesPath()))
97
            ->load($this->get('providers', []));
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103
    public function registerAliases(): void
104
    {
105
        $loader = AliasLoader::getInstance();
106
        foreach ($this->get('aliases', []) as $aliasName => $aliasClass) {
107
            $loader->alias($aliasName, $aliasClass);
108
        }
109
    }
110
111
    public function getLaravel()
112
    {
113
        // TODO: Implement getLaravel() method.
114
    }
115
116
    public function enabled(): bool
117
    {
118
        return $this->attributes['is_active'];
119
    }
120
121
    public function disabled(): bool
122
    {
123
        return !$this->attributes['is_active'];
124
    }
125
126
    public function getId() {
127
        return $this->id;
128
    }
129
130
    public function getAttributes()
131
    {
132
        return $this->attributes;
133
    }
134
135
    /**
136
     * Get a specific data from json file by given the key.
137
     *
138
     * @param string $key
139
     * @param null $default
140
     *
141
     * @return mixed
142
     */
143
    public function get(string $key, $default = null)
144
    {
145
        return isset($this->attributes[$key]) ? $this->attributes[$key] : $default;
146
    }
147
148
    public function getAlias(): string
149
    {
150
        return $this->attributes['alias'];
151
    }
152
}
153