]> git.mxchange.org Git - friendica.git/commitdiff
Move Container logic into own class
authorPhilipp <admin@philipp.info>
Sun, 5 Jan 2025 21:02:38 +0000 (22:02 +0100)
committerPhilipp <admin@philipp.info>
Sun, 5 Jan 2025 22:13:41 +0000 (23:13 +0100)
14 files changed:
bin/auth_ejabberd.php
bin/console.php
bin/daemon.php
bin/jetstream.php
bin/worker.php
index.php
src/App.php
src/Console/AbstractConsole.php [new file with mode: 0644]
src/Console/Daemon.php
src/Console/JetstreamDaemon.php
src/Console/Worker.php
src/Core/Console.php
src/Core/Container.php [new file with mode: 0644]
tests/Unit/AppTest.php

index 30202bbd9333e3e397f1965ade7f42c61c3e8c40..1cf62da35576d294527de8bc0777c1704a83c8ba 100755 (executable)
@@ -52,6 +52,7 @@ require dirname(__FILE__, 2) . '/vendor/autoload.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();
index 1c502cc673c044f2808306ce434b679cb28bcf55..63412887d59d41e79478869c81262adc1074d569 100755 (executable)
@@ -19,6 +19,5 @@ require dirname(__DIR__) . '/vendor/autoload.php';
 
 $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();
index 499bbc5d9b9d0e6c4732f713e27360e50f2c11c5..06a97d74ca2d0e048ac4b2fb8c60c17dcb0e5ec0 100755 (executable)
@@ -28,9 +28,8 @@ require dirname(__DIR__) . '/vendor/autoload.php';
 
 $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();
index f1834c783b5f9f3307cc01a3a9d464f512d9d7e7..7c9447a59ef6ead955c1b1cd6bf9a5e9d97ea71a 100755 (executable)
@@ -23,9 +23,8 @@ require dirname(__DIR__) . '/vendor/autoload.php';
 
 $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();
index 4837cd67eb63b0deadfb58dc286382f063c9d392..4efc614d63d645126584de5c983d96f94bb9720e 100755 (executable)
@@ -25,9 +25,8 @@ require dirname(__DIR__) . '/vendor/autoload.php';
 
 $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();
index 5cdb30936318b432bd507ba12038d71ecd490644..954f9b742337b1f308464ef6a0f31a3cf048e026 100644 (file)
--- a/index.php
+++ b/index.php
@@ -19,6 +19,7 @@ $request = \GuzzleHttp\Psr7\ServerRequest::fromGlobals();
 
 $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);
index 79981526652649526919127c5f6bd5638d4fac15..487707f9fdfe85a5a6947e4cf1e6cb0d6c425faa 100644 (file)
@@ -18,6 +18,7 @@ use Friendica\Capabilities\ICanCreateResponses;
 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 @@ class App
         */
        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 @@ class App
 
        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 @@ 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
        {
                Renderer::registerTemplateEngine('Friendica\Render\FriendicaSmartyEngine');
diff --git a/src/Console/AbstractConsole.php b/src/Console/AbstractConsole.php
new file mode 100644 (file)
index 0000000..9311d6f
--- /dev/null
@@ -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;
+}
index eac7b935a8d2d9b270cbb7aecbdb94a47c4f873e..3838c80adae069b3f10b6621090710792c7afcde 100644 (file)
@@ -9,10 +9,11 @@ declare(strict_types=1);
 
 namespace Friendica\Console;
 
+use Asika\SimpleConsole\CommandArgsException;
 use Friendica\App\Mode;
 use Friendica\Core\Config\Capability\IManageConfigValues;
-use Asika\SimpleConsole\Console;
 use Friendica\Core\KeyValueStorage\Capability\IManageKeyValuePairs;
+use Friendica\Core\Logger\Capability\LogChannel;
 use Friendica\Core\System;
 use Friendica\Core\Update;
 use Friendica\Core\Worker;
@@ -26,8 +27,10 @@ use RuntimeException;
 /**
  * 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 IManageConfigValues $config;
        private IManageKeyValuePairs $keyValue;
@@ -98,6 +101,8 @@ HELP;
                        throw new RuntimeException("Friendica isn't properly installed yet");
                }
 
+               $this->logger->warning('blah!');
+
                $this->mode->setExecutor(Mode::DAEMON);
 
                $this->config->reload();
@@ -120,7 +125,7 @@ HELP;
                $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);
index 133bf9c073ffef7ddf1af8e4cc7f5d0319c8730d..3e1867a6e0442072a9e0af22d1b1ba600be2cd6c 100644 (file)
@@ -12,9 +12,9 @@ namespace Friendica\Console;
 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 @@ use RuntimeException;
 /**
  * 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;
index 7b817104bd032812218500303a999b143cf72d3c..51c4a777ab90d966e0b961fff65934c40278a023 100644 (file)
@@ -10,7 +10,7 @@ declare(strict_types=1);
 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 @@ use Friendica\Util\BasePath;
 /**
  * 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;
index e3718824658e695a64c53c8fb7c1784b43bf6ca9..ad4c6fd8078359630e84dc7dde99b03471964df3 100644 (file)
@@ -10,6 +10,7 @@ namespace Friendica\Core;
 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 @@ HELP;
        /**
         * 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 @@ HELP;
 
                $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 @@ HELP;
 
                return $subconsole;
        }
-
 }
diff --git a/src/Core/Container.php b/src/Core/Container.php
new file mode 100644 (file)
index 0000000..89751a8
--- /dev/null
@@ -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');
+       }
+}
index 124e63472a709e48386ed7633fe6942a36d40dd6..975cb5eecb6a488a92bffa9e9e92cea59664dc16 100644 (file)
@@ -20,7 +20,7 @@ class AppTest extends TestCase
                $dice = $this->createMock(Dice::class);
                $dice->expects($this->never())->method('create');
 
-               $app = App::fromDice($dice);
+               $app = App::fromContainer($dice);
 
                $this->assertInstanceOf(App::class, $app);
        }