2024-04-28 17:14:45 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Modules\Plugins;
|
|
|
|
|
|
|
|
class Plugins
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var array<PluginInterface>
|
|
|
|
*/
|
2024-04-29 16:03:00 +00:00
|
|
|
protected static array $plugins = [];
|
2024-04-28 17:14:45 +00:00
|
|
|
|
2024-04-29 16:03:00 +00:00
|
|
|
public function __construct()
|
2024-04-28 17:14:45 +00:00
|
|
|
{
|
2024-04-29 16:03:00 +00:00
|
|
|
$this->registerPlugins();
|
2024-04-28 17:14:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array<PluginInterface>
|
|
|
|
*/
|
2024-04-29 16:03:00 +00:00
|
|
|
public function getPlugins(): array
|
2024-04-28 17:14:45 +00:00
|
|
|
{
|
2024-04-29 16:03:00 +00:00
|
|
|
return $this->plugins;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array<mixed> $parameters
|
|
|
|
*/
|
|
|
|
public function runHook(string $name, array $parameters): void
|
|
|
|
{
|
|
|
|
dd(static::$plugins);
|
|
|
|
// only run active plugins' hooks
|
|
|
|
foreach (static::$plugins as $plugin) {
|
|
|
|
$plugin->{$name}(...$parameters);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function registerPlugins(): void
|
|
|
|
{
|
|
|
|
$locator = service('locator');
|
|
|
|
$pluginsFiles = $locator->search('HelloWorld/Plugin.php');
|
|
|
|
|
|
|
|
// dd($pluginsFiles);
|
|
|
|
|
|
|
|
foreach ($pluginsFiles as $file) {
|
|
|
|
$className = $locator->findQualifiedNameFromPath($file);
|
|
|
|
|
|
|
|
dd($file);
|
|
|
|
if ($className === false) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$plugin = new $className();
|
|
|
|
if (! $plugin instanceof PluginInterface) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
static::$plugins[] = $plugin;
|
|
|
|
}
|
2024-04-28 17:14:45 +00:00
|
|
|
}
|
|
|
|
}
|