Move Container logic into own class

This commit is contained in:
Philipp 2025-01-05 22:02:38 +01:00
parent 6d77eb9b10
commit 40fbb02149
No known key found for this signature in database
GPG key ID: 24A7501396EB5432
14 changed files with 147 additions and 88 deletions

View file

@ -52,6 +52,7 @@ require dirname(__FILE__, 2) . '/vendor/autoload.php';
$dice = (new Dice())->addRules(require(dirname(__FILE__, 2) . '/static/dependencies.config.php')); $dice = (new Dice())->addRules(require(dirname(__FILE__, 2) . '/static/dependencies.config.php'));
$app = \Friendica\App::fromDice($dice); $container = \Friendica\Core\Container::fromDice($dice);
$app = \Friendica\App::fromContainer($container);
$app->processEjabberd(); $app->processEjabberd();

View file

@ -19,6 +19,5 @@ require dirname(__DIR__) . '/vendor/autoload.php';
$dice = (new Dice())->addRules(require(dirname(__DIR__) . '/static/dependencies.config.php')); $dice = (new Dice())->addRules(require(dirname(__DIR__) . '/static/dependencies.config.php'));
$app = \Friendica\App::fromDice($dice); $container = \Friendica\Core\Container::fromDice($dice);
\Friendica\Core\Console::create($container, $_SERVER['argv'] ?? [])->execute();
$app->processConsole($_SERVER['argv'] ?? []);

View file

@ -28,9 +28,8 @@ require dirname(__DIR__) . '/vendor/autoload.php';
$dice = (new Dice())->addRules(require(dirname(__DIR__) . '/static/dependencies.config.php')); $dice = (new Dice())->addRules(require(dirname(__DIR__) . '/static/dependencies.config.php'));
$app = \Friendica\App::fromDice($dice);
$argv = $_SERVER['argv'] ?? []; $argv = $_SERVER['argv'] ?? [];
array_splice($argv, 1, 0, "daemon"); array_splice($argv, 1, 0, "daemon");
$app->processConsole($argv); $container = \Friendica\Core\Container::fromDice($dice);
\Friendica\Core\Console::create($container, $_SERVER['argv'] ?? [])->execute();

View file

@ -23,9 +23,8 @@ require dirname(__DIR__) . '/vendor/autoload.php';
$dice = (new Dice())->addRules(require(dirname(__DIR__) . '/static/dependencies.config.php')); $dice = (new Dice())->addRules(require(dirname(__DIR__) . '/static/dependencies.config.php'));
$app = \Friendica\App::fromDice($dice);
$argv = $_SERVER['argv'] ?? []; $argv = $_SERVER['argv'] ?? [];
array_splice($argv, 1, 0, "jetstream"); array_splice($argv, 1, 0, "jetstream");
$app->processConsole($argv); $container = \Friendica\Core\Container::fromDice($dice);
\Friendica\Core\Console::create($container, $_SERVER['argv'] ?? [])->execute();

View file

@ -25,9 +25,8 @@ require dirname(__DIR__) . '/vendor/autoload.php';
$dice = (new Dice())->addRules(require(dirname(__DIR__) . '/static/dependencies.config.php')); $dice = (new Dice())->addRules(require(dirname(__DIR__) . '/static/dependencies.config.php'));
$app = \Friendica\App::fromDice($dice);
$argv = $_SERVER['argv'] ?? []; $argv = $_SERVER['argv'] ?? [];
array_splice($argv, 1, 0, "worker"); array_splice($argv, 1, 0, "worker");
$app->processConsole($argv); $container = \Friendica\Core\Container::fromDice($dice);
\Friendica\Core\Console::create($container, $_SERVER['argv'] ?? [])->execute();

View file

@ -19,6 +19,7 @@ $request = \GuzzleHttp\Psr7\ServerRequest::fromGlobals();
$dice = (new Dice())->addRules(require(__DIR__ . '/static/dependencies.config.php')); $dice = (new Dice())->addRules(require(__DIR__ . '/static/dependencies.config.php'));
$app = \Friendica\App::fromDice($dice); $container = \Friendica\Core\Container::fromDice($dice);
$app = \Friendica\App::fromContainer($container);
$app->processRequest($request, $start_time); $app->processRequest($request, $start_time);

View file

@ -18,6 +18,7 @@ use Friendica\Capabilities\ICanCreateResponses;
use Friendica\Capabilities\ICanHandleRequests; use Friendica\Capabilities\ICanHandleRequests;
use Friendica\Content\Nav; use Friendica\Content\Nav;
use Friendica\Core\Config\Factory\Config; use Friendica\Core\Config\Factory\Config;
use Friendica\Core\Container;
use Friendica\Core\Renderer; use Friendica\Core\Renderer;
use Friendica\Core\Session\Capability\IHandleUserSessions; use Friendica\Core\Session\Capability\IHandleUserSessions;
use Friendica\Database\Definition\DbaDefinition; use Friendica\Database\Definition\DbaDefinition;
@ -59,13 +60,13 @@ class App
const CODENAME = 'Interrupted Fern'; const CODENAME = 'Interrupted Fern';
const VERSION = '2025.02-dev'; const VERSION = '2025.02-dev';
public static function fromDice(Dice $dice): self public static function fromContainer(Container $container): self
{ {
return new self($dice); return new self($container);
} }
/** /**
* @var Dice * @var Container
*/ */
private $container; private $container;
@ -120,26 +121,20 @@ class App
*/ */
private $appHelper; private $appHelper;
private function __construct(Dice $container) private function __construct(Container $container)
{ {
$this->container = $container; $this->container = $container;
} }
public function processRequest(ServerRequestInterface $request, float $start_time): void public function processRequest(ServerRequestInterface $request, float $start_time): void
{ {
$this->setupContainerForAddons(); $this->container->addRule(Mode::class, [
$this->setupContainerForLogger(LogChannel::DEFAULT);
$this->container = $this->container->addRule(Mode::class, [
'call' => [ 'call' => [
['determineRunMode', [false, $request->getServerParams()], Dice::CHAIN_CALL], ['determineRunMode', [false, $request->getServerParams()], Dice::CHAIN_CALL],
], ],
]); ]);
$this->setupLegacyServiceLocator(); $this->container->setup(LogChannel::APP, false);
$this->registerErrorHandler();
$this->requestId = $this->container->create(Request::class)->getRequestId(); $this->requestId = $this->container->create(Request::class)->getRequestId();
$this->auth = $this->container->create(Authentication::class); $this->auth = $this->container->create(Authentication::class);
@ -175,13 +170,7 @@ class App
public function processEjabberd(): void public function processEjabberd(): void
{ {
$this->setupContainerForAddons(); $this->container->setup(LogChannel::AUTH_JABBERED, false);
$this->setupContainerForLogger(LogChannel::AUTH_JABBERED);
$this->setupLegacyServiceLocator();
$this->registerErrorHandler();
/** @var BasePath */ /** @var BasePath */
$basePath = $this->container->create(BasePath::class); $basePath = $this->container->create(BasePath::class);
@ -198,46 +187,6 @@ class App
} }
} }
public function processConsole(array $argv): void
{
$this->setupContainerForAddons();
$this->setupContainerForLogger(LogChannel::CONSOLE);
$this->setupLegacyServiceLocator();
$this->registerErrorHandler();
$this->registerTemplateEngine();
(new \Friendica\Core\Console($this->container, $argv))->execute();
}
private function setupContainerForAddons(): void
{
/** @var \Friendica\Core\Addon\Capability\ICanLoadAddons $addonLoader */
$addonLoader = $this->container->create(\Friendica\Core\Addon\Capability\ICanLoadAddons::class);
$this->container = $this->container->addRules($addonLoader->getActiveAddonConfig('dependencies'));
}
private function setupContainerForLogger(string $logChannel): void
{
$this->container = $this->container->addRule(LoggerInterface::class, [
'constructParams' => [$logChannel],
]);
}
private function setupLegacyServiceLocator(): void
{
DI::init($this->container);
}
private function registerErrorHandler(): void
{
\Friendica\Core\Logger\Handler\ErrorHandler::register($this->container->create(LoggerInterface::class));
}
private function registerTemplateEngine(): void private function registerTemplateEngine(): void
{ {
Renderer::registerTemplateEngine('Friendica\Render\FriendicaSmartyEngine'); Renderer::registerTemplateEngine('Friendica\Render\FriendicaSmartyEngine');

View file

@ -0,0 +1,19 @@
<?php
namespace Friendica\Console;
use Asika\SimpleConsole\Console;
use Friendica\Core\Logger\Capability\LogChannel;
/**
* Abstract Console class for common settings
*/
abstract class AbstractConsole extends Console
{
/**
* Overwrite this const in case you want to switch the LogChannel for this console command
*
* @var string
*/
public const LOG_CHANNEL = LogChannel::class;
}

View file

@ -9,10 +9,11 @@ declare(strict_types=1);
namespace Friendica\Console; namespace Friendica\Console;
use Asika\SimpleConsole\CommandArgsException;
use Friendica\App\Mode; use Friendica\App\Mode;
use Friendica\Core\Config\Capability\IManageConfigValues; use Friendica\Core\Config\Capability\IManageConfigValues;
use Asika\SimpleConsole\Console;
use Friendica\Core\KeyValueStorage\Capability\IManageKeyValuePairs; use Friendica\Core\KeyValueStorage\Capability\IManageKeyValuePairs;
use Friendica\Core\Logger\Capability\LogChannel;
use Friendica\Core\System; use Friendica\Core\System;
use Friendica\Core\Update; use Friendica\Core\Update;
use Friendica\Core\Worker; use Friendica\Core\Worker;
@ -26,8 +27,10 @@ use RuntimeException;
/** /**
* Console command for interacting with the daemon * Console command for interacting with the daemon
*/ */
final class Daemon extends Console final class Daemon extends AbstractConsole
{ {
public const LOG_CHANNEL = LogChannel::DAEMON;
private Mode $mode; private Mode $mode;
private IManageConfigValues $config; private IManageConfigValues $config;
private IManageKeyValuePairs $keyValue; private IManageKeyValuePairs $keyValue;
@ -98,6 +101,8 @@ HELP;
throw new RuntimeException("Friendica isn't properly installed yet"); throw new RuntimeException("Friendica isn't properly installed yet");
} }
$this->logger->warning('blah!');
$this->mode->setExecutor(Mode::DAEMON); $this->mode->setExecutor(Mode::DAEMON);
$this->config->reload(); $this->config->reload();
@ -120,7 +125,7 @@ HELP;
$foreground = $this->getOption(['f', 'foreground']) ?? false; $foreground = $this->getOption(['f', 'foreground']) ?? false;
if (empty($daemonMode)) { if (empty($daemonMode)) {
throw new RuntimeException("Please use either 'start', 'stop' or 'status'"); throw new CommandArgsException("Please use either 'start', 'stop' or 'status'");
} }
$this->daemon->init($pidfile); $this->daemon->init($pidfile);

View file

@ -12,9 +12,9 @@ namespace Friendica\Console;
use Friendica\App\Mode; use Friendica\App\Mode;
use Friendica\Core\Addon; use Friendica\Core\Addon;
use Friendica\Core\Config\Capability\IManageConfigValues; use Friendica\Core\Config\Capability\IManageConfigValues;
use Asika\SimpleConsole\Console;
use Friendica\Core\Hook; use Friendica\Core\Hook;
use Friendica\Core\KeyValueStorage\Capability\IManageKeyValuePairs; use Friendica\Core\KeyValueStorage\Capability\IManageKeyValuePairs;
use Friendica\Core\Logger\Capability\LogChannel;
use Friendica\Protocol\ATProtocol\Jetstream; use Friendica\Protocol\ATProtocol\Jetstream;
use Friendica\System\Daemon as SysDaemon; use Friendica\System\Daemon as SysDaemon;
use RuntimeException; use RuntimeException;
@ -22,8 +22,10 @@ use RuntimeException;
/** /**
* Console command for interacting with the daemon * Console command for interacting with the daemon
*/ */
final class JetstreamDaemon extends Console final class JetstreamDaemon extends AbstractConsole
{ {
public const LOG_CHANNEL = LogChannel::AUTH_JABBERED;
private Mode $mode; private Mode $mode;
private IManageConfigValues $config; private IManageConfigValues $config;
private IManageKeyValuePairs $keyValue; private IManageKeyValuePairs $keyValue;

View file

@ -10,7 +10,7 @@ declare(strict_types=1);
namespace Friendica\Console; namespace Friendica\Console;
use Friendica\App\Mode; use Friendica\App\Mode;
use Asika\SimpleConsole\Console; use Friendica\Core\Logger\Capability\LogChannel;
use Friendica\Core\Update; use Friendica\Core\Update;
use Friendica\Core\Worker as CoreWorker; use Friendica\Core\Worker as CoreWorker;
use Friendica\Core\Worker\Repository\Process as ProcessRepository; use Friendica\Core\Worker\Repository\Process as ProcessRepository;
@ -19,8 +19,10 @@ use Friendica\Util\BasePath;
/** /**
* Console command for starting worker * Console command for starting worker
*/ */
final class Worker extends Console final class Worker extends AbstractConsole
{ {
public const LOG_CHANNEL = LogChannel::WORKER;
private Mode $mode; private Mode $mode;
private BasePath $basePath; private BasePath $basePath;
private ProcessRepository $processRepo; private ProcessRepository $processRepo;

View file

@ -10,6 +10,7 @@ namespace Friendica\Core;
use Dice\Dice; use Dice\Dice;
use Friendica; use Friendica;
use Friendica\App; use Friendica\App;
use Friendica\Core\Logger\Capability\LogChannel;
/** /**
* Description of Console * Description of Console
@ -23,7 +24,7 @@ class Console extends \Asika\SimpleConsole\Console
/** /**
* @var Dice The DI library * @var Dice The DI library
*/ */
protected $dice; protected $container;
protected function getHelp() protected function getHelp()
{ {
@ -106,14 +107,18 @@ HELP;
/** /**
* CliInput Friendica constructor. * CliInput Friendica constructor.
* *
* @param Dice $dice The DI library * @param Container $container The Friendica container
* @param array $argv
*/ */
public function __construct(Dice $dice, array $argv = null) public function __construct(Container $container, array $argv = null)
{ {
parent::__construct($argv); parent::__construct($argv);
$this->dice = $dice; $this->container = $container;
}
public static function create(Container $container, array $argv = null): Console
{
return new static($container, $argv);
} }
protected function doExecute(): int protected function doExecute(): int
@ -172,8 +177,14 @@ HELP;
$className = $this->subConsoles[$command]; $className = $this->subConsoles[$command];
if (is_subclass_of($className, Friendica\Console\AbstractConsole::class)) {
$this->container->setup($className::LOG_CHANNEL);
} else {
$this->container->setup(LogChannel::CONSOLE);
}
/** @var Console $subconsole */ /** @var Console $subconsole */
$subconsole = $this->dice->create($className, [$subargs]); $subconsole = $this->container->create($className, [$subargs]);
foreach ($this->options as $name => $value) { foreach ($this->options as $name => $value) {
$subconsole->setOption($name, $value); $subconsole->setOption($name, $value);
@ -181,5 +192,4 @@ HELP;
return $subconsole; return $subconsole;
} }
} }

74
src/Core/Container.php Normal file
View file

@ -0,0 +1,74 @@
<?php
namespace Friendica\Core;
use Dice\Dice;
use Friendica\Core\Logger\Capability\LogChannel;
use Friendica\DI;
use Psr\Log\LoggerInterface;
final class Container
{
private Dice $container;
protected function __construct(Dice $container)
{
$this->container = $container;
}
public static function fromDice(Dice $container): self {
return new static($container);
}
public function setup(string $logChannel = LogChannel::DEFAULT, bool $withTemplateEngine = true)
{
$this->setupContainerForAddons();
$this->setupContainerForLogger($logChannel);
$this->setupLegacyServiceLocator();
$this->registerErrorHandler();
if ($withTemplateEngine) {
$this->registerTemplateEngine();
}
}
public function create(string $name, array $args = [], array $share = []): object
{
return $this->container->create($name, $args, $share);
}
public function addRule(string $name, array $rule):void
{
$this->container = $this->container->addRule($name, $rule);
}
private function setupContainerForAddons(): void
{
/** @var \Friendica\Core\Addon\Capability\ICanLoadAddons $addonLoader */
$addonLoader = $this->container->create(\Friendica\Core\Addon\Capability\ICanLoadAddons::class);
$this->container = $this->container->addRules($addonLoader->getActiveAddonConfig('dependencies'));
}
private function setupContainerForLogger(string $logChannel): void
{
$this->container = $this->container->addRule(LoggerInterface::class, [
'constructParams' => [$logChannel],
]);
}
private function setupLegacyServiceLocator(): void
{
DI::init($this->container);
}
private function registerErrorHandler(): void
{
\Friendica\Core\Logger\Handler\ErrorHandler::register($this->container->create(LoggerInterface::class));
}
private function registerTemplateEngine(): void
{
Renderer::registerTemplateEngine('Friendica\Render\FriendicaSmartyEngine');
}
}

View file

@ -20,7 +20,7 @@ class AppTest extends TestCase
$dice = $this->createMock(Dice::class); $dice = $this->createMock(Dice::class);
$dice->expects($this->never())->method('create'); $dice->expects($this->never())->method('create');
$app = App::fromDice($dice); $app = App::fromContainer($dice);
$this->assertInstanceOf(App::class, $app); $this->assertInstanceOf(App::class, $app);
} }