mirror of
https://git.friendi.ca/friendica/friendica.git
synced 2025-06-11 09:04:26 +02:00
Use AddonLoader in AddonManager
This commit is contained in:
parent
247e9c5fba
commit
ee480130ad
4 changed files with 22 additions and 52 deletions
|
@ -15,7 +15,7 @@ use Psr\Log\LoggerInterface;
|
|||
/**
|
||||
* Factory for all addons.
|
||||
*/
|
||||
final class AddonFactory
|
||||
final class AddonFactory implements AddonLoader
|
||||
{
|
||||
private string $addonPath;
|
||||
|
||||
|
@ -30,6 +30,9 @@ final class AddonFactory
|
|||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Addon[] Returns an array of Addon instances.
|
||||
*/
|
||||
public function getAddons(array $addonNames): array
|
||||
{
|
||||
foreach ($addonNames as $addonName => $addonDetails) {
|
||||
|
|
|
@ -17,29 +17,19 @@ use Psr\Log\LoggerInterface;
|
|||
*/
|
||||
final class AddonManager
|
||||
{
|
||||
private string $addonPath;
|
||||
|
||||
private LoggerInterface $logger;
|
||||
private AddonLoader $addonFactory;
|
||||
|
||||
/** @var Addon[] */
|
||||
private array $addons = [];
|
||||
|
||||
public function __construct(string $addonPath, LoggerInterface $logger)
|
||||
public function __construct(AddonLoader $addonFactory)
|
||||
{
|
||||
$this->addonPath = $addonPath;
|
||||
$this->logger = $logger;
|
||||
$this->addonFactory = $addonFactory;
|
||||
}
|
||||
|
||||
public function bootstrapAddons(array $addonNames): void
|
||||
{
|
||||
foreach ($addonNames as $addonName => $addonDetails) {
|
||||
try {
|
||||
$this->bootstrapAddon($addonName);
|
||||
} catch (\Throwable $th) {
|
||||
// @TODO Here we can check if we have a Legacy addon and try to load it
|
||||
// throw $th;
|
||||
}
|
||||
}
|
||||
$this->addons = $this->addonFactory->getAddons($addonNames);
|
||||
}
|
||||
|
||||
public function getAllRequiredDependencies(): array
|
||||
|
@ -76,29 +66,6 @@ final class AddonManager
|
|||
return $events;
|
||||
}
|
||||
|
||||
private function bootstrapAddon(string $addonName): void
|
||||
{
|
||||
$bootstrapFile = sprintf('%s/%s/bootstrap.php', $this->addonPath, $addonName);
|
||||
|
||||
if (!file_exists($bootstrapFile)) {
|
||||
throw new \RuntimeException(sprintf('Bootstrap file for addon "%s" not found.', $addonName));
|
||||
}
|
||||
|
||||
try {
|
||||
$bootstrap = require $bootstrapFile;
|
||||
} catch (\Throwable $th) {
|
||||
throw new \RuntimeException(sprintf('Something went wrong loading the Bootstrap file for addon "%s".', $addonName), $th->getCode(), $th);
|
||||
}
|
||||
|
||||
if (!($bootstrap instanceof AddonBootstrap)) {
|
||||
throw new \RuntimeException(sprintf('Bootstrap file for addon "%s" MUST return an instance of AddonBootstrap.', $addonName));
|
||||
}
|
||||
|
||||
$this->addons[$addonName] = new AddonProxy($bootstrap);
|
||||
|
||||
$this->logger->info(sprintf('Addon "%s" loaded.', $addonName));
|
||||
}
|
||||
|
||||
public function initAddons(array $dependencies): void
|
||||
{
|
||||
foreach ($this->addons as $addon) {
|
||||
|
|
|
@ -165,7 +165,8 @@ return (function(string $basepath, array $getVars, array $serverVars, array $coo
|
|||
\Psr\EventDispatcher\EventDispatcherInterface::class => [
|
||||
'instanceOf' => \Friendica\Event\EventDispatcher::class,
|
||||
],
|
||||
\Friendica\Service\Addon\AddonManager::class => [
|
||||
\Friendica\Service\Addon\AddonLoader::class => [
|
||||
'instanceOf' => \Friendica\Service\Addon\AddonFactory::class,
|
||||
'constructParams' => [
|
||||
$basepath . '/addon',
|
||||
],
|
||||
|
|
|
@ -10,6 +10,8 @@ declare(strict_types=1);
|
|||
namespace Friendica\Test\Unit\Service\Addon;
|
||||
|
||||
use Friendica\Event\HtmlFilterEvent;
|
||||
use Friendica\Service\Addon\Addon;
|
||||
use Friendica\Service\Addon\AddonLoader;
|
||||
use Friendica\Service\Addon\AddonManager;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
@ -18,31 +20,28 @@ class AddonManagerTest extends TestCase
|
|||
{
|
||||
public function testBootstrapAddonsLoadsTheAddon(): void
|
||||
{
|
||||
$logger = $this->createMock(LoggerInterface::class);
|
||||
$logger->expects($this->once())->method('info')->with('Addon "helloaddon" loaded.');
|
||||
$loader = $this->createMock(AddonLoader::class);
|
||||
$loader->expects($this->once())->method('getAddons')->willReturn(['helloaddon' => $this->createMock(Addon::class)]);
|
||||
|
||||
$manager = new AddonManager(
|
||||
dirname(__DIR__, 3) . '/Util',
|
||||
$logger
|
||||
);
|
||||
$manager = new AddonManager($loader);
|
||||
|
||||
$manager->bootstrapAddons(['helloaddon' => []]);
|
||||
}
|
||||
|
||||
public function testGetAllSubscribedEventsReturnsEvents(): void
|
||||
{
|
||||
$logger = $this->createMock(LoggerInterface::class);
|
||||
$logger->expects($this->once())->method('info')->with('Addon "helloaddon" loaded.');
|
||||
$addon = $this->createMock(Addon::class);
|
||||
$addon->expects($this->once())->method('getSubscribedEvents')->willReturn([[HtmlFilterEvent::PAGE_END, [Addon::class, 'onPageEnd']]]);
|
||||
|
||||
$manager = new AddonManager(
|
||||
dirname(__DIR__, 3) . '/Util',
|
||||
$logger
|
||||
);
|
||||
$loader = $this->createMock(AddonLoader::class);
|
||||
$loader->expects($this->once())->method('getAddons')->willReturn(['helloaddon' => $addon]);
|
||||
|
||||
$manager = new AddonManager($loader);
|
||||
|
||||
$manager->bootstrapAddons(['helloaddon' => []]);
|
||||
|
||||
$this->assertSame(
|
||||
[[HtmlFilterEvent::PAGE_END, ['', 'onPageEnd']]],
|
||||
[[HtmlFilterEvent::PAGE_END, [Addon::class, 'onPageEnd']]],
|
||||
$manager->getAllSubscribedEvents()
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue