| 1 |
<?php |
| 2 |
|
| 3 |
namespace ipl\Stdlib; |
| 4 |
|
| 5 |
use ipl\Stdlib\Contract\PluginLoader; |
| 6 |
use ipl\Stdlib\Loader\AutoloadingPluginLoader; |
| 7 |
|
| 8 |
/** |
| 9 |
* Register plugin loaders by type and resolve plugin class names |
| 10 |
*/ |
| 11 |
trait Plugins |
| 12 |
{ |
| 13 |
/** @var array<string, PluginLoader[]> Registered plugin loaders by type */ |
| 14 |
protected array $pluginLoaders = []; |
| 15 |
|
| 16 |
/** |
| 17 |
* Factory for plugin loaders |
| 18 |
* |
| 19 |
* @param PluginLoader|string $loaderOrNamespace |
| 20 |
* @param ?string $postfix |
| 21 |
* |
| 22 |
* @return PluginLoader |
| 23 |
*/ |
| 24 |
public static function wantPluginLoader( |
| 25 |
PluginLoader|string $loaderOrNamespace, |
| 26 |
?string $postfix = null |
| 27 |
): PluginLoader { |
| 28 |
return $loaderOrNamespace instanceof PluginLoader |
| 29 |
? $loaderOrNamespace |
| 30 |
: new AutoloadingPluginLoader($loaderOrNamespace, $postfix); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Get whether a plugin loader for the given type exists |
| 35 |
* |
| 36 |
* @param string $type |
| 37 |
* |
| 38 |
* @return bool |
| 39 |
*/ |
| 40 |
public function hasPluginLoader(string $type): bool |
| 41 |
{ |
| 42 |
return isset($this->pluginLoaders[$type]); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Add a plugin loader for the given type |
| 47 |
* |
| 48 |
* @param string $type |
| 49 |
* @param PluginLoader|string $loaderOrNamespace |
| 50 |
* @param ?string $postfix |
| 51 |
* |
| 52 |
* @return $this |
| 53 |
*/ |
| 54 |
public function addPluginLoader( |
| 55 |
string $type, |
| 56 |
PluginLoader|string $loaderOrNamespace, |
| 57 |
?string $postfix = null |
| 58 |
): static { |
| 59 |
$loader = static::wantPluginLoader($loaderOrNamespace, $postfix); |
| 60 |
|
| 61 |
if (! isset($this->pluginLoaders[$type])) { |
| 62 |
$this->pluginLoaders[$type] = []; |
| 63 |
} |
| 64 |
|
| 65 |
array_unshift($this->pluginLoaders[$type], $loader); |
| 66 |
|
| 67 |
return $this; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Load the class file of the given plugin |
| 72 |
* |
| 73 |
* @param string $type |
| 74 |
* @param string $name |
| 75 |
* |
| 76 |
* @return string|false |
| 77 |
*/ |
| 78 |
public function loadPlugin(string $type, string $name): string|false |
| 79 |
{ |
| 80 |
if ($this->hasPluginLoader($type)) { |
| 81 |
foreach ($this->pluginLoaders[$type] as $loader) { |
| 82 |
$class = $loader->load($name); |
| 83 |
if ($class) { |
| 84 |
return $class; |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
return false; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Add a default plugin loader for the given type |
| 94 |
* |
| 95 |
* Default loaders are appended after any loaders added via {@see addPluginLoader()}. |
| 96 |
* |
| 97 |
* @param string $type |
| 98 |
* @param PluginLoader|string $loaderOrNamespace |
| 99 |
* @param string $postfix |
| 100 |
* |
| 101 |
* @return $this |
| 102 |
*/ |
| 103 |
protected function addDefaultPluginLoader( |
| 104 |
string $type, |
| 105 |
PluginLoader|string $loaderOrNamespace, |
| 106 |
string $postfix |
| 107 |
): static { |
| 108 |
$this->pluginLoaders[$type][] = static::wantPluginLoader($loaderOrNamespace, $postfix); |
| 109 |
|
| 110 |
return $this; |
| 111 |
} |
| 112 |
} |