Commit 587938d2 authored by Yassine Doghri's avatar Yassine Doghri
Browse files

feat(plugins): load plugins using file locator service

parent 7253e13a
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -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');
+41 −5
Original line number Diff line number Diff line
@@ -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;
        }
    }
}