2018-09-21 14:04:05 +02:00
|
|
|
<?php
|
2020-10-15 00:19:31 +02:00
|
|
|
|
2021-07-19 07:53:45 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2018-09-21 14:04:05 +02:00
|
|
|
namespace App;
|
|
|
|
|
2020-07-08 09:03:50 +02:00
|
|
|
use Doctrine\Inflector\Inflector;
|
|
|
|
use Doctrine\Inflector\InflectorFactory;
|
2018-09-21 14:04:05 +02:00
|
|
|
use Symfony\Component\Finder\Finder;
|
|
|
|
use Symfony\Component\Finder\SplFileInfo;
|
|
|
|
|
2022-07-01 09:41:04 +02:00
|
|
|
final class Plugins
|
2018-09-21 14:04:05 +02:00
|
|
|
{
|
|
|
|
/** @var array An array of all plugins and their capabilities. */
|
2022-07-01 09:41:04 +02:00
|
|
|
private array $plugins = [];
|
2018-09-21 14:04:05 +02:00
|
|
|
|
2022-07-01 09:41:04 +02:00
|
|
|
private Inflector $inflector;
|
2020-07-08 09:03:50 +02:00
|
|
|
|
2021-07-19 07:53:45 +02:00
|
|
|
public function __construct(string $baseDir)
|
2018-09-21 14:04:05 +02:00
|
|
|
{
|
2020-07-08 09:03:50 +02:00
|
|
|
$this->inflector = InflectorFactory::create()
|
|
|
|
->build();
|
|
|
|
|
2021-07-19 07:53:45 +02:00
|
|
|
$this->loadDirectory($baseDir);
|
2018-09-21 14:04:05 +02:00
|
|
|
}
|
|
|
|
|
2021-07-19 07:53:45 +02:00
|
|
|
public function loadDirectory(string $dir): void
|
2018-09-21 14:04:05 +02:00
|
|
|
{
|
|
|
|
$plugins = (new Finder())
|
|
|
|
->ignoreUnreadableDirs()
|
|
|
|
->directories()
|
2018-09-27 19:29:05 +02:00
|
|
|
->depth('== 0')
|
2018-09-21 14:04:05 +02:00
|
|
|
->in($dir);
|
|
|
|
|
2023-06-08 13:32:32 +02:00
|
|
|
foreach ($plugins as $pluginDir) {
|
|
|
|
/** @var SplFileInfo $pluginDir */
|
|
|
|
$pluginPrefix = $pluginDir->getRelativePathname();
|
|
|
|
$pluginNamespace = 'Plugin\\' . $this->inflector->classify($pluginPrefix) . '\\';
|
|
|
|
|
|
|
|
$this->plugins[$pluginPrefix] = [
|
|
|
|
'namespace' => $pluginNamespace,
|
|
|
|
'path' => $pluginDir->getPathname(),
|
2018-09-21 14:04:05 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register or override any services contained in the global Dependency Injection container.
|
|
|
|
*
|
2019-08-07 06:33:55 +02:00
|
|
|
* @param array $diDefinitions
|
2019-09-20 18:44:38 +02:00
|
|
|
*
|
2020-10-15 00:19:31 +02:00
|
|
|
* @return mixed[]
|
2018-09-21 14:04:05 +02:00
|
|
|
*/
|
2019-08-07 06:33:55 +02:00
|
|
|
public function registerServices(array $diDefinitions = []): array
|
2018-09-21 14:04:05 +02:00
|
|
|
{
|
2019-09-04 20:00:51 +02:00
|
|
|
foreach ($this->plugins as $plugin) {
|
2023-06-08 13:32:32 +02:00
|
|
|
$pluginPath = $plugin['path'];
|
2018-09-21 14:04:05 +02:00
|
|
|
|
2023-06-08 13:32:32 +02:00
|
|
|
if (is_file($pluginPath . '/services.php')) {
|
|
|
|
$services = include $pluginPath . '/services.php';
|
2019-08-07 06:33:55 +02:00
|
|
|
$diDefinitions = array_merge($diDefinitions, $services);
|
2018-09-21 14:04:05 +02:00
|
|
|
}
|
|
|
|
}
|
2019-08-07 06:33:55 +02:00
|
|
|
|
|
|
|
return $diDefinitions;
|
2018-09-21 14:04:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register custom events that the plugin overrides with the Event Dispatcher.
|
|
|
|
*
|
2021-08-05 04:24:27 +02:00
|
|
|
* @param CallableEventDispatcherInterface $dispatcher
|
2018-09-21 14:04:05 +02:00
|
|
|
*/
|
2021-08-05 04:24:27 +02:00
|
|
|
public function registerEvents(CallableEventDispatcherInterface $dispatcher): void
|
2018-09-21 14:04:05 +02:00
|
|
|
{
|
2019-09-04 20:00:51 +02:00
|
|
|
foreach ($this->plugins as $plugin) {
|
2023-06-08 13:32:32 +02:00
|
|
|
$pluginPath = $plugin['path'];
|
2018-09-21 14:04:05 +02:00
|
|
|
|
2023-06-08 13:32:32 +02:00
|
|
|
if (file_exists($pluginPath . '/events.php')) {
|
|
|
|
call_user_func(include($pluginPath . '/events.php'), $dispatcher);
|
2018-09-21 14:04:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-18 07:56:44 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param mixed[] $receivers
|
|
|
|
*
|
|
|
|
* @return mixed[]
|
|
|
|
*/
|
|
|
|
public function registerMessageQueueReceivers(array $receivers): array
|
|
|
|
{
|
|
|
|
foreach ($this->plugins as $plugin) {
|
|
|
|
$pluginPath = $plugin['path'];
|
|
|
|
|
2021-06-08 08:40:49 +02:00
|
|
|
if (is_file($pluginPath . '/messagequeue.php')) {
|
2020-10-18 07:56:44 +02:00
|
|
|
$pluginReceivers = include $pluginPath . '/messagequeue.php';
|
|
|
|
$receivers = array_merge($receivers, $pluginReceivers);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $receivers;
|
|
|
|
}
|
2018-09-21 14:04:05 +02:00
|
|
|
}
|