-
-
Notifications
You must be signed in to change notification settings - Fork 829
/
ExtensionManager.php
516 lines (423 loc) · 16.8 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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
<?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\Extension\Exception\CircularDependenciesException;
use Flarum\Foundation\MaintenanceMode;
use Flarum\Foundation\Paths;
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Filesystem\Cloud;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Database\Schema\Builder;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
class ExtensionManager
{
protected ?Collection $extensions = null;
public function __construct(
protected SettingsRepositoryInterface $config,
protected Paths $paths,
protected Container $container,
protected Migrator $migrator,
protected Dispatcher $dispatcher,
protected Filesystem $filesystem,
protected MaintenanceMode $maintenance,
) {
}
public function getExtensions(): Collection
{
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;
// We calculate and store a set of composer package names for all installed Flarum extensions,
// so we know what is and isn't a flarum extension in `calculateDependencies`.
// Using keys of an associative array allows us to do these checks in constant time.
$installedSet = [];
$composerJsonConfs = [];
foreach ($installed as $package) {
$name = Arr::get($package, 'name');
if (empty($name)) {
continue;
}
$packagePath = isset($package['install-path'])
? $this->paths->vendor.'/composer/'.$package['install-path']
: $this->paths->vendor.'/'.$name;
if (Arr::get($package, 'type') === 'flarum-extension') {
$composerJsonConfs[$packagePath] = $package;
}
if ($subExtConfs = $this->subExtensionConfsFromJson($package, $packagePath)) {
$composerJsonConfs = array_merge($composerJsonConfs, $subExtConfs);
}
}
foreach ($composerJsonConfs as $path => $package) {
$installedSet[Arr::get($package, 'name')] = true;
$extension = $this->extensionFromJson($package, $path);
$extensions->put($extension->getId(), $extension);
}
/** @var Extension $extension */
foreach ($extensions as $extension) {
$extension->calculateDependencies($installedSet);
}
$needsReset = false;
$enabledExtensions = [];
foreach ($this->getEnabled() as $enabledKey) {
$extension = $extensions->get($enabledKey);
if (is_null($extension)) {
$needsReset = true;
} else { // @phpstan-ignore-line
$enabledExtensions[] = $extension;
}
}
if ($needsReset) {
$this->setEnabledExtensions($enabledExtensions);
}
$this->extensions = $extensions->sortBy(function ($extension, $name) {
return $extension->getTitle();
});
}
return $this->extensions;
}
public function getExtensionsById(array $ids): Collection
{
return $this->getExtensions()->filter(function (Extension $extension) use ($ids) {
return in_array($extension->getId(), $ids);
});
}
public function getExtension(string $name): ?Extension
{
return $this->getExtensions()->get($name);
}
/**
* @internal
*/
public function enable(string $name): void
{
if ($this->isEnabled($name)) {
return;
}
$extension = $this->getExtension($name);
$missingDependencies = [];
$enabledIds = $this->getEnabled();
foreach ($extension->getExtensionDependencyIds() as $dependencyId) {
if (! in_array($dependencyId, $enabledIds)) {
$missingDependencies[] = $this->getExtension($dependencyId);
}
}
if (! empty($missingDependencies)) {
throw new Exception\MissingDependenciesException($extension, $missingDependencies);
}
$this->dispatcher->dispatch(new Enabling($extension));
$this->migrate($extension);
$this->publishAssets($extension);
$enabledExtensions = $this->getEnabledExtensions();
$enabledExtensions[] = $extension;
$this->setEnabledExtensions($enabledExtensions);
$extension->enable($this->container);
$this->dispatcher->dispatch(new Enabled($extension));
}
/**
* @internal
*/
public function disable(string $name): void
{
$extension = $this->getExtension($name);
$enabledExtensions = $this->getEnabledExtensions();
if (($k = array_search($extension, $enabledExtensions)) === false) {
return;
}
$dependentExtensions = [];
foreach ($enabledExtensions as $possibleDependent) {
if (in_array($extension->getId(), $possibleDependent->getExtensionDependencyIds())) {
$dependentExtensions[] = $possibleDependent;
}
}
if (! empty($dependentExtensions)) {
throw new Exception\DependentExtensionsException($extension, $dependentExtensions);
}
$this->dispatcher->dispatch(new Disabling($extension));
unset($enabledExtensions[$k]);
$this->setEnabledExtensions($enabledExtensions);
$extension->disable($this->container);
$this->dispatcher->dispatch(new Disabled($extension));
}
/**
* @internal
*/
public function uninstall(string $name): void
{
$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.
*/
protected function publishAssets(Extension $extension): void
{
$extension->copyAssetsTo($this->getAssetsFilesystem());
}
/**
* Delete an extension's assets from public view.
*/
protected function unpublishAssets(Extension $extension): void
{
$this->getAssetsFilesystem()->deleteDirectory('extensions/'.$extension->getId());
}
/**
* Get the path to an extension's published asset.
*/
public function getAsset(Extension $extension, string $path): string
{
return $this->getAssetsFilesystem()->url($extension->getId()."/$path");
}
/**
* Get an instance of the `assets` filesystem.
* This is resolved dynamically because Flarum's filesystem configuration
* might not be booted yet when the ExtensionManager singleton initializes.
*/
protected function getAssetsFilesystem(): Cloud
{
return resolve('filesystem')->disk('flarum-assets');
}
/**
* Runs the database migrations for the extension.
*
* @internal
*/
public function migrate(Extension $extension, string $direction = 'up'): ?int
{
$this->container->bind(Builder::class, function ($container) {
return $container->make(ConnectionInterface::class)->getSchemaBuilder();
});
return $extension->migrate($this->migrator, $direction);
}
/**
* Runs the database migrations to reset the database to its old state.
*
* @internal
*/
public function migrateDown(Extension $extension): void
{
$this->migrate($extension, 'down');
}
/**
* The database migrator.
*/
public function getMigrator(): Migrator
{
return $this->migrator;
}
/**
* @return Extension[]
*/
public function getEnabledExtensions(): array
{
$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.
*/
public function extend(Container $container): void
{
$extensions = $this->getEnabledExtensions();
if ($this->maintenance->inSafeMode()) {
$safeModeExtensions = $this->maintenance->safeModeExtensions();
$extensions = array_filter($extensions, function (Extension $extension) use ($safeModeExtensions) {
return in_array($extension->getId(), $safeModeExtensions, true);
});
$extensions = $this->sortDependencies($extensions);
}
foreach ($extensions as $extension) {
$extension->extend($container);
}
}
/**
* The id's of the enabled extensions.
*
* @return string[]
*/
public function getEnabled(): array
{
return json_decode($this->config->get('extensions_enabled'), true) ?? [];
}
/**
* Persist the currently enabled extensions.
*
* @param Extension[] $enabledExtensions
* @throws CircularDependenciesException
*/
protected function setEnabledExtensions(array $enabledExtensions): void
{
$this->config->set('extensions_enabled', json_encode(array_map(function (Extension $extension) {
return $extension->getId();
}, $this->sortDependencies($enabledExtensions))));
}
/**
* Apply a topological sort to the extensions to ensure that they are in the correct order.
*
* @param Extension[] $extensions
* @return Extension[]
* @throws CircularDependenciesException
*/
public function sortDependencies(array $extensions): array
{
$resolved = static::resolveExtensionOrder($extensions);
if (! empty($resolved['circularDependencies'])) {
throw new Exception\CircularDependenciesException(
$this->getExtensionsById($resolved['circularDependencies'])->values()->all()
);
}
return $resolved['valid'];
}
public function isEnabled(string $extension): bool
{
$enabled = $this->getEnabledExtensions();
return isset($enabled[$extension]);
}
/**
* Returns the titles of the extensions passed.
*
* @param Extension[] $extensions
* @return string[]
*/
public static function pluckTitles(array $extensions): array
{
return array_map(function (Extension $extension) {
return $extension->getTitle();
}, $extensions);
}
/**
* Sort a list of extensions so that they are properly resolved in respect to order.
* Effectively just topological sorting.
*
* @param Extension[] $extensionList
*
* @return array{valid: Extension[], missingDependencies: array<string, string[]>, circularDependencies: string[]}
* 'valid' points to an ordered array of \Flarum\Extension\Extension
* 'missingDependencies' points to an associative array of extensions that could not be resolved due
* to missing dependencies, in the format extension id => array of missing dependency IDs.
* 'circularDependencies' points to an array of extensions ids of extensions
* that cannot be processed due to circular dependencies
*
* @internal
*/
public static function resolveExtensionOrder(array $extensionList): array
{
$extensionIdMapping = []; // Used for caching so we don't rerun ->getExtensions every time.
// This is an implementation of Kahn's Algorithm (https://dl.acm.org/doi/10.1145/368996.369025)
$extensionGraph = [];
$output = [];
$missingDependencies = []; // Extensions are invalid if they are missing dependencies, or have circular dependencies.
$circularDependencies = [];
$pendingQueue = [];
$inDegreeCount = []; // How many extensions are dependent on a given extension?
// Sort alphabetically by ID. This guarantees that any set of extensions will always be sorted the same way.
// This makes boot order deterministic, and independent of enabled order.
$extensionList = Arr::sort($extensionList, function ($ext) {
return $ext->getId();
});
foreach ($extensionList as $extension) {
$extensionIdMapping[$extension->getId()] = $extension;
}
/** @var Extension $extension */
foreach ($extensionList as $extension) {
$optionalDependencies = array_filter($extension->getOptionalDependencyIds(), function ($id) use ($extensionIdMapping) {
return array_key_exists($id, $extensionIdMapping);
});
$extensionGraph[$extension->getId()] = array_merge($extension->getExtensionDependencyIds(), $optionalDependencies);
foreach ($extensionGraph[$extension->getId()] as $dependency) {
$inDegreeCount[$dependency] = array_key_exists($dependency, $inDegreeCount) ? $inDegreeCount[$dependency] + 1 : 1;
}
}
foreach ($extensionList as $extension) {
if (! array_key_exists($extension->getId(), $inDegreeCount)) {
$inDegreeCount[$extension->getId()] = 0;
$pendingQueue[] = $extension->getId();
}
}
while (! empty($pendingQueue)) {
$activeNode = array_shift($pendingQueue);
$output[] = $activeNode;
foreach ($extensionGraph[$activeNode] as $dependency) {
$inDegreeCount[$dependency] -= 1;
if ($inDegreeCount[$dependency] === 0) {
if (! array_key_exists($dependency, $extensionGraph)) {
// Missing Dependency
$missingDependencies[$activeNode] = array_merge(
Arr::get($missingDependencies, $activeNode, []),
[$dependency]
);
} else {
$pendingQueue[] = $dependency;
}
}
}
}
$validOutput = array_filter($output, function ($extension) use ($missingDependencies) {
return ! array_key_exists($extension, $missingDependencies);
});
$validExtensions = array_reverse(array_map(function ($extensionId) use ($extensionIdMapping) {
return $extensionIdMapping[$extensionId];
}, $validOutput)); // Reversed as required by Kahn's algorithm.
foreach ($inDegreeCount as $id => $count) {
if ($count != 0) {
$circularDependencies[] = $id;
}
}
return [
'valid' => $validExtensions,
'missingDependencies' => $missingDependencies,
'circularDependencies' => $circularDependencies
];
}
protected function extensionFromJson(array $package, string $path): Extension
{
// 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', '0.0'));
return $extension;
}
protected function subExtensionConfsFromJson(array $package, string $packagePath): ?array
{
if (! ($subExtPaths = Arr::get($package, 'extra.flarum-subextensions', []))) {
return null;
}
$subExtConfs = [];
foreach ($subExtPaths as $subExtPath) {
$subPackagePath = "$packagePath/$subExtPath";
$conf = json_decode($this->filesystem->get("$subPackagePath/composer.json"), true);
if (Arr::get($conf, 'type') === 'flarum-extension') {
$subExtConfs[$subPackagePath] = $conf;
}
}
return $subExtConfs;
}
}