]> git.mxchange.org Git - friendica.git/commitdiff
Create Container interface, add DiceContainer as implementation
authorArt4 <art4@wlabs.de>
Wed, 8 Jan 2025 22:59:29 +0000 (22:59 +0000)
committerArt4 <art4@wlabs.de>
Wed, 8 Jan 2025 22:59:29 +0000 (22:59 +0000)
bin/auth_ejabberd.php
bin/console.php
bin/daemon.php
bin/jetstream.php
bin/worker.php
index.php
src/Core/Container.php
src/Core/DiceContainer.php [new file with mode: 0644]
tests/Unit/Core/ContainerTest.php [deleted file]
tests/Unit/Core/DiceContainerTest.php [new file with mode: 0644]

index 7a279b079bf0b73571c4eb9a27b7f215b546d56d..bc976e61a436fefc7890e56a2c2530210c6ab8ed 100755 (executable)
@@ -48,7 +48,7 @@ chdir(dirname(__DIR__));
 
 require dirname(__DIR__) . '/vendor/autoload.php';
 
-$container = \Friendica\Core\Container::fromBasePath(dirname(__DIR__));
+$container = \Friendica\Core\DiceContainer::fromBasePath(dirname(__DIR__));
 
 $app = \Friendica\App::fromContainer($container);
 
index 94738dbf8cc4bbccabed0856db495fa6f7d91311..ad612f43639682bb62bd8b0a76ad91572ba2243f 100755 (executable)
@@ -15,7 +15,7 @@ if (php_sapi_name() !== 'cli') {
 
 require dirname(__DIR__) . '/vendor/autoload.php';
 
-$container = \Friendica\Core\Container::fromBasePath(dirname(__DIR__));
+$container = \Friendica\Core\DiceContainer::fromBasePath(dirname(__DIR__));
 
 $app = \Friendica\App::fromContainer($container);
 
index 04e00ccf0d21b27576ed8e603594e60a6ff94dce..546f207e08143158529fa21012c016d79871e9ad 100755 (executable)
@@ -27,7 +27,7 @@ require dirname(__DIR__) . '/vendor/autoload.php';
 $argv = $_SERVER['argv'] ?? [];
 array_splice($argv, 1, 0, "daemon");
 
-$container = \Friendica\Core\Container::fromBasePath(dirname(__DIR__));
+$container = \Friendica\Core\DiceContainer::fromBasePath(dirname(__DIR__));
 
 $app = \Friendica\App::fromContainer($container);
 
index 72d13c3e1fbe7be10f5d161a127fc8333bb7d37c..156f96185662f5ec53afac6f03a019db3db6cff6 100755 (executable)
@@ -22,7 +22,7 @@ require dirname(__DIR__) . '/vendor/autoload.php';
 $argv = $_SERVER['argv'] ?? [];
 array_splice($argv, 1, 0, "jetstream");
 
-$container = \Friendica\Core\Container::fromBasePath(dirname(__DIR__));
+$container = \Friendica\Core\DiceContainer::fromBasePath(dirname(__DIR__));
 
 $app = \Friendica\App::fromContainer($container);
 
index 54b36e392fcf1c782bc59dbc746f2602062ec81e..a79d8836a73fc746afd9832984f5d2de86bdfbab 100755 (executable)
@@ -24,7 +24,7 @@ require dirname(__DIR__) . '/vendor/autoload.php';
 $argv = $_SERVER['argv'] ?? [];
 array_splice($argv, 1, 0, "worker");
 
-$container = \Friendica\Core\Container::fromBasePath(dirname(__DIR__));
+$container = \Friendica\Core\DiceContainer::fromBasePath(dirname(__DIR__));
 
 $app = \Friendica\App::fromContainer($container);
 
index 906d91bc3c7ee2250b9c88aff06de1975dd02c32..850d9cd1c020338535a0c870b0795f9e376e2204 100644 (file)
--- a/index.php
+++ b/index.php
@@ -15,7 +15,8 @@ require __DIR__ . '/vendor/autoload.php';
 
 $request = \GuzzleHttp\Psr7\ServerRequest::fromGlobals();
 
-$container = \Friendica\Core\Container::fromBasePath(__DIR__);
-$app       = \Friendica\App::fromContainer($container);
+$container = \Friendica\Core\DiceContainer::fromBasePath(__DIR__);
+
+$app = \Friendica\App::fromContainer($container);
 
 $app->processRequest($request, $start_time);
index 0da94935d8ad2727d40fe17d1cbae74d4b81cfd5..f0f44f38db7649424b0180c58e2c7e8954ee95ac 100644 (file)
@@ -9,46 +9,13 @@ declare(strict_types=1);
 
 namespace Friendica\Core;
 
-use Dice\Dice;
-use Friendica\Core\Addon\Capability\ICanLoadAddons;
 use Friendica\Core\Logger\Capability\LogChannel;
-use Friendica\Core\Logger\Handler\ErrorHandler;
-use Friendica\DI;
-use Psr\Log\LoggerInterface;
 
 /**
- * Wrapper for the Dice class to make some basic setups
+ * Dependency Injection Container
  */
-class Container
+interface Container
 {
-       public static function fromBasePath(string $basePath): self
-       {
-               $path = $basePath . '/static/dependencies.config.php';
-
-               $dice = (new Dice())->addRules(require($path));
-
-               return static::fromDice($dice);
-       }
-
-       private Dice $container;
-
-       private function __construct(Dice $container)
-       {
-               $this->container = $container;
-       }
-
-       /**
-        * Creates an instance with Dice
-        *
-        * @param Dice $container
-        *
-        * @return self
-        */
-       public static function fromDice(Dice $container): self
-       {
-               return new self($container);
-       }
-
        /**
         * Initialize the container with the given parameters
         *
@@ -57,17 +24,7 @@ class Container
         *
         * @return void
         */
-       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 setup(string $logChannel = LogChannel::DEFAULT, bool $withTemplateEngine = true): void;
 
        /**
         * Returns a fully constructed object based on $name using $args and $share as constructor arguments if supplied
@@ -78,10 +35,7 @@ class Container
         *
         * @see Dice::create()
         */
-       public function create(string $name, array $args = [], array $share = []): object
-       {
-               return $this->container->create($name, $args, $share);
-       }
+       public function create(string $name, array $args = [], array $share = []): object;
 
        /**
         * Add a rule $rule to the class $name
@@ -90,38 +44,5 @@ class Container
         *
         * @see Dice::addRule()
         */
-       public function addRule(string $name, array $rule): void
-       {
-               $this->container = $this->container->addRule($name, $rule);
-       }
-
-       private function setupContainerForAddons(): void
-       {
-               /** @var ICanLoadAddons $addonLoader */
-               $addonLoader = $this->container->create(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
-       {
-               ErrorHandler::register($this->container->create(LoggerInterface::class));
-       }
-
-       private function registerTemplateEngine(): void
-       {
-               Renderer::registerTemplateEngine('Friendica\Render\FriendicaSmartyEngine');
-       }
+       public function addRule(string $name, array $rule): void;
 }
diff --git a/src/Core/DiceContainer.php b/src/Core/DiceContainer.php
new file mode 100644 (file)
index 0000000..7d8b57b
--- /dev/null
@@ -0,0 +1,127 @@
+<?php
+
+// Copyright (C) 2010-2024, the Friendica project
+// SPDX-FileCopyrightText: 2010-2024 the Friendica project
+//
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+declare(strict_types=1);
+
+namespace Friendica\Core;
+
+use Dice\Dice;
+use Friendica\Core\Addon\Capability\ICanLoadAddons;
+use Friendica\Core\Logger\Capability\LogChannel;
+use Friendica\Core\Logger\Handler\ErrorHandler;
+use Friendica\DI;
+use Psr\Log\LoggerInterface;
+
+/**
+ * Wrapper for the Dice class to make some basic setups
+ */
+final class DiceContainer implements Container
+{
+       public static function fromBasePath(string $basePath): self
+       {
+               $path = $basePath . '/static/dependencies.config.php';
+
+               $dice = (new Dice())->addRules(require($path));
+
+               return static::fromDice($dice);
+       }
+
+       private Dice $container;
+
+       private function __construct(Dice $container)
+       {
+               $this->container = $container;
+       }
+
+       /**
+        * Creates an instance with Dice
+        *
+        * @param Dice $container
+        *
+        * @return self
+        */
+       public static function fromDice(Dice $container): self
+       {
+               return new self($container);
+       }
+
+       /**
+        * Initialize the container with the given parameters
+        *
+        * @param string $logChannel The Log Channel of this call
+        * @param bool   $withTemplateEngine true, if the template engine should be set too
+        *
+        * @return void
+        */
+       public function setup(string $logChannel = LogChannel::DEFAULT, bool $withTemplateEngine = true): void
+       {
+               $this->setupContainerForAddons();
+               $this->setupContainerForLogger($logChannel);
+               $this->setupLegacyServiceLocator();
+               $this->registerErrorHandler();
+
+               if ($withTemplateEngine) {
+                       $this->registerTemplateEngine();
+               }
+       }
+
+       /**
+        * Returns a fully constructed object based on $name using $args and $share as constructor arguments if supplied
+        * @param string $name  name The name of the class to instantiate
+        * @param array  $args  An array with any additional arguments to be passed into the constructor upon instantiation
+        * @param array  $share a list of defined in shareInstances for objects higher up the object graph, should only be used internally
+        * @return object A fully constructed object based on the specified input arguments
+        *
+        * @see Dice::create()
+        */
+       public function create(string $name, array $args = [], array $share = []): object
+       {
+               return $this->container->create($name, $args, $share);
+       }
+
+       /**
+        * Add a rule $rule to the class $name
+        * @param string $name The name of the class to add the rule for
+        * @param array  $rule The container can be fully configured using rules provided by associative arrays. See {@link https://r.je/dice.html#example3} for a description of the rules.
+        *
+        * @see Dice::addRule()
+        */
+       public function addRule(string $name, array $rule): void
+       {
+               $this->container = $this->container->addRule($name, $rule);
+       }
+
+       private function setupContainerForAddons(): void
+       {
+               /** @var ICanLoadAddons $addonLoader */
+               $addonLoader = $this->container->create(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
+       {
+               ErrorHandler::register($this->container->create(LoggerInterface::class));
+       }
+
+       private function registerTemplateEngine(): void
+       {
+               Renderer::registerTemplateEngine('Friendica\Render\FriendicaSmartyEngine');
+       }
+}
diff --git a/tests/Unit/Core/ContainerTest.php b/tests/Unit/Core/ContainerTest.php
deleted file mode 100644 (file)
index 5c56c69..0000000
+++ /dev/null
@@ -1,81 +0,0 @@
-<?php
-
-// Copyright (C) 2010-2024, the Friendica project
-// SPDX-FileCopyrightText: 2010-2024 the Friendica project
-//
-// SPDX-License-Identifier: AGPL-3.0-or-later
-
-declare(strict_types=1);
-
-namespace Core;
-
-use Dice\Dice;
-use Friendica\Core\Container;
-use org\bovigo\vfs\vfsStream;
-use PHPUnit\Framework\TestCase;
-use Psr\Log\LoggerInterface;
-use Psr\Log\NullLogger;
-
-class ContainerTest extends TestCase
-{
-       public function testFromBasePathReturnsContainer(): void
-       {
-               $root = vfsStream::setup('friendica', null, [
-                       'static' => [
-                               'dependencies.config.php' => '<?php return [];',
-                       ],
-               ]);
-
-               $container = Container::fromBasePath($root->url());
-
-               $this->assertInstanceOf(Container::class, $container);
-       }
-
-       public function testCreateReturnsObject(): void
-       {
-               $root = vfsStream::setup('friendica', null, [
-                       'static' => [
-                               'dependencies.config.php' => <<< PHP
-                                       <?php return [
-                                               \Psr\Log\LoggerInterface::class => [
-                                                       'instanceOf' => \Psr\Log\NullLogger::class,
-                                               ],
-                                       ];
-                                       PHP,
-                       ],
-               ]);
-
-               $container = Container::fromBasePath($root->url());
-
-               $this->assertInstanceOf(NullLogger::class, $container->create(LoggerInterface::class));
-       }
-
-       public function testFromDiceReturnsContainer(): void
-       {
-               $dice = $this->createMock(Dice::class);
-               $dice->expects($this->never())->method('create');
-
-               $container = Container::fromDice($dice);
-
-               $this->assertInstanceOf(Container::class, $container);
-       }
-
-       public function testCreateFromContainer(): void
-       {
-               $dice = $this->createMock(Dice::class);
-               $dice->expects($this->once())->method('create')->with(LoggerInterface::class)->willReturn(new NullLogger());
-
-               $container = Container::fromDice($dice);
-
-               $this->assertInstanceOf(NullLogger::class, $container->create(LoggerInterface::class));
-       }
-
-       public function testAddRuleFromContainer(): void
-       {
-               $dice = $this->createMock(Dice::class);
-               $dice->expects($this->once())->method('addRule')->with(LoggerInterface::class, ['constructParams' => ['console']])->willReturn($dice);
-
-               $container = Container::fromDice($dice);
-               $container->addRule(LoggerInterface::class, ['constructParams' => ['console']]);
-       }
-}
diff --git a/tests/Unit/Core/DiceContainerTest.php b/tests/Unit/Core/DiceContainerTest.php
new file mode 100644 (file)
index 0000000..d18c551
--- /dev/null
@@ -0,0 +1,82 @@
+<?php
+
+// Copyright (C) 2010-2024, the Friendica project
+// SPDX-FileCopyrightText: 2010-2024 the Friendica project
+//
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+declare(strict_types=1);
+
+namespace Core;
+
+use Dice\Dice;
+use Friendica\Core\Container;
+use Friendica\Core\DiceContainer;
+use org\bovigo\vfs\vfsStream;
+use PHPUnit\Framework\TestCase;
+use Psr\Log\LoggerInterface;
+use Psr\Log\NullLogger;
+
+class DiceContainerTest extends TestCase
+{
+       public function testFromBasePathReturnsContainer(): void
+       {
+               $root = vfsStream::setup('friendica', null, [
+                       'static' => [
+                               'dependencies.config.php' => '<?php return [];',
+                       ],
+               ]);
+
+               $container = DiceContainer::fromBasePath($root->url());
+
+               $this->assertInstanceOf(Container::class, $container);
+       }
+
+       public function testCreateReturnsObject(): void
+       {
+               $root = vfsStream::setup('friendica', null, [
+                       'static' => [
+                               'dependencies.config.php' => <<< PHP
+                                       <?php return [
+                                               \Psr\Log\LoggerInterface::class => [
+                                                       'instanceOf' => \Psr\Log\NullLogger::class,
+                                               ],
+                                       ];
+                                       PHP,
+                       ],
+               ]);
+
+               $container = DiceContainer::fromBasePath($root->url());
+
+               $this->assertInstanceOf(NullLogger::class, $container->create(LoggerInterface::class));
+       }
+
+       public function testFromDiceReturnsContainer(): void
+       {
+               $dice = $this->createMock(Dice::class);
+               $dice->expects($this->never())->method('create');
+
+               $container = DiceContainer::fromDice($dice);
+
+               $this->assertInstanceOf(Container::class, $container);
+       }
+
+       public function testCreateFromContainer(): void
+       {
+               $dice = $this->createMock(Dice::class);
+               $dice->expects($this->once())->method('create')->with(LoggerInterface::class)->willReturn(new NullLogger());
+
+               $container = DiceContainer::fromDice($dice);
+
+               $this->assertInstanceOf(NullLogger::class, $container->create(LoggerInterface::class));
+       }
+
+       public function testAddRuleFromContainer(): void
+       {
+               $dice = $this->createMock(Dice::class);
+               $dice->expects($this->once())->method('addRule')->with(LoggerInterface::class, ['constructParams' => ['console']])->willReturn($dice);
+
+               $container = DiceContainer::fromDice($dice);
+               $container->addRule(LoggerInterface::class, ['constructParams' => ['console']]);
+       }
+}