feat(plugins): load plugins using file locator service

This commit is contained in:
Yassine Doghri 2024-04-29 16:03:00 +00:00
parent 5293752bb5
commit 2f517fde47
2 changed files with 45 additions and 5 deletions

View File

@ -31,6 +31,8 @@ if (! function_exists('get_rss_feed')) {
Subscription $subscription = null,
string $token = null
): string {
$plugins = service('plugins');
$episodes = $podcast->episodes;
$itunesNamespace = 'http://www.itunes.com/dtds/podcast-1.0.dtd';
@ -69,6 +71,8 @@ if (! function_exists('get_rss_feed')) {
$channel->addChild('generator', 'Castopod - https://castopod.org/');
$channel->addChild('docs', 'https://cyber.harvard.edu/rss/rss.html');
$plugins->runHook('setChannelTag', [$podcast, $channel]);
if ($podcast->guid === '') {
// FIXME: guid shouldn't be empty here as it should be filled upon Podcast creation
$uuid = service('uuid');

View File

@ -9,18 +9,54 @@ class Plugins
/**
* @var array<PluginInterface>
*/
protected array $installed = [];
protected static array $plugins = [];
public function registerPlugin(PluginInterface $plugin): void
public function __construct()
{
$this->installed[] = $plugin;
$this->registerPlugins();
}
/**
* @return array<PluginInterface>
*/
public function getInstalled(): array
public function getPlugins(): array
{
return $this->installed;
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;
}
}
}