-
-
Notifications
You must be signed in to change notification settings - Fork 836
/
ExtensionManager.php
338 lines (277 loc) · 8.35 KB
/
ExtensionManager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Extension;
use Flarum\Database\Migrator;
use Flarum\Extension\Event\Disabled;
use Flarum\Extension\Event\Disabling;
use Flarum\Extension\Event\Enabled;
use Flarum\Extension\Event\Enabling;
use Flarum\Extension\Event\Uninstalled;
use Flarum\Foundation\Paths;
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Database\Schema\Builder;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
class ExtensionManager
{
protected $config;
/**
* @var Paths
*/
protected $paths;
protected $container;
protected $migrator;
/**
* @var Dispatcher
*/
protected $dispatcher;
/**
* @var Filesystem
*/
protected $filesystem;
/**
* @var Collection|null
*/
protected $extensions;
public function __construct(
SettingsRepositoryInterface $config,
Paths $paths,
Container $container,
Migrator $migrator,
Dispatcher $dispatcher,
Filesystem $filesystem
) {
$this->config = $config;
$this->paths = $paths;
$this->container = $container;
$this->migrator = $migrator;
$this->dispatcher = $dispatcher;
$this->filesystem = $filesystem;
}
/**
* @return Collection
*/
public function getExtensions()
{
if (is_null($this->extensions) && $this->filesystem->exists($this->paths->vendor.'/composer/installed.json')) {
$extensions = new Collection();
// Load all packages installed by composer.
$installed = json_decode($this->filesystem->get($this->paths->vendor.'/composer/installed.json'), true);
// Composer 2.0 changes the structure of the installed.json manifest
$installed = $installed['packages'] ?? $installed;
foreach ($installed as $package) {
if (Arr::get($package, 'type') != 'flarum-extension' || empty(Arr::get($package, 'name'))) {
continue;
}
$path = isset($package['install-path'])
? $this->paths->vendor.'/composer/'.$package['install-path']
: $this->paths->vendor.'/'.Arr::get($package, 'name');
// Instantiates an Extension object using the package path and composer.json file.
$extension = new Extension($path, $package);
// Per default all extensions are installed if they are registered in composer.
$extension->setInstalled(true);
$extension->setVersion(Arr::get($package, 'version'));
$extensions->put($extension->getId(), $extension);
}
$this->extensions = $extensions->sortBy(function ($extension, $name) {
return $extension->composerJsonAttribute('extra.flarum-extension.title');
});
}
return $this->extensions;
}
/**
* Loads an Extension with all information.
*
* @param string $name
* @return Extension|null
*/
public function getExtension($name)
{
return $this->getExtensions()->get($name);
}
/**
* Enables the extension.
*
* @param string $name
*/
public function enable($name)
{
if ($this->isEnabled($name)) {
return;
}
$extension = $this->getExtension($name);
$this->dispatcher->dispatch(new Enabling($extension));
$enabled = $this->getEnabled();
$enabled[] = $name;
$this->migrate($extension);
$this->publishAssets($extension);
$this->setEnabled($enabled);
$extension->enable($this->container);
$this->dispatcher->dispatch(new Enabled($extension));
}
/**
* Disables an extension.
*
* @param string $name
*/
public function disable($name)
{
$enabled = $this->getEnabled();
if (($k = array_search($name, $enabled)) === false) {
return;
}
$extension = $this->getExtension($name);
$this->dispatcher->dispatch(new Disabling($extension));
unset($enabled[$k]);
$this->setEnabled($enabled);
$extension->disable($this->container);
$this->dispatcher->dispatch(new Disabled($extension));
}
/**
* Uninstalls an extension.
*
* @param string $name
*/
public function uninstall($name)
{
$extension = $this->getExtension($name);
$this->disable($name);
$this->migrateDown($extension);
$this->unpublishAssets($extension);
$extension->setInstalled(false);
$this->dispatcher->dispatch(new Uninstalled($extension));
}
/**
* Copy the assets from an extension's assets directory into public view.
*
* @param Extension $extension
*/
protected function publishAssets(Extension $extension)
{
if ($extension->hasAssets()) {
$this->filesystem->copyDirectory(
$extension->getPath().'/assets',
$this->paths->public.'/assets/extensions/'.$extension->getId()
);
}
}
/**
* Delete an extension's assets from public view.
*
* @param Extension $extension
*/
protected function unpublishAssets(Extension $extension)
{
$this->filesystem->deleteDirectory($this->paths->public.'/assets/extensions/'.$extension->getId());
}
/**
* Get the path to an extension's published asset.
*
* @param Extension $extension
* @param string $path
* @return string
*/
public function getAsset(Extension $extension, $path)
{
return $this->paths->public.'/assets/extensions/'.$extension->getId().$path;
}
/**
* Runs the database migrations for the extension.
*
* @param Extension $extension
* @param string $direction
* @return void
*/
public function migrate(Extension $extension, $direction = 'up')
{
$this->container->bind(Builder::class, function ($container) {
return $container->make(ConnectionInterface::class)->getSchemaBuilder();
});
$extension->migrate($this->migrator, $direction);
}
/**
* Runs the database migrations to reset the database to its old state.
*
* @param Extension $extension
* @return array Notes from the migrator.
*/
public function migrateDown(Extension $extension)
{
return $this->migrate($extension, 'down');
}
/**
* The database migrator.
*
* @return Migrator
*/
public function getMigrator()
{
return $this->migrator;
}
/**
* Get only enabled extensions.
*
* @return array|Extension[]
*/
public function getEnabledExtensions()
{
$enabled = [];
$extensions = $this->getExtensions();
foreach ($this->getEnabled() as $id) {
if (isset($extensions[$id])) {
$enabled[$id] = $extensions[$id];
}
}
return $enabled;
}
/**
* Call on all enabled extensions to extend the Flarum application.
*
* @param Container $app
*/
public function extend(Container $app)
{
foreach ($this->getEnabledExtensions() as $extension) {
$extension->extend($app);
}
}
/**
* The id's of the enabled extensions.
*
* @return array
*/
public function getEnabled()
{
return json_decode($this->config->get('extensions_enabled'), true) ?? [];
}
/**
* Persist the currently enabled extensions.
*
* @param array $enabled
*/
protected function setEnabled(array $enabled)
{
$enabled = array_values(array_unique($enabled));
$this->config->set('extensions_enabled', json_encode($enabled));
}
/**
* Whether the extension is enabled.
*
* @param $extension
* @return bool
*/
public function isEnabled($extension)
{
$enabled = $this->getEnabledExtensions();
return isset($enabled[$extension]);
}
}