-
-
Notifications
You must be signed in to change notification settings - Fork 194
/
PluginManager.php
1073 lines (903 loc) · 32.2 KB
/
PluginManager.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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php namespace System\Classes;
use Db;
use App;
use Str;
use Log;
use File;
use Lang;
use View;
use Cache;
use Config;
use Schema;
use SystemException;
use FilesystemIterator;
use RecursiveIteratorIterator;
use RecursiveDirectoryIterator;
use System\Models\PluginVersion;
use Winter\Storm\Foundation\Application;
use Winter\Storm\Support\ClassLoader;
use Backend\Classes\NavigationManager;
/**
* Plugin manager
*
* @package winter\wn-system-module
* @author Alexey Bobkov, Samuel Georges
*/
class PluginManager
{
use \Winter\Storm\Support\Traits\Singleton;
//
// Disabled by system
//
public const DISABLED_MISSING = 'disabled-missing';
public const DISABLED_REPLACED = 'disabled-replaced';
public const DISABLED_REPLACEMENT_FAILED = 'disabled-replacement-failed';
public const DISABLED_MISSING_DEPENDENCIES = 'disabled-dependencies';
//
// Explicitly disabled for a reason
//
public const DISABLED_REQUEST = 'disabled-request';
public const DISABLED_BY_USER = 'disabled-user';
public const DISABLED_BY_CONFIG = 'disabled-config';
/**
* The application instance, since Plugins are an extension of a Service Provider
*/
protected Application $app;
/**
* @var PluginBase[] Container array used for storing plugin information objects.
*/
protected $plugins = [];
/**
* @var array Array of plugin codes that contain any flags currently associated with the plugin
*/
protected $pluginFlags = [];
/**
* @var PluginVersion[] Local cache of loaded PluginVersion records keyed by plugin code
*/
protected $pluginRecords = [];
/**
* @var array A map of normalized plugin identifiers [lowercase.identifier => Normalized.Identifier]
*/
protected $normalizedMap = [];
/**
* @var array A map of plugin identifiers with their replacements [Original.Plugin => Replacement.Plugin]
*/
protected $replacementMap = [];
/**
* @var array A map of plugins that are currently replaced [Original.Plugin => Replacement.Plugin]
*/
protected $activeReplacementMap = [];
/**
* @var bool Flag to indicate that all plugins have had the register() method called by registerAll() being called on this class.
*/
protected $registered = false;
/**
* @var bool Flag to indicate that all plugins have had the boot() method called by bootAll() being called on this class.
*/
protected $booted = false;
/**
* @var array Cache of registration method results.
*/
protected $registrationMethodCache = [];
/**
* @var bool Prevent all plugins from registering or booting
*/
public static $noInit = false;
/**
* Initializes the plugin manager
*/
protected function init(): void
{
$this->app = App::make('app');
// Load the plugins from the filesystem and sort them by dependencies
$this->loadPlugins();
// Loads the plugin flags (disabled & replacement states) from the cache
// regenerating them if required.
$this->loadPluginFlags();
// Register plugin replacements
$this->registerPluginReplacements();
}
/**
* Finds all available plugins and loads them in to the $this->plugins array.
*/
public function loadPlugins(): array
{
$this->plugins = [];
/**
* Locate all plugins and binds them to the container
*/
foreach ($this->getPluginNamespaces() as $namespace => $path) {
$this->loadPlugin($namespace, $path);
}
// Sort all the plugins by number of dependencies
$this->sortByDependencies();
return $this->getAllPlugins();
}
/**
* Loads a single plugin into the manager.
*
* @param string $namespace Eg: Acme\Blog
* @param string $path Eg: plugins_path().'/acme/blog';
*/
public function loadPlugin(string $namespace, string $path): ?PluginBase
{
$className = $namespace . '\Plugin';
$classPath = $path . '/Plugin.php';
$this->app->make(ClassLoader::class)->autoloadPackage($namespace, $path);
try {
// Autoloader failed?
if (!class_exists($className)) {
include_once $classPath;
}
// Not a valid plugin!
if (!class_exists($className)) {
return null;
}
$pluginObj = new $className($this->app);
} catch (\Throwable $e) {
Log::error('Plugin ' . $className . ' could not be instantiated.', [
'message' => $e->getMessage(),
'file' => $e->getFile(),
'line' => $e->getLine(),
'trace' => $e->getTraceAsString()
]);
return null;
}
$classId = $this->getIdentifier($pluginObj);
$lowerClassId = strtolower($classId);
$this->plugins[$lowerClassId] = $pluginObj;
$this->normalizedMap[$lowerClassId] = $classId;
$replaces = $pluginObj->getReplaces();
if ($replaces) {
foreach ($replaces as $replace) {
$lowerReplace = strtolower($replace);
$this->replacementMap[$lowerReplace] = $lowerClassId;
if (!isset($this->normalizedMap[$lowerReplace])) {
$this->normalizedMap[$lowerReplace] = $replace;
}
}
}
return $pluginObj;
}
/**
* Get the cache key for the current plugin manager state
*/
protected function getFlagCacheKey(): string
{
$loadedPlugins = array_keys($this->plugins);
$configDisabledPlugins = Config::get('cms.disablePlugins', []);
if (!is_array($configDisabledPlugins)) {
$configDisabledPlugins = [];
}
$plugins = $loadedPlugins + $configDisabledPlugins;
return 'system.pluginmanager.state.' . md5(implode('.', $plugins));
}
/**
* Loads the plugin flags (disabled & replacement states) from the cache
* regenerating them if required.
*/
public function loadPluginFlags(): void
{
// Cache the data for a month so that stale keys can be autocleaned if necessary
$data = Cache::remember($this->getFlagCacheKey(), now()->addMonths(1), function () {
// Check the config files & database for plugins to disable
$this->loadDisabled();
// Check plugin dependencies for plugins to disable
$this->loadDependencies();
// Check plugin replacments for plugins to disable
$this->detectPluginReplacements();
return [
$this->pluginFlags,
$this->replacementMap,
$this->activeReplacementMap,
];
});
list($this->pluginFlags, $this->replacementMap, $this->activeReplacementMap) = $data;
}
/**
* Reset the plugin flag cache
*/
public function clearFlagCache(): void
{
Cache::forget($this->getFlagCacheKey());
}
/**
* Runs the register() method on all plugins. Can only be called once.
*
* @param bool $force Defaults to false, if true will force the re-registration of all plugins. Use unregisterAll() instead.
*/
public function registerAll(bool $force = false): void
{
if ($this->registered && !$force) {
return;
}
foreach ($this->plugins as $pluginId => $plugin) {
$this->registerPlugin($plugin, $pluginId);
}
// Ensure that route attributes are properly loaded
// @see Illuminate\Foundation\Support\Providers\RouteServiceProvider->register()
// @fixes wintercms/winter#106
$this->app->booting(function () {
$this->app['router']->getRoutes()->refreshNameLookups();
$this->app['router']->getRoutes()->refreshActionLookups();
});
$this->registered = true;
}
/**
* Unregisters all plugins: the inverse of registerAll().
*/
public function unregisterAll(): void
{
$this->registered = false;
$this->plugins = [];
$this->replacementMap = [];
}
/**
* Registers a single plugin object.
*/
public function registerPlugin(PluginBase $plugin, ?string $pluginId = null): void
{
if (!$pluginId) {
$pluginId = $this->getIdentifier($plugin);
}
$pluginPath = $this->getPluginPath($plugin);
$pluginNamespace = strtolower($pluginId);
/*
* Register language namespaces
*/
$langPath = $pluginPath . '/lang';
if (File::isDirectory($langPath)) {
Lang::addNamespace($pluginNamespace, $langPath);
}
/**
* Prevent autoloaders from loading if plugin is disabled
*/
if ($this->isDisabled($pluginId)) {
return;
}
/*
* Register plugin class autoloaders
*/
$autoloadPath = $pluginPath . '/vendor/autoload.php';
if (File::isFile($autoloadPath)) {
ComposerManager::instance()->autoload($pluginPath . '/vendor');
}
/*
* Register configuration path
*/
$configPath = $pluginPath . '/config';
if (File::isDirectory($configPath)) {
Config::package($pluginNamespace, $configPath, $pluginNamespace);
}
/*
* Register views path
*/
$viewsPath = $pluginPath . '/views';
if (File::isDirectory($viewsPath)) {
View::addNamespace($pluginNamespace, $viewsPath);
}
/**
* Disable plugin registration for restricted pages, unless elevated
*/
if (self::$noInit && !$plugin->elevated) {
return;
}
/**
* Run the plugin's register() method
*/
$plugin->register();
/*
* Add init, if available
*/
$initFile = $pluginPath . '/init.php';
if (File::exists($initFile)) {
require $initFile;
}
/*
* Add routes, if available
*/
$routesFile = $pluginPath . '/routes.php';
if (File::exists($routesFile) && !$this->app->routesAreCached()) {
require $routesFile;
}
}
/**
* Runs the boot() method on all plugins. Can only be called once.
*
* @param bool $force Defaults to false, if true will force the re-booting of all plugins
*/
public function bootAll(bool $force = false): void
{
if ($this->booted && !$force) {
return;
}
foreach ($this->plugins as $plugin) {
$this->bootPlugin($plugin);
}
$this->booted = true;
}
/**
* Boots the provided plugin object.
*/
public function bootPlugin(PluginBase $plugin): void
{
if ((self::$noInit && !$plugin->elevated) || $this->isDisabled($plugin)) {
return;
}
$plugin->boot();
}
/**
* Returns the directory path to a plugin
*/
public function getPluginPath(PluginBase|string $plugin): ?string
{
return $this->findByIdentifier($plugin, true)?->getPluginPath();
}
/**
* Check if a plugin exists and is enabled.
*
* @param string $id Plugin identifier, eg: Namespace.PluginName
* @return bool
*/
public function exists(PluginBase|string $plugin): bool
{
return $this->findByIdentifier($plugin) && !$this->isDisabled($plugin);
}
/**
* Returns an array with all enabled plugins
*
* @return array [$code => $pluginObj]
*/
public function getPlugins(): array
{
$activePlugins = array_diff_key($this->plugins, $this->pluginFlags);
return array_combine(
array_map(
fn($code) => $this->normalizedMap[$code],
array_keys($activePlugins)
),
$activePlugins
);
}
/**
* Returns an array will all plugins detected on the filesystem
*
* @return array [$code => $pluginObj]
*/
public function getAllPlugins(): array
{
$plugins = [];
foreach ($this->plugins as $code => $plugin) {
$plugins[$this->normalizedMap[$code]] = $plugin;
}
return $plugins;
}
/**
* Returns a plugin registration class based on its namespace (Author\Plugin).
*/
public function findByNamespace(string $namespace): ?PluginBase
{
$identifier = $this->getIdentifier($namespace, true);
return $this->plugins[$identifier] ?? null;
}
/**
* Returns a plugin registration class based on its identifier (Author.Plugin).
*/
public function findByIdentifier(PluginBase|string $identifier, bool $ignoreReplacements = false): ?PluginBase
{
if ($identifier instanceof PluginBase) {
return $identifier;
}
$identifier = $this->getNormalizedIdentifier($identifier, true);
if (!$ignoreReplacements && isset($this->replacementMap[$identifier])) {
$identifier = $this->replacementMap[$identifier];
}
return $this->plugins[$identifier] ?? null;
}
/**
* Checks to see if a plugin has been registered.
*/
public function hasPlugin(PluginBase|string $plugin): bool
{
$normalized = $this->getNormalizedIdentifier($plugin, true);
return isset($this->plugins[$normalized]) || isset($this->replacementMap[$normalized]);
}
/**
* Returns a flat array of vendor plugin namespaces and their paths
* ['Author\Plugin' => 'plugins/author/plugin']
*/
public function getPluginNamespaces(): array
{
$classNames = [];
foreach ($this->getVendorAndPluginNames() as $vendorName => $vendorList) {
foreach ($vendorList as $pluginName => $pluginPath) {
$namespace = '\\' . $vendorName . '\\' . $pluginName;
$namespace = Str::normalizeClassName($namespace);
$classNames[$namespace] = $pluginPath;
}
}
return $classNames;
}
/**
* Returns a 2 dimensional array of vendors and their plugins.
* ['vendor' => ['author' => 'plugins/author/plugin']]
*/
public function getVendorAndPluginNames(): array
{
$plugins = [];
$dirPath = $this->app->pluginsPath();
if (!File::isDirectory($dirPath)) {
return $plugins;
}
$it = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($dirPath, FilesystemIterator::FOLLOW_SYMLINKS)
);
$it->setMaxDepth(2);
$it->rewind();
while ($it->valid()) {
if (($it->getDepth() > 1) && $it->isFile() && (strtolower($it->getFilename()) === "plugin.php")) {
$filePath = dirname($it->getPathname());
$pluginName = basename($filePath);
$vendorName = basename(dirname($filePath));
$plugins[$vendorName][$pluginName] = $filePath;
}
$it->next();
}
return $plugins;
}
/**
* Resolves a plugin identifier (Author.Plugin) from a plugin class name
* (Author\Plugin) or PluginBase instance.
*/
public function getIdentifier(PluginBase|string $plugin, bool $lower = false): string
{
$namespace = Str::normalizeClassName($plugin);
if (strpos($namespace, '\\') === null) {
return $namespace;
}
$parts = explode('\\', $namespace);
$slice = array_slice($parts, 1, 2);
$namespace = implode('.', $slice);
return $lower ? strtolower($namespace) : $namespace;
}
/**
* Resolves a plugin namespace (Author\Plugin) from a plugin class name
* (Author\Plugin\Classes\Example), identifier (Author.Plugin), or
* PluginBase instance.
*/
public function getNamespace(PluginBase|string $plugin): string
{
if (is_string($plugin) && strpos($plugin, '.') !== null) {
$parts = explode('.', $plugin);
$slice = array_slice($parts, 0, 2);
$namespace = implode('\\', $slice);
return Str::normalizeClassName($namespace);
}
return Str::normalizeClassName($plugin);
}
/**
* Normalizes the provided plugin identifier (author.plugin) and resolves
* it case-insensitively to the normalized identifier (Author.Plugin)
* Returns the provided identifier if a match isn't found
*/
public function normalizeIdentifier(string $code): string
{
return $this->getNormalizedIdentifier($code);
}
/**
* Returns the normalized identifier (i.e. Winter.Blog) from the provided
* string or PluginBase instance.
*/
public function getNormalizedIdentifier(PluginBase|string $plugin, bool $lower = false): string
{
$code = $this->getIdentifier($plugin);
$identifier = $this->normalizedMap[strtolower($code)] ?? $code;
return $lower ? strtolower($identifier) : $identifier;
}
/**
* Spins over every plugin object and collects the results of the provided
* method call. Results are cached in memory.
*/
public function getRegistrationMethodValues(string $methodName): array
{
if (isset($this->registrationMethodCache[$methodName])) {
return $this->registrationMethodCache[$methodName];
}
$results = [];
$plugins = $this->getPlugins();
foreach ($plugins as $id => $plugin) {
if (!is_callable([$plugin, $methodName])) {
continue;
}
$results[$id] = $plugin->{$methodName}();
}
return $this->registrationMethodCache[$methodName] = $results;
}
//
// State Management (Disable, Enable, Freeze, Unfreeze)
//
public function getPluginFlags(PluginBase|string $plugin): array
{
$code = $this->getNormalizedIdentifier($plugin, true);
return $this->pluginFlags[$code] ?? [];
}
/**
* Sets the provided flag on the provided plugin
*/
protected function flagPlugin(PluginBase|string $plugin, string $flag): void
{
$code = $this->getNormalizedIdentifier($plugin, true);
$this->pluginFlags[$code][$flag] = true;
}
/**
* Removes the provided flag from the provided plugin
*/
protected function unflagPlugin(PluginBase|string $plugin, string $flag): void
{
// Remove the provided flag from the provided plugin
$code = $this->getNormalizedIdentifier($plugin, true);
unset($this->pluginFlags[$code][$flag]);
// Remove the plugin from the pluginFlags property if it has no flags
if (empty($this->pluginFlags[$code])) {
unset($this->pluginFlags[$code]);
}
}
/**
* Loads all disabled plugins from the cached JSON file.
*/
protected function loadDisabled(): void
{
// Check the config files for disabled plugins
if (($configDisabled = Config::get('cms.disablePlugins')) && is_array($configDisabled)) {
foreach ($configDisabled as $disabled) {
$this->flagPlugin($disabled, static::DISABLED_BY_CONFIG);
}
}
// Check the database for disabled plugins
if (
$this->app->hasDatabaseTable('system_plugin_versions')
) {
$userDisabled = Db::table('system_plugin_versions')->where('is_disabled', 1)->lists('code') ?? [];
foreach ($userDisabled as $code) {
$this->flagPlugin($code, static::DISABLED_BY_USER);
}
}
}
/**
* Determines if a plugin is disabled by looking at the meta information
* or the application configuration.
*/
public function isDisabled(PluginBase|string $plugin): bool
{
$code = $this->getNormalizedIdentifier($plugin, true);
// @TODO: Limit this to only disabled flags if we add more than disabled flags
return !empty($this->pluginFlags[$code]);
}
/**
* Returns the plugin replacements defined in $this->replacementMap
*/
public function getReplacementMap(): array
{
return $this->replacementMap;
}
/**
* Returns the actively replaced plugins defined in $this->activeReplacementMap
*/
public function getActiveReplacementMap(PluginBase|string $plugin = null): array|string|null
{
if ($plugin) {
return $this->normalizedMap[
$this->activeReplacementMap[$this->getNormalizedIdentifier($plugin, true)] ?? null
] ?? null;
}
$map = [];
foreach ($this->activeReplacementMap as $key => $value) {
$map[$this->normalizedMap[$key]] = $this->normalizedMap[$value];
}
return $map;
}
/**
* Evaluates the replacement map to determine which replacements can actually
* take effect
*/
protected function detectPluginReplacements(): void
{
if (empty($this->replacementMap)) {
return;
}
foreach ($this->replacementMap as $target => $replacement) {
// If the replaced plugin isn't present then assume it can be replaced
if (!isset($this->plugins[$target])) {
continue;
}
// Only allow one of the replaced plugin or the replacing plugin to exist
// at once depending on whether the version constraints are met or not
if (
$this->plugins[$replacement]->canReplacePlugin(
$this->normalizeIdentifier($target),
$this->plugins[$target]->getPluginVersion()
)
) {
// Set the plugin flags to disable the target plugin
$this->flagPlugin($target, static::DISABLED_REPLACED);
$this->unflagPlugin($replacement, static::DISABLED_REPLACEMENT_FAILED);
// Register this plugin as actively replaced (i.e. both are present, replaced are disabled)
$this->activeReplacementMap[$target] = $replacement;
} else {
// Set the plugin flags to disable the replacement plugin
$this->flagPlugin($replacement, static::DISABLED_REPLACEMENT_FAILED);
$this->unflagPlugin($target, static::DISABLED_REPLACED);
// Remove the replacement alias to prevent redirection to a disabled plugin
unset($this->replacementMap[$target]);
}
}
}
/**
* Executes the plugin replacements defined in the activeReplacementMap property
*/
protected function registerPluginReplacements(): void
{
foreach ($this->replacementMap as $target => $replacement) {
list($target, $replacement) = array_map(
fn($plugin) => $this->normalizeIdentifier($plugin),
[$target, $replacement]
);
// Alias the replaced plugin to the replacing plugin
$this->aliasPluginAs($replacement, $target);
// Register namespace aliases for any replaced plugins
$this->app->make(ClassLoader::class)->addNamespaceAliases([
// class_alias() expects order to be $real, $alias
$this->getNamespace($replacement) => $this->getNamespace($target),
]);
}
}
/**
* Registers namespace aliasing for multiple subsystems
*/
protected function aliasPluginAs(string $namespace, string $alias): void
{
Lang::registerNamespaceAlias($namespace, $alias);
Config::registerNamespaceAlias($namespace, $alias);
Config::registerPackageFallback($namespace, $alias);
SettingsManager::lazyRegisterOwnerAlias($namespace, $alias);
NavigationManager::lazyRegisterOwnerAlias($namespace, $alias);
}
/**
* Get the PluginVersion record for the provided plugin
*
* @throws InvalidArgumentException if unable to find the requested plugin record in the database
*/
protected function getPluginRecord(PluginBase|string $plugin): PluginVersion
{
$plugin = $this->getNormalizedIdentifier($plugin);
if (isset($this->pluginRecords[$plugin])) {
return $this->pluginRecords[$plugin];
}
$record = PluginVersion::where('code', $plugin)->first();
if (!$record) {
throw new \InvalidArgumentException("$plugin was not found in the database.");
}
return $this->pluginRecords[$plugin] = $record;
}
/**
* Flags the provided plugin as "frozen" (updates cannot be downloaded / installed)
*/
public function freezePlugin(PluginBase|string $plugin): void
{
$record = $this->getPluginRecord($plugin);
$record->is_frozen = true;
$record->save();
}
/**
* "Unfreezes" the provided plugin, allowing for updates to be performed
*/
public function unfreezePlugin(PluginBase|string $plugin): void
{
$record = $this->getPluginRecord($plugin);
$record->is_frozen = false;
$record->save();
}
/**
* Disables the provided plugin using the provided flag (defaults to static::DISABLED_BY_USER)
*/
public function disablePlugin(PluginBase|string $plugin, string|bool $flag = self::DISABLED_BY_USER): bool
{
// $flag used to be (bool) $byUser
if ($flag === true) {
$flag = static::DISABLED_BY_USER;
}
// Flag the plugin as disabled
$this->flagPlugin($plugin, $flag);
// Updates the database record for the plugin if required
if ($flag === static::DISABLED_BY_USER) {
$record = $this->getPluginRecord($plugin);
$record->is_disabled = true;
$record->save();
// Clear the cache so that the next request will regenerate the active flags
$this->clearFlagCache();
}
// Clear the registration values cache
$this->registrationMethodCache = [];
return true;
}
/**
* Enables the provided plugin using the provided flag (defaults to static::DISABLED_BY_USER)
*/
public function enablePlugin(PluginBase|string $plugin, $flag = self::DISABLED_BY_USER): bool
{
// $flag used to be (bool) $byUser
if ($flag === true) {
$flag = static::DISABLED_BY_USER;
}
// Unflag the plugin as disabled
$this->unflagPlugin($plugin, $flag);
// Updates the database record for the plugin if required
if ($flag === static::DISABLED_BY_USER) {
$record = $this->getPluginRecord($plugin);
$record->is_disabled = false;
$record->save();
// Clear the cache so that the next request will regenerate the active flags
$this->clearFlagCache();
}
// Clear the registration values cache
$this->registrationMethodCache = [];
return true;
}
//
// Dependencies
//
/**
* Returns the plugin identifiers that are required by the supplied plugin.
*/
public function getDependencies(PluginBase|string $plugin): array
{
if (is_string($plugin) && (!$plugin = $this->findByIdentifier($plugin))) {
return [];
}
if (!isset($plugin->require) || !$plugin->require) {
return [];
}
return array_map(function ($require) {
return $this->replacementMap[$require] ?? $require;
}, is_array($plugin->require) ? $plugin->require : [$plugin->require]);
}
/**
* Scans the system plugins to locate any dependencies that are not currently
* installed. Returns an array of missing plugin codes keyed by the plugin that requires them.
*
* ['Author.Plugin' => ['Required.Plugin1', 'Required.Plugin2']
*
* PluginManager::instance()->findMissingDependencies();
*
*/
public function findMissingDependencies(): array
{
$missing = [];
foreach ($this->plugins as $id => $plugin) {
if (!$required = $this->getDependencies($plugin)) {
continue;
}
foreach ($required as $require) {
if ($this->hasPlugin($require)) {
continue;
}
if (!in_array($require, $missing)) {
$missing[$this->getIdentifier($plugin)][] = $require;
}
}
}
return $missing;
}
/**
* Checks plugin dependencies and flags plugins with missing dependencies as disabled
*/
protected function loadDependencies(): void
{
foreach ($this->plugins as $id => $plugin) {
if (!$plugin->checkDependencies($this)) {
$this->flagPlugin($id, static::DISABLED_MISSING_DEPENDENCIES);
} else {
$this->unflagPlugin($id, static::DISABLED_MISSING_DEPENDENCIES);
}
}
}
/**
* Sorts a collection of plugins, in the order that they should be actioned,
* according to their given dependencies. Least dependent come first.
*
* @return array Array of sorted plugin identifiers and instantiated classes ['Author.Plugin' => PluginBase]
* @throws SystemException If a possible circular dependency is detected
*/
protected function sortByDependencies(): array
{
ksort($this->plugins);
/*
* Canvas the dependency tree
*/
$checklist = $this->plugins;
$result = [];
$loopCount = 0;
while (count($checklist)) {
if (++$loopCount > 2048) {
throw new SystemException('Too much recursion! Check for circular dependencies in your plugins.');
}
foreach ($checklist as $code => $plugin) {
/*
* Get dependencies and remove any aliens, replacing any dependencies which have been superceded
* by another plugin.
*/
$depends = $this->getDependencies($plugin);
$depends = array_map(function ($depend) {
$depend = $this->getNormalizedIdentifier($depend, true);
if (isset($this->replacementMap[$depend])) {
return $this->replacementMap[$depend];
}
return $depend;
}, $depends);
$depends = array_filter($depends, function ($pluginCode) {
return isset($this->plugins[$pluginCode]);
});
/*
* No dependencies