diff --git a/bin/auth_ejabberd.php b/bin/auth_ejabberd.php index 30202bbd933..1cf62da3557 100755 --- a/bin/auth_ejabberd.php +++ b/bin/auth_ejabberd.php @@ -52,6 +52,7 @@ $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(); diff --git a/bin/console.php b/bin/console.php index 1c502cc673c..63412887d59 100755 --- a/bin/console.php +++ b/bin/console.php @@ -19,6 +19,5 @@ $dice = (new Dice())->addRules(require(dirname(__DIR__) . '/static/dependencies.config.php')); -$app = \Friendica\App::fromDice($dice); - -$app->processConsole($_SERVER['argv'] ?? []); +$container = \Friendica\Core\Container::fromDice($dice); +\Friendica\Core\Console::create($container, $_SERVER['argv'] ?? [])->execute(); diff --git a/bin/daemon.php b/bin/daemon.php index 499bbc5d9b9..06a97d74ca2 100755 --- a/bin/daemon.php +++ b/bin/daemon.php @@ -28,9 +28,8 @@ $dice = (new Dice())->addRules(require(dirname(__DIR__) . '/static/dependencies.config.php')); -$app = \Friendica\App::fromDice($dice); - $argv = $_SERVER['argv'] ?? []; array_splice($argv, 1, 0, "daemon"); -$app->processConsole($argv); +$container = \Friendica\Core\Container::fromDice($dice); +\Friendica\Core\Console::create($container, $_SERVER['argv'] ?? [])->execute(); diff --git a/bin/jetstream.php b/bin/jetstream.php index f1834c783b5..7c9447a59ef 100755 --- a/bin/jetstream.php +++ b/bin/jetstream.php @@ -23,9 +23,8 @@ $dice = (new Dice())->addRules(require(dirname(__DIR__) . '/static/dependencies.config.php')); -$app = \Friendica\App::fromDice($dice); - $argv = $_SERVER['argv'] ?? []; array_splice($argv, 1, 0, "jetstream"); -$app->processConsole($argv); +$container = \Friendica\Core\Container::fromDice($dice); +\Friendica\Core\Console::create($container, $_SERVER['argv'] ?? [])->execute(); diff --git a/bin/worker.php b/bin/worker.php index 4837cd67eb6..4efc614d63d 100755 --- a/bin/worker.php +++ b/bin/worker.php @@ -25,9 +25,8 @@ $dice = (new Dice())->addRules(require(dirname(__DIR__) . '/static/dependencies.config.php')); -$app = \Friendica\App::fromDice($dice); - $argv = $_SERVER['argv'] ?? []; array_splice($argv, 1, 0, "worker"); -$app->processConsole($argv); +$container = \Friendica\Core\Container::fromDice($dice); +\Friendica\Core\Console::create($container, $_SERVER['argv'] ?? [])->execute(); diff --git a/index.php b/index.php index 5cdb3093631..954f9b74233 100644 --- a/index.php +++ b/index.php @@ -19,6 +19,7 @@ $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); diff --git a/src/App.php b/src/App.php index 79981526652..487707f9fdf 100644 --- a/src/App.php +++ b/src/App.php @@ -18,6 +18,7 @@ use Friendica\Capabilities\ICanHandleRequests; use Friendica\Content\Nav; use Friendica\Core\Config\Factory\Config; +use Friendica\Core\Container; use Friendica\Core\Renderer; use Friendica\Core\Session\Capability\IHandleUserSessions; use Friendica\Database\Definition\DbaDefinition; @@ -59,13 +60,13 @@ class App const CODENAME = 'Interrupted Fern'; 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; @@ -120,26 +121,20 @@ public static function fromDice(Dice $dice): self */ private $appHelper; - private function __construct(Dice $container) + private function __construct(Container $container) { $this->container = $container; } public function processRequest(ServerRequestInterface $request, float $start_time): void { - $this->setupContainerForAddons(); - - $this->setupContainerForLogger(LogChannel::DEFAULT); - - $this->container = $this->container->addRule(Mode::class, [ + $this->container->addRule(Mode::class, [ 'call' => [ ['determineRunMode', [false, $request->getServerParams()], Dice::CHAIN_CALL], ], ]); - $this->setupLegacyServiceLocator(); - - $this->registerErrorHandler(); + $this->container->setup(LogChannel::APP, false); $this->requestId = $this->container->create(Request::class)->getRequestId(); $this->auth = $this->container->create(Authentication::class); @@ -175,13 +170,7 @@ public function processRequest(ServerRequestInterface $request, float $start_tim public function processEjabberd(): void { - $this->setupContainerForAddons(); - - $this->setupContainerForLogger(LogChannel::AUTH_JABBERED); - - $this->setupLegacyServiceLocator(); - - $this->registerErrorHandler(); + $this->container->setup(LogChannel::AUTH_JABBERED, false); /** @var BasePath */ $basePath = $this->container->create(BasePath::class); @@ -198,46 +187,6 @@ public function processEjabberd(): void } } - 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 { Renderer::registerTemplateEngine('Friendica\Render\FriendicaSmartyEngine'); diff --git a/src/Console/AbstractConsole.php b/src/Console/AbstractConsole.php new file mode 100644 index 00000000000..9311d6f4eab --- /dev/null +++ b/src/Console/AbstractConsole.php @@ -0,0 +1,19 @@ +logger->warning('blah!'); + $this->mode->setExecutor(Mode::DAEMON); $this->config->reload(); @@ -120,7 +125,7 @@ protected function doExecute() $foreground = $this->getOption(['f', 'foreground']) ?? false; 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); diff --git a/src/Console/JetstreamDaemon.php b/src/Console/JetstreamDaemon.php index 133bf9c073f..3e1867a6e04 100644 --- a/src/Console/JetstreamDaemon.php +++ b/src/Console/JetstreamDaemon.php @@ -12,9 +12,9 @@ use Friendica\App\Mode; use Friendica\Core\Addon; use Friendica\Core\Config\Capability\IManageConfigValues; -use Asika\SimpleConsole\Console; use Friendica\Core\Hook; use Friendica\Core\KeyValueStorage\Capability\IManageKeyValuePairs; +use Friendica\Core\Logger\Capability\LogChannel; use Friendica\Protocol\ATProtocol\Jetstream; use Friendica\System\Daemon as SysDaemon; use RuntimeException; @@ -22,8 +22,10 @@ /** * 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 IManageConfigValues $config; private IManageKeyValuePairs $keyValue; diff --git a/src/Console/Worker.php b/src/Console/Worker.php index 7b817104bd0..51c4a777ab9 100644 --- a/src/Console/Worker.php +++ b/src/Console/Worker.php @@ -10,7 +10,7 @@ namespace Friendica\Console; use Friendica\App\Mode; -use Asika\SimpleConsole\Console; +use Friendica\Core\Logger\Capability\LogChannel; use Friendica\Core\Update; use Friendica\Core\Worker as CoreWorker; use Friendica\Core\Worker\Repository\Process as ProcessRepository; @@ -19,8 +19,10 @@ /** * 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 BasePath $basePath; private ProcessRepository $processRepo; diff --git a/src/Core/Console.php b/src/Core/Console.php index e3718824658..ad4c6fd8078 100644 --- a/src/Core/Console.php +++ b/src/Core/Console.php @@ -10,6 +10,7 @@ use Dice\Dice; use Friendica; use Friendica\App; +use Friendica\Core\Logger\Capability\LogChannel; /** * Description of Console @@ -23,7 +24,7 @@ class Console extends \Asika\SimpleConsole\Console /** * @var Dice The DI library */ - protected $dice; + protected $container; protected function getHelp() { @@ -106,14 +107,18 @@ protected function getHelp() /** * CliInput Friendica constructor. * - * @param Dice $dice The DI library - * @param array $argv + * @param Container $container The Friendica container */ - public function __construct(Dice $dice, array $argv = null) + public function __construct(Container $container, array $argv = null) { 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 @@ -172,8 +177,14 @@ private function getSubConsole($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 */ - $subconsole = $this->dice->create($className, [$subargs]); + $subconsole = $this->container->create($className, [$subargs]); foreach ($this->options as $name => $value) { $subconsole->setOption($name, $value); @@ -181,5 +192,4 @@ private function getSubConsole($command) return $subconsole; } - } diff --git a/src/Core/Container.php b/src/Core/Container.php new file mode 100644 index 00000000000..89751a8d4ec --- /dev/null +++ b/src/Core/Container.php @@ -0,0 +1,74 @@ +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'); + } +} diff --git a/tests/Unit/AppTest.php b/tests/Unit/AppTest.php index 124e63472a7..975cb5eecb6 100644 --- a/tests/Unit/AppTest.php +++ b/tests/Unit/AppTest.php @@ -20,7 +20,7 @@ public function testFromDiceReturnsApp(): void $dice = $this->createMock(Dice::class); $dice->expects($this->never())->method('create'); - $app = App::fromDice($dice); + $app = App::fromContainer($dice); $this->assertInstanceOf(App::class, $app); }