|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: Mark |
|
5
|
|
|
* Date: 20/07/2016 |
|
6
|
|
|
* Time: 18:49. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace App\Classes; |
|
10
|
|
|
|
|
11
|
|
|
use App\Model\Plugin; |
|
12
|
|
|
use Illuminate\Support\Collection; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class PluginManager. |
|
16
|
|
|
*/ |
|
17
|
|
|
class PluginManager |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Enabled plugins. |
|
21
|
|
|
* |
|
22
|
|
|
* @var array |
|
23
|
|
|
*/ |
|
24
|
|
|
private $enabled = []; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Disabled plugins. |
|
28
|
|
|
* |
|
29
|
|
|
* @var array |
|
30
|
|
|
*/ |
|
31
|
|
|
private $disabled = []; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Viewable by user plugin. |
|
35
|
|
|
* |
|
36
|
|
|
* @var array |
|
37
|
|
|
*/ |
|
38
|
|
|
private $viewable = []; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Add a plugin to the plugin manager for application usage. |
|
42
|
|
|
* |
|
43
|
|
|
* @param Plugin $plugin |
|
44
|
|
|
* @return $this |
|
45
|
|
|
* @throws \Exception |
|
46
|
|
|
*/ |
|
47
|
|
|
public function add(Plugin $plugin) |
|
48
|
|
|
{ |
|
49
|
|
|
if ($plugin->isEnabled()) { |
|
50
|
|
|
// set as a viewable plugin |
|
51
|
|
|
if ($plugin->isHidden() == false) { |
|
|
|
|
|
|
52
|
|
|
$this->viewable[$plugin->name()] = $plugin; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
// add to enabled plugins. |
|
56
|
|
|
$this->enabled[$plugin->name()] = $plugin; |
|
57
|
|
|
} else { |
|
58
|
|
|
$this->disabled[$plugin->name()] = $plugin; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return $this; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Return all the application loaded plugins. |
|
66
|
|
|
* |
|
67
|
|
|
* @return array |
|
68
|
|
|
*/ |
|
69
|
|
|
public function all() |
|
70
|
|
|
{ |
|
71
|
|
|
return array_merge($this->enabled, $this->disabled); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Check if the plugins is enabled (boolean) |
|
76
|
|
|
* or return all enabled plugins by default. |
|
77
|
|
|
* |
|
78
|
|
|
* @param null $plugin |
|
|
|
|
|
|
79
|
|
|
* @return array|bool |
|
80
|
|
|
*/ |
|
81
|
|
|
public function enabled($plugin = null) |
|
82
|
|
|
{ |
|
83
|
|
|
if ($plugin != null) { |
|
84
|
|
|
return $this->checkStatus($this->getPlugin($plugin), true); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return $this->enabled; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Check if the plugin is disabled (boolean) |
|
92
|
|
|
* Or return all disabled plugins by default. |
|
93
|
|
|
* |
|
94
|
|
|
* @param null $plugin |
|
|
|
|
|
|
95
|
|
|
* @return array|bool |
|
96
|
|
|
*/ |
|
97
|
|
|
public function disabled($plugin = null) |
|
98
|
|
|
{ |
|
99
|
|
|
if ($plugin != null) { |
|
100
|
|
|
return $this->checkStatus($this->getPlugin($plugin), false); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return $this->disabled; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Check does the user have the plugin. Boolean. |
|
108
|
|
|
* |
|
109
|
|
|
* @param $plugin_name |
|
110
|
|
|
* @return bool |
|
111
|
|
|
*/ |
|
112
|
|
|
public function hasPlugin($plugin_name) |
|
113
|
|
|
{ |
|
114
|
|
|
return array_key_exists($plugin_name, $this->enabled); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Get the loaded plugin from the array. |
|
119
|
|
|
* |
|
120
|
|
|
* @param $plugin_name |
|
121
|
|
|
* @return mixed |
|
122
|
|
|
* @throws \Exception |
|
123
|
|
|
*/ |
|
124
|
|
|
private function getPlugin($plugin_name) |
|
125
|
|
|
{ |
|
126
|
|
|
if (array_key_exists($plugin_name, $this->all())) { |
|
127
|
|
|
return $this->all()[$plugin_name]; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
throw new \Exception(sprintf('Cannot load the plugin (%s) as it does not exist.', $plugin_name)); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Check the status of a loaded plugin. |
|
135
|
|
|
* |
|
136
|
|
|
* @param array $plugin |
|
137
|
|
|
* @param $status |
|
138
|
|
|
* @return bool |
|
139
|
|
|
*/ |
|
140
|
|
|
private function checkStatus($plugin, $status) |
|
141
|
|
|
{ |
|
142
|
|
|
return $plugin['enabled'] === $status; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* For tenant usage, we must add a collection of settings models from the database. |
|
147
|
|
|
* |
|
148
|
|
|
* Can be used for anything else though. |
|
149
|
|
|
* |
|
150
|
|
|
* @param Collection $collection |
|
151
|
|
|
* @return $this |
|
152
|
|
|
*/ |
|
153
|
|
|
public function collect(Collection $collection) |
|
154
|
|
|
{ |
|
155
|
|
|
/** @var Plugin $model */ |
|
156
|
|
|
foreach ($collection as $model) { |
|
157
|
|
|
$this->add($model); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
return $this; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Check if plugin is viewable (booleans) |
|
165
|
|
|
* or return all viewable plugins by default. |
|
166
|
|
|
* |
|
167
|
|
|
* @param null $plugin |
|
|
|
|
|
|
168
|
|
|
* @return array|bool |
|
169
|
|
|
*/ |
|
170
|
|
|
public function viewable($plugin = null) |
|
171
|
|
|
{ |
|
172
|
|
|
if ($plugin != null) { |
|
173
|
|
|
return $this->checkViewable($this->getPlugin($plugin), true); |
|
|
|
|
|
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
return $this->viewable; |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|
When comparing two booleans, it is generally considered safer to use the strict comparison operator.