| 1 |
<?php |
| 2 |
|
| 3 |
namespace ipl\Stdlib\Loader; |
| 4 |
|
| 5 |
use ipl\Stdlib\Contract\PluginLoader; |
| 6 |
|
| 7 |
/** |
| 8 |
* Plugin loader that makes use of registered PHP autoloaders |
| 9 |
*/ |
| 10 |
class AutoloadingPluginLoader implements PluginLoader |
| 11 |
{ |
| 12 |
/** @var string Namespace of the plugins */ |
| 13 |
protected string $namespace; |
| 14 |
|
| 15 |
/** @var ?string Class name postfix */ |
| 16 |
protected ?string $postfix = null; |
| 17 |
|
| 18 |
/** |
| 19 |
* Create a new autoloading plugin loader |
| 20 |
* |
| 21 |
* @param string $namespace Namespace of the plugins |
| 22 |
* @param ?string $postfix Class name postfix |
| 23 |
*/ |
| 24 |
public function __construct(string $namespace, ?string $postfix = null) |
| 25 |
{ |
| 26 |
$this->namespace = $namespace; |
| 27 |
$this->postfix = $postfix; |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Get the FQN of a plugin |
| 32 |
* |
| 33 |
* @param string $name Name of the plugin |
| 34 |
* |
| 35 |
* @return string |
| 36 |
*/ |
| 37 |
protected function getFqn(string $name): string |
| 38 |
{ |
| 39 |
return $this->namespace . '\\' . ucfirst($name) . $this->postfix; |
| 40 |
} |
| 41 |
|
| 42 |
public function load(string $name): false|string |
| 43 |
{ |
| 44 |
$class = $this->getFqn($name); |
| 45 |
|
| 46 |
if (! class_exists($class)) { |
| 47 |
return false; |
| 48 |
} |
| 49 |
|
| 50 |
return $class; |
| 51 |
} |
| 52 |
} |