1
|
|
|
<?php namespace Anomaly\Streams\Platform\Addon; |
2
|
|
|
|
3
|
|
|
use Anomaly\Streams\Platform\Addon\Event\AddonsHaveRegistered; |
4
|
|
|
use Anomaly\Streams\Platform\Addon\Extension\ExtensionModel; |
5
|
|
|
use Anomaly\Streams\Platform\Addon\Module\ModuleModel; |
6
|
|
|
use Illuminate\Contracts\Events\Dispatcher; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class AddonManager |
10
|
|
|
* |
11
|
|
|
* @link http://pyrocms.com/ |
12
|
|
|
* @author PyroCMS, Inc. <[email protected]> |
13
|
|
|
* @author Ryan Thompson <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class AddonManager |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The addon paths. |
20
|
|
|
* |
21
|
|
|
* @var AddonPaths |
22
|
|
|
*/ |
23
|
|
|
protected $paths; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The addon collection. |
27
|
|
|
* |
28
|
|
|
* @var AddonCollection |
29
|
|
|
*/ |
30
|
|
|
protected $addons; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The addon loader. |
34
|
|
|
* |
35
|
|
|
* @var AddonLoader |
36
|
|
|
*/ |
37
|
|
|
protected $loader; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The addon integrator. |
41
|
|
|
* |
42
|
|
|
* @var AddonIntegrator |
43
|
|
|
*/ |
44
|
|
|
protected $integrator; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* The modules model. |
48
|
|
|
* |
49
|
|
|
* @var ModuleModel |
50
|
|
|
*/ |
51
|
|
|
protected $modules; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* The event dispatcher. |
55
|
|
|
* |
56
|
|
|
* @var Dispatcher |
57
|
|
|
*/ |
58
|
|
|
protected $dispatcher; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* The extensions model. |
62
|
|
|
* |
63
|
|
|
* @var ExtensionModel |
64
|
|
|
*/ |
65
|
|
|
protected $extensions; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Create a new AddonManager instance. |
69
|
|
|
* |
70
|
|
|
* @param AddonPaths $paths |
71
|
|
|
* @param AddonLoader $loader |
72
|
|
|
* @param ModuleModel $modules |
73
|
|
|
* @param Dispatcher $dispatcher |
74
|
|
|
* @param ExtensionModel $extensions |
75
|
|
|
* @param AddonIntegrator $integrator |
76
|
|
|
* @param AddonCollection $addons |
77
|
|
|
*/ |
78
|
|
|
public function __construct( |
79
|
|
|
AddonPaths $paths, |
80
|
|
|
AddonLoader $loader, |
81
|
|
|
ModuleModel $modules, |
82
|
|
|
Dispatcher $dispatcher, |
83
|
|
|
ExtensionModel $extensions, |
84
|
|
|
AddonIntegrator $integrator, |
85
|
|
|
AddonCollection $addons |
86
|
|
|
) { |
87
|
|
|
$this->paths = $paths; |
88
|
|
|
$this->addons = $addons; |
89
|
|
|
$this->loader = $loader; |
90
|
|
|
$this->modules = $modules; |
91
|
|
|
$this->integrator = $integrator; |
92
|
|
|
$this->dispatcher = $dispatcher; |
93
|
|
|
$this->extensions = $extensions; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Register all addons. |
98
|
|
|
*/ |
99
|
|
|
public function register() |
100
|
|
|
{ |
101
|
|
|
$enabled = $this->getEnabledAddonNamespaces(); |
102
|
|
|
$installed = $this->getInstalledAddonNamespaces(); |
103
|
|
|
|
104
|
|
|
$paths = $this->paths->all(); |
105
|
|
|
|
106
|
|
|
/* |
107
|
|
|
* First load all the addons |
108
|
|
|
* so they're available. |
109
|
|
|
*/ |
110
|
|
|
foreach ($paths as $path) { |
111
|
|
|
$this->loader->load($path); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$this->loader->register(); |
115
|
|
|
|
116
|
|
|
/* |
117
|
|
|
* Then register all of the addons now |
118
|
|
|
* that they're all PSR autoloaded. |
119
|
|
|
*/ |
120
|
|
|
foreach ($paths as $path) { |
121
|
|
|
|
122
|
|
|
$namespace = $this->getAddonNamespace($path); |
123
|
|
|
|
124
|
|
|
$this->integrator->register( |
125
|
|
|
$path, |
126
|
|
|
$namespace, |
127
|
|
|
in_array($namespace, $enabled), |
128
|
|
|
in_array($namespace, $installed) |
129
|
|
|
); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
// Sort all addons. |
133
|
|
|
$this->addons = $this->addons->sort(); |
134
|
|
|
|
135
|
|
|
/* |
136
|
|
|
* Disperse addons to their |
137
|
|
|
* respective collections and |
138
|
|
|
* finish the integration service. |
139
|
|
|
*/ |
140
|
|
|
$this->addons->disperse(); |
141
|
|
|
$this->addons->registered(); |
142
|
|
|
$this->integrator->finish(); |
143
|
|
|
|
144
|
|
|
$this->dispatcher->fire(new AddonsHaveRegistered($this->addons)); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Get namespaces for enabled addons. |
149
|
|
|
* |
150
|
|
|
* @return array |
151
|
|
|
*/ |
152
|
|
View Code Duplication |
protected function getEnabledAddonNamespaces() |
|
|
|
|
153
|
|
|
{ |
154
|
|
|
if (!env('INSTALLED')) { |
155
|
|
|
return []; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
$modules = $this->modules->cache( |
159
|
|
|
'streams::modules.enabled', |
160
|
|
|
9999, |
161
|
|
|
function () { |
162
|
|
|
return $this->modules->getEnabledNamespaces()->all(); |
163
|
|
|
} |
164
|
|
|
); |
165
|
|
|
|
166
|
|
|
$extensions = $this->extensions->cache( |
167
|
|
|
'streams::extensions.enabled', |
168
|
|
|
9999, |
169
|
|
|
function () { |
170
|
|
|
return $this->extensions->getEnabledNamespaces()->all(); |
171
|
|
|
} |
172
|
|
|
); |
173
|
|
|
|
174
|
|
|
return array_merge($modules, $extensions); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Get namespaces for installed addons. |
179
|
|
|
* |
180
|
|
|
* @return array |
181
|
|
|
*/ |
182
|
|
View Code Duplication |
protected function getInstalledAddonNamespaces() |
|
|
|
|
183
|
|
|
{ |
184
|
|
|
if (!env('INSTALLED')) { |
185
|
|
|
return []; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
$modules = $this->modules->cache( |
189
|
|
|
'streams::modules.installed', |
190
|
|
|
9999, |
191
|
|
|
function () { |
192
|
|
|
return $this->modules->getInstalledNamespaces()->all(); |
193
|
|
|
} |
194
|
|
|
); |
195
|
|
|
|
196
|
|
|
$extensions = $this->extensions->cache( |
197
|
|
|
'streams::extensions.installed', |
198
|
|
|
9999, |
199
|
|
|
function () { |
200
|
|
|
return $this->extensions->getInstalledNamespaces()->all(); |
201
|
|
|
} |
202
|
|
|
); |
203
|
|
|
|
204
|
|
|
return array_merge($modules, $extensions); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Get the addon namespace. |
209
|
|
|
* |
210
|
|
|
* @param $path |
211
|
|
|
* @return string |
212
|
|
|
*/ |
213
|
|
|
protected function getAddonNamespace($path) |
214
|
|
|
{ |
215
|
|
|
$vendor = strtolower(basename(dirname($path))); |
216
|
|
|
$slug = strtolower(substr(basename($path), 0, strpos(basename($path), '-'))); |
217
|
|
|
$type = strtolower(substr(basename($path), strpos(basename($path), '-') + 1)); |
218
|
|
|
|
219
|
|
|
return "{$vendor}.{$type}.{$slug}"; |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.