2024-04-28 17:14:45 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Modules\Plugins;
|
|
|
|
|
2024-05-01 14:48:05 +00:00
|
|
|
use App\Entities\Episode;
|
|
|
|
use App\Entities\Podcast;
|
|
|
|
use App\Libraries\SimpleRSSElement;
|
|
|
|
|
|
|
|
/**
|
2024-05-01 15:41:13 +00:00
|
|
|
* @method void channelTag(Podcast $podcast, SimpleRSSElement $channel)
|
|
|
|
* @method void itemTag(Episode $episode, SimpleRSSElement $item)
|
|
|
|
* @method string siteHead()
|
2024-05-01 14:48:05 +00:00
|
|
|
*/
|
2024-04-28 17:14:45 +00:00
|
|
|
class Plugins
|
|
|
|
{
|
2024-05-01 14:48:05 +00:00
|
|
|
protected const API_VERSION = '1.0';
|
|
|
|
|
2024-04-28 17:14:45 +00:00
|
|
|
/**
|
2024-05-01 14:48:05 +00:00
|
|
|
* @var list<string>
|
|
|
|
*/
|
2024-05-01 15:41:13 +00:00
|
|
|
protected const HOOKS = ['channelTag', 'itemTag', 'siteHead'];
|
2024-05-01 14:48:05 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array<BasePlugin>
|
2024-04-28 17:14:45 +00:00
|
|
|
*/
|
2024-04-29 16:03:00 +00:00
|
|
|
protected static array $plugins = [];
|
2024-04-28 17:14:45 +00:00
|
|
|
|
2024-05-01 14:48:05 +00:00
|
|
|
protected static int $installedCount = 0;
|
|
|
|
|
2024-04-29 16:03:00 +00:00
|
|
|
public function __construct()
|
2024-04-28 17:14:45 +00:00
|
|
|
{
|
2024-05-01 14:48:05 +00:00
|
|
|
helper('plugins');
|
|
|
|
|
2024-04-29 16:03:00 +00:00
|
|
|
$this->registerPlugins();
|
2024-04-28 17:14:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-05-01 14:48:05 +00:00
|
|
|
* @param value-of<static::HOOKS> $name
|
|
|
|
* @param array<mixed> $arguments
|
|
|
|
*/
|
|
|
|
public function __call(string $name, array $arguments): void
|
|
|
|
{
|
|
|
|
if (! in_array($name, static::HOOKS, true)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->runHook($name, $arguments);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array<BasePlugin>
|
2024-04-28 17:14:45 +00:00
|
|
|
*/
|
2024-05-01 14:48:05 +00:00
|
|
|
public function getPlugins(int $page, int $perPage): array
|
2024-04-28 17:14:45 +00:00
|
|
|
{
|
2024-05-01 14:48:05 +00:00
|
|
|
return array_slice(static::$plugins, (($page - 1) * $perPage), $perPage);
|
2024-04-29 16:03:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-05-01 14:48:05 +00:00
|
|
|
* @param value-of<static::HOOKS> $name
|
|
|
|
* @param array<mixed> $arguments
|
2024-04-29 16:03:00 +00:00
|
|
|
*/
|
2024-05-01 14:48:05 +00:00
|
|
|
public function runHook(string $name, array $arguments): void
|
2024-04-29 16:03:00 +00:00
|
|
|
{
|
|
|
|
foreach (static::$plugins as $plugin) {
|
2024-05-01 14:48:05 +00:00
|
|
|
// only run hook on active plugins
|
|
|
|
if ($plugin->isActive()) {
|
|
|
|
$plugin->{$name}(...$arguments);
|
|
|
|
}
|
2024-04-29 16:03:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-01 14:48:05 +00:00
|
|
|
public function activate(string $pluginKey): void
|
|
|
|
{
|
|
|
|
set_plugin_option($pluginKey, 'active', true);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function deactivate(string $pluginKey): void
|
|
|
|
{
|
|
|
|
set_plugin_option($pluginKey, 'active', false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getInstalledCount(): int
|
|
|
|
{
|
|
|
|
return static::$installedCount;
|
|
|
|
}
|
|
|
|
|
2024-04-29 16:03:00 +00:00
|
|
|
protected function registerPlugins(): void
|
|
|
|
{
|
2024-05-01 14:48:05 +00:00
|
|
|
// search for plugins in plugins folder
|
|
|
|
$pluginsFiles = glob(ROOTPATH . '/plugins/**/Plugin.php', GLOB_NOSORT);
|
2024-04-29 16:03:00 +00:00
|
|
|
|
2024-05-01 14:48:05 +00:00
|
|
|
if (! $pluginsFiles) {
|
|
|
|
return;
|
|
|
|
}
|
2024-04-29 16:03:00 +00:00
|
|
|
|
2024-05-01 14:48:05 +00:00
|
|
|
$locator = service('locator');
|
2024-04-29 16:03:00 +00:00
|
|
|
foreach ($pluginsFiles as $file) {
|
|
|
|
$className = $locator->findQualifiedNameFromPath($file);
|
|
|
|
|
|
|
|
if ($className === false) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-05-01 14:48:05 +00:00
|
|
|
$plugin = new $className(basename(dirname($file)), $file);
|
|
|
|
if (! $plugin instanceof BasePlugin) {
|
2024-04-29 16:03:00 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
static::$plugins[] = $plugin;
|
2024-05-01 14:48:05 +00:00
|
|
|
++static::$installedCount;
|
2024-04-29 16:03:00 +00:00
|
|
|
}
|
2024-04-28 17:14:45 +00:00
|
|
|
}
|
|
|
|
}
|