From 744bf9f97128e3f840c749aac3441005aaa716ee Mon Sep 17 00:00:00 2001 From: Art4 Date: Wed, 8 Jan 2025 22:07:16 +0000 Subject: [PATCH 01/21] Fix depreation message for old bin files --- src/Console/AbstractConsole.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Console/AbstractConsole.php b/src/Console/AbstractConsole.php index f9c413b915..0b8c1d6eb8 100644 --- a/src/Console/AbstractConsole.php +++ b/src/Console/AbstractConsole.php @@ -32,7 +32,7 @@ abstract class AbstractConsole extends Console */ protected function checkDeprecated(string $command): void { - if (substr($this->executable, -strlen(CoreConsole::getDefaultExecutable())) === CoreConsole::getDefaultExecutable()) { + if (substr($this->executable, -strlen(CoreConsole::getDefaultExecutable())) !== CoreConsole::getDefaultExecutable()) { $this->out(sprintf("'%s' is deprecated and will removed. Please use 'bin/console.php %s' instead", $this->executable, $command)); } } From 610267a3ea6cd909a1c0dbfec11f688dc4f9a66c Mon Sep 17 00:00:00 2001 From: Art4 Date: Wed, 8 Jan 2025 22:20:06 +0000 Subject: [PATCH 02/21] Move Console call into App class --- bin/console.php | 5 ++++- bin/daemon.php | 5 ++++- bin/jetstream.php | 5 ++++- bin/worker.php | 5 ++++- src/App.php | 5 +++++ 5 files changed, 21 insertions(+), 4 deletions(-) diff --git a/bin/console.php b/bin/console.php index 63412887d5..404f327868 100755 --- a/bin/console.php +++ b/bin/console.php @@ -20,4 +20,7 @@ require dirname(__DIR__) . '/vendor/autoload.php'; $dice = (new Dice())->addRules(require(dirname(__DIR__) . '/static/dependencies.config.php')); $container = \Friendica\Core\Container::fromDice($dice); -\Friendica\Core\Console::create($container, $_SERVER['argv'] ?? [])->execute(); + +$app = \Friendica\App::fromContainer($container); + +$app->processConsole($_SERVER['argv'] ?? []); diff --git a/bin/daemon.php b/bin/daemon.php index 18af315bc3..a1c0abbfaf 100755 --- a/bin/daemon.php +++ b/bin/daemon.php @@ -32,4 +32,7 @@ $argv = $_SERVER['argv'] ?? []; array_splice($argv, 1, 0, "daemon"); $container = \Friendica\Core\Container::fromDice($dice); -\Friendica\Core\Console::create($container, $argv)->execute(); + +$app = \Friendica\App::fromContainer($container); + +$app->processConsole($argv); diff --git a/bin/jetstream.php b/bin/jetstream.php index e3df667d3f..ac33e8ff2a 100755 --- a/bin/jetstream.php +++ b/bin/jetstream.php @@ -27,4 +27,7 @@ $argv = $_SERVER['argv'] ?? []; array_splice($argv, 1, 0, "jetstream"); $container = \Friendica\Core\Container::fromDice($dice); -\Friendica\Core\Console::create($container, $argv)->execute(); + +$app = \Friendica\App::fromContainer($container); + +$app->processConsole($argv); diff --git a/bin/worker.php b/bin/worker.php index a58d657a0f..fa238b66a2 100755 --- a/bin/worker.php +++ b/bin/worker.php @@ -29,4 +29,7 @@ $argv = $_SERVER['argv'] ?? []; array_splice($argv, 1, 0, "worker"); $container = \Friendica\Core\Container::fromDice($dice); -\Friendica\Core\Console::create($container, $argv)->execute(); + +$app = \Friendica\App::fromContainer($container); + +$app->processConsole($argv); diff --git a/src/App.php b/src/App.php index 487707f9fd..10c3603199 100644 --- a/src/App.php +++ b/src/App.php @@ -168,6 +168,11 @@ class App ); } + public function processConsole(array $argv): void + { + (\Friendica\Core\Console::create($this->container, $argv))->execute(); + } + public function processEjabberd(): void { $this->container->setup(LogChannel::AUTH_JABBERED, false); From 37ffeb81218f1ef13328ead46e7f7238cbb80004 Mon Sep 17 00:00:00 2001 From: Art4 Date: Wed, 8 Jan 2025 22:35:27 +0000 Subject: [PATCH 03/21] Move creation of Dice into Container class --- bin/auth_ejabberd.php | 11 ++++------- bin/console.php | 6 +----- bin/daemon.php | 6 +----- bin/jetstream.php | 6 +----- bin/worker.php | 6 +----- index.php | 6 +----- src/Core/Container.php | 11 ++++++++++- 7 files changed, 19 insertions(+), 33 deletions(-) diff --git a/bin/auth_ejabberd.php b/bin/auth_ejabberd.php index 68ee53a8b0..7a279b079b 100755 --- a/bin/auth_ejabberd.php +++ b/bin/auth_ejabberd.php @@ -44,15 +44,12 @@ if (php_sapi_name() !== 'cli') { exit(); } -use Dice\Dice; +chdir(dirname(__DIR__)); -chdir(dirname(__FILE__, 2)); +require dirname(__DIR__) . '/vendor/autoload.php'; -require dirname(__FILE__, 2) . '/vendor/autoload.php'; +$container = \Friendica\Core\Container::fromBasePath(dirname(__DIR__)); -$dice = (new Dice())->addRules(require(dirname(__FILE__, 2) . '/static/dependencies.config.php')); - -$container = \Friendica\Core\Container::fromDice($dice); -$app = \Friendica\App::fromContainer($container); +$app = \Friendica\App::fromContainer($container); $app->processEjabberd(); diff --git a/bin/console.php b/bin/console.php index 404f327868..94738dbf8c 100755 --- a/bin/console.php +++ b/bin/console.php @@ -13,13 +13,9 @@ if (php_sapi_name() !== 'cli') { exit(); } -use Dice\Dice; - require dirname(__DIR__) . '/vendor/autoload.php'; -$dice = (new Dice())->addRules(require(dirname(__DIR__) . '/static/dependencies.config.php')); - -$container = \Friendica\Core\Container::fromDice($dice); +$container = \Friendica\Core\Container::fromBasePath(dirname(__DIR__)); $app = \Friendica\App::fromContainer($container); diff --git a/bin/daemon.php b/bin/daemon.php index a1c0abbfaf..04e00ccf0d 100755 --- a/bin/daemon.php +++ b/bin/daemon.php @@ -19,19 +19,15 @@ if (php_sapi_name() !== 'cli') { exit(); } -use Dice\Dice; - // Ensure that daemon.php is executed from the base path of the installation chdir(dirname(__DIR__)); require dirname(__DIR__) . '/vendor/autoload.php'; -$dice = (new Dice())->addRules(require(dirname(__DIR__) . '/static/dependencies.config.php')); - $argv = $_SERVER['argv'] ?? []; array_splice($argv, 1, 0, "daemon"); -$container = \Friendica\Core\Container::fromDice($dice); +$container = \Friendica\Core\Container::fromBasePath(dirname(__DIR__)); $app = \Friendica\App::fromContainer($container); diff --git a/bin/jetstream.php b/bin/jetstream.php index ac33e8ff2a..72d13c3e1f 100755 --- a/bin/jetstream.php +++ b/bin/jetstream.php @@ -9,8 +9,6 @@ * @deprecated 2025.02 use bin/console.php jetstream instead */ -use Dice\Dice; - if (php_sapi_name() !== 'cli') { header($_SERVER["SERVER_PROTOCOL"] . ' 403 Forbidden'); exit(); @@ -21,12 +19,10 @@ chdir(dirname(__DIR__)); require dirname(__DIR__) . '/vendor/autoload.php'; -$dice = (new Dice())->addRules(require(dirname(__DIR__) . '/static/dependencies.config.php')); - $argv = $_SERVER['argv'] ?? []; array_splice($argv, 1, 0, "jetstream"); -$container = \Friendica\Core\Container::fromDice($dice); +$container = \Friendica\Core\Container::fromBasePath(dirname(__DIR__)); $app = \Friendica\App::fromContainer($container); diff --git a/bin/worker.php b/bin/worker.php index fa238b66a2..54b36e392f 100755 --- a/bin/worker.php +++ b/bin/worker.php @@ -16,19 +16,15 @@ if (php_sapi_name() !== 'cli') { exit(); } -use Dice\Dice; - // Ensure that worker.php is executed from the base path of the installation chdir(dirname(__DIR__)); require dirname(__DIR__) . '/vendor/autoload.php'; -$dice = (new Dice())->addRules(require(dirname(__DIR__) . '/static/dependencies.config.php')); - $argv = $_SERVER['argv'] ?? []; array_splice($argv, 1, 0, "worker"); -$container = \Friendica\Core\Container::fromDice($dice); +$container = \Friendica\Core\Container::fromBasePath(dirname(__DIR__)); $app = \Friendica\App::fromContainer($container); diff --git a/index.php b/index.php index 6654dcb515..906d91bc3c 100644 --- a/index.php +++ b/index.php @@ -5,8 +5,6 @@ // // SPDX-License-Identifier: AGPL-3.0-or-later -use Dice\Dice; - $start_time = microtime(true); if (!file_exists(__DIR__ . '/vendor/autoload.php')) { @@ -17,9 +15,7 @@ require __DIR__ . '/vendor/autoload.php'; $request = \GuzzleHttp\Psr7\ServerRequest::fromGlobals(); -$dice = (new Dice())->addRules(require(__DIR__ . '/static/dependencies.config.php')); - -$container = \Friendica\Core\Container::fromDice($dice); +$container = \Friendica\Core\Container::fromBasePath(__DIR__); $app = \Friendica\App::fromContainer($container); $app->processRequest($request, $start_time); diff --git a/src/Core/Container.php b/src/Core/Container.php index 12a9b625a7..0da94935d8 100644 --- a/src/Core/Container.php +++ b/src/Core/Container.php @@ -21,9 +21,18 @@ use Psr\Log\LoggerInterface; */ class 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; - protected function __construct(Dice $container) + private function __construct(Dice $container) { $this->container = $container; } From 316fac0161b4d02a094fa523c2176fb4b843f0aa Mon Sep 17 00:00:00 2001 From: Art4 Date: Wed, 8 Jan 2025 22:52:30 +0000 Subject: [PATCH 04/21] Add tests for Container::fromBasePath() --- tests/Unit/Core/ContainerTest.php | 33 +++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/tests/Unit/Core/ContainerTest.php b/tests/Unit/Core/ContainerTest.php index bb5ccda398..5c56c696be 100644 --- a/tests/Unit/Core/ContainerTest.php +++ b/tests/Unit/Core/ContainerTest.php @@ -11,12 +11,45 @@ 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' => 'url()); + + $this->assertInstanceOf(Container::class, $container); + } + + public function testCreateReturnsObject(): void + { + $root = vfsStream::setup('friendica', null, [ + 'static' => [ + 'dependencies.config.php' => <<< PHP + [ + '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); From 320fe18654dfbde864ac9a84959059faa10b5adb Mon Sep 17 00:00:00 2001 From: Art4 Date: Wed, 8 Jan 2025 22:59:29 +0000 Subject: [PATCH 05/21] Create Container interface, add DiceContainer as implementation --- bin/auth_ejabberd.php | 2 +- bin/console.php | 2 +- bin/daemon.php | 2 +- bin/jetstream.php | 2 +- bin/worker.php | 2 +- index.php | 5 +- src/Core/Container.php | 89 +----------- src/Core/DiceContainer.php | 127 ++++++++++++++++++ ...ontainerTest.php => DiceContainerTest.php} | 13 +- 9 files changed, 147 insertions(+), 97 deletions(-) create mode 100644 src/Core/DiceContainer.php rename tests/Unit/Core/{ContainerTest.php => DiceContainerTest.php} (84%) diff --git a/bin/auth_ejabberd.php b/bin/auth_ejabberd.php index 7a279b079b..bc976e61a4 100755 --- a/bin/auth_ejabberd.php +++ b/bin/auth_ejabberd.php @@ -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); diff --git a/bin/console.php b/bin/console.php index 94738dbf8c..ad612f4363 100755 --- a/bin/console.php +++ b/bin/console.php @@ -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); diff --git a/bin/daemon.php b/bin/daemon.php index 04e00ccf0d..546f207e08 100755 --- a/bin/daemon.php +++ b/bin/daemon.php @@ -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); diff --git a/bin/jetstream.php b/bin/jetstream.php index 72d13c3e1f..156f961856 100755 --- a/bin/jetstream.php +++ b/bin/jetstream.php @@ -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); diff --git a/bin/worker.php b/bin/worker.php index 54b36e392f..a79d8836a7 100755 --- a/bin/worker.php +++ b/bin/worker.php @@ -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); diff --git a/index.php b/index.php index 906d91bc3c..850d9cd1c0 100644 --- 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); diff --git a/src/Core/Container.php b/src/Core/Container.php index 0da94935d8..f0f44f38db 100644 --- a/src/Core/Container.php +++ b/src/Core/Container.php @@ -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 index 0000000000..7d8b57b14d --- /dev/null +++ b/src/Core/DiceContainer.php @@ -0,0 +1,127 @@ +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/DiceContainerTest.php similarity index 84% rename from tests/Unit/Core/ContainerTest.php rename to tests/Unit/Core/DiceContainerTest.php index 5c56c696be..d18c551da1 100644 --- a/tests/Unit/Core/ContainerTest.php +++ b/tests/Unit/Core/DiceContainerTest.php @@ -11,12 +11,13 @@ 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 ContainerTest extends TestCase +class DiceContainerTest extends TestCase { public function testFromBasePathReturnsContainer(): void { @@ -26,7 +27,7 @@ class ContainerTest extends TestCase ], ]); - $container = Container::fromBasePath($root->url()); + $container = DiceContainer::fromBasePath($root->url()); $this->assertInstanceOf(Container::class, $container); } @@ -45,7 +46,7 @@ class ContainerTest extends TestCase ], ]); - $container = Container::fromBasePath($root->url()); + $container = DiceContainer::fromBasePath($root->url()); $this->assertInstanceOf(NullLogger::class, $container->create(LoggerInterface::class)); } @@ -55,7 +56,7 @@ class ContainerTest extends TestCase $dice = $this->createMock(Dice::class); $dice->expects($this->never())->method('create'); - $container = Container::fromDice($dice); + $container = DiceContainer::fromDice($dice); $this->assertInstanceOf(Container::class, $container); } @@ -65,7 +66,7 @@ class ContainerTest extends TestCase $dice = $this->createMock(Dice::class); $dice->expects($this->once())->method('create')->with(LoggerInterface::class)->willReturn(new NullLogger()); - $container = Container::fromDice($dice); + $container = DiceContainer::fromDice($dice); $this->assertInstanceOf(NullLogger::class, $container->create(LoggerInterface::class)); } @@ -75,7 +76,7 @@ class ContainerTest extends TestCase $dice = $this->createMock(Dice::class); $dice->expects($this->once())->method('addRule')->with(LoggerInterface::class, ['constructParams' => ['console']])->willReturn($dice); - $container = Container::fromDice($dice); + $container = DiceContainer::fromDice($dice); $container->addRule(LoggerInterface::class, ['constructParams' => ['console']]); } } From c762c62029295928d2dbb781edcb88d91affb843 Mon Sep 17 00:00:00 2001 From: Art4 Date: Wed, 8 Jan 2025 23:00:38 +0000 Subject: [PATCH 06/21] Remove DiceContainer::fromDice() --- src/Core/DiceContainer.php | 14 +------------ tests/Unit/Core/DiceContainerTest.php | 29 --------------------------- 2 files changed, 1 insertion(+), 42 deletions(-) diff --git a/src/Core/DiceContainer.php b/src/Core/DiceContainer.php index 7d8b57b14d..8e0a95b472 100644 --- a/src/Core/DiceContainer.php +++ b/src/Core/DiceContainer.php @@ -27,7 +27,7 @@ final class DiceContainer implements Container $dice = (new Dice())->addRules(require($path)); - return static::fromDice($dice); + return new self($dice); } private Dice $container; @@ -37,18 +37,6 @@ final class DiceContainer implements 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 * diff --git a/tests/Unit/Core/DiceContainerTest.php b/tests/Unit/Core/DiceContainerTest.php index d18c551da1..0b6040029e 100644 --- a/tests/Unit/Core/DiceContainerTest.php +++ b/tests/Unit/Core/DiceContainerTest.php @@ -50,33 +50,4 @@ class DiceContainerTest extends TestCase $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']]); - } } From 57c96b89a1afe188e77e1516ae485cf75610e4f4 Mon Sep 17 00:00:00 2001 From: Art4 Date: Wed, 8 Jan 2025 23:10:31 +0000 Subject: [PATCH 07/21] Remove unused import --- tests/Unit/Core/DiceContainerTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Unit/Core/DiceContainerTest.php b/tests/Unit/Core/DiceContainerTest.php index 0b6040029e..93e688c424 100644 --- a/tests/Unit/Core/DiceContainerTest.php +++ b/tests/Unit/Core/DiceContainerTest.php @@ -9,7 +9,6 @@ declare(strict_types=1); namespace Core; -use Dice\Dice; use Friendica\Core\Container; use Friendica\Core\DiceContainer; use org\bovigo\vfs\vfsStream; From e8991271378c39896471c87658540b456242827c Mon Sep 17 00:00:00 2001 From: Art4 Date: Thu, 9 Jan 2025 08:11:38 +0000 Subject: [PATCH 08/21] Deprecate Container::setup() --- src/Core/Container.php | 2 ++ src/Core/DiceContainer.php | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/Core/Container.php b/src/Core/Container.php index f0f44f38db..42ed4ae83f 100644 --- a/src/Core/Container.php +++ b/src/Core/Container.php @@ -19,6 +19,8 @@ interface Container /** * Initialize the container with the given parameters * + * @deprecated + * * @param string $logChannel The Log Channel of this call * @param bool $withTemplateEngine true, if the template engine should be set too * diff --git a/src/Core/DiceContainer.php b/src/Core/DiceContainer.php index 8e0a95b472..d2abdc58fa 100644 --- a/src/Core/DiceContainer.php +++ b/src/Core/DiceContainer.php @@ -40,6 +40,8 @@ final class DiceContainer implements Container /** * Initialize the container with the given parameters * + * @deprecated + * * @param string $logChannel The Log Channel of this call * @param bool $withTemplateEngine true, if the template engine should be set too * From 768d7961a65ffbdb89f86eafa765062f7d313c48 Mon Sep 17 00:00:00 2001 From: Art4 Date: Thu, 9 Jan 2025 08:17:43 +0000 Subject: [PATCH 09/21] Move setupContainerForAddons() into App --- src/App.php | 17 +++++++++++++++++ src/Core/DiceContainer.php | 9 --------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/App.php b/src/App.php index 10c3603199..8c22a42a06 100644 --- a/src/App.php +++ b/src/App.php @@ -17,6 +17,7 @@ use Friendica\App\Router; use Friendica\Capabilities\ICanCreateResponses; use Friendica\Capabilities\ICanHandleRequests; use Friendica\Content\Nav; +use Friendica\Core\Addon\Capability\ICanLoadAddons; use Friendica\Core\Config\Factory\Config; use Friendica\Core\Container; use Friendica\Core\Renderer; @@ -134,6 +135,8 @@ class App ], ]); + $this->setupContainerForAddons(); + $this->container->setup(LogChannel::APP, false); $this->requestId = $this->container->create(Request::class)->getRequestId(); @@ -170,11 +173,15 @@ class App public function processConsole(array $argv): void { + $this->setupContainerForAddons(); + (\Friendica\Core\Console::create($this->container, $argv))->execute(); } public function processEjabberd(): void { + $this->setupContainerForAddons(); + $this->container->setup(LogChannel::AUTH_JABBERED, false); /** @var BasePath */ @@ -192,6 +199,16 @@ class App } } + private function setupContainerForAddons(): void + { + /** @var ICanLoadAddons $addonLoader */ + $addonLoader = $this->container->create(ICanLoadAddons::class); + + foreach ($addonLoader->getActiveAddonConfig('dependencies') as $name => $rule) { + $this->container->addRule($name, $rule); + } + } + private function registerTemplateEngine(): void { Renderer::registerTemplateEngine('Friendica\Render\FriendicaSmartyEngine'); diff --git a/src/Core/DiceContainer.php b/src/Core/DiceContainer.php index d2abdc58fa..8b7f9472d7 100644 --- a/src/Core/DiceContainer.php +++ b/src/Core/DiceContainer.php @@ -49,7 +49,6 @@ final class DiceContainer implements Container */ public function setup(string $logChannel = LogChannel::DEFAULT, bool $withTemplateEngine = true): void { - $this->setupContainerForAddons(); $this->setupContainerForLogger($logChannel); $this->setupLegacyServiceLocator(); $this->registerErrorHandler(); @@ -85,14 +84,6 @@ final class DiceContainer implements Container $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, [ From 04fb9dd9e08f8e94115fa271d3cfd92df905f931 Mon Sep 17 00:00:00 2001 From: Art4 Date: Thu, 9 Jan 2025 08:36:47 +0000 Subject: [PATCH 10/21] Move registerTemplateEngine() into App class --- src/App.php | 4 ++++ src/Core/Container.php | 3 +-- src/Core/DiceContainer.php | 13 +------------ 3 files changed, 6 insertions(+), 14 deletions(-) diff --git a/src/App.php b/src/App.php index 8c22a42a06..0b86e3caae 100644 --- a/src/App.php +++ b/src/App.php @@ -175,6 +175,8 @@ class App { $this->setupContainerForAddons(); + $this->registerTemplateEngine(); + (\Friendica\Core\Console::create($this->container, $argv))->execute(); } @@ -184,6 +186,8 @@ class App $this->container->setup(LogChannel::AUTH_JABBERED, false); + $this->registerTemplateEngine(); + /** @var BasePath */ $basePath = $this->container->create(BasePath::class); diff --git a/src/Core/Container.php b/src/Core/Container.php index 42ed4ae83f..44d770c644 100644 --- a/src/Core/Container.php +++ b/src/Core/Container.php @@ -22,11 +22,10 @@ interface Container * @deprecated * * @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; + public function setup(string $logChannel = LogChannel::DEFAULT): void; /** * Returns a fully constructed object based on $name using $args and $share as constructor arguments if supplied diff --git a/src/Core/DiceContainer.php b/src/Core/DiceContainer.php index 8b7f9472d7..e5c69c4982 100644 --- a/src/Core/DiceContainer.php +++ b/src/Core/DiceContainer.php @@ -10,7 +10,6 @@ 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; @@ -43,19 +42,14 @@ final class DiceContainer implements Container * @deprecated * * @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 + public function setup(string $logChannel = LogChannel::DEFAULT): void { $this->setupContainerForLogger($logChannel); $this->setupLegacyServiceLocator(); $this->registerErrorHandler(); - - if ($withTemplateEngine) { - $this->registerTemplateEngine(); - } } /** @@ -100,9 +94,4 @@ final class DiceContainer implements Container { ErrorHandler::register($this->container->create(LoggerInterface::class)); } - - private function registerTemplateEngine(): void - { - Renderer::registerTemplateEngine('Friendica\Render\FriendicaSmartyEngine'); - } } From 633c692083f509dd92bc7611550abd6f2b5cb7ba Mon Sep 17 00:00:00 2001 From: Art4 Date: Thu, 9 Jan 2025 08:53:07 +0000 Subject: [PATCH 11/21] Move registerErrorHandler() into App class --- src/App.php | 16 +++++++++++++--- src/Core/DiceContainer.php | 7 ------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/App.php b/src/App.php index 0b86e3caae..aa6dc51234 100644 --- a/src/App.php +++ b/src/App.php @@ -29,6 +29,7 @@ use Friendica\Security\Authentication; use Friendica\Core\Config\Capability\IManageConfigValues; use Friendica\Core\L10n; use Friendica\Core\Logger\Capability\LogChannel; +use Friendica\Core\Logger\Handler\ErrorHandler; use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues; use Friendica\Core\System; use Friendica\Core\Update; @@ -137,7 +138,9 @@ class App $this->setupContainerForAddons(); - $this->container->setup(LogChannel::APP, false); + $this->container->setup(LogChannel::APP); + + $this->registerErrorHandler(); $this->requestId = $this->container->create(Request::class)->getRequestId(); $this->auth = $this->container->create(Authentication::class); @@ -175,6 +178,8 @@ class App { $this->setupContainerForAddons(); + $this->registerErrorHandler(); + $this->registerTemplateEngine(); (\Friendica\Core\Console::create($this->container, $argv))->execute(); @@ -184,9 +189,9 @@ class App { $this->setupContainerForAddons(); - $this->container->setup(LogChannel::AUTH_JABBERED, false); + $this->container->setup(LogChannel::AUTH_JABBERED); - $this->registerTemplateEngine(); + $this->registerErrorHandler(); /** @var BasePath */ $basePath = $this->container->create(BasePath::class); @@ -213,6 +218,11 @@ class App } } + private function registerErrorHandler(): void + { + ErrorHandler::register($this->container->create(LoggerInterface::class)); + } + private function registerTemplateEngine(): void { Renderer::registerTemplateEngine('Friendica\Render\FriendicaSmartyEngine'); diff --git a/src/Core/DiceContainer.php b/src/Core/DiceContainer.php index e5c69c4982..bba5a18ab6 100644 --- a/src/Core/DiceContainer.php +++ b/src/Core/DiceContainer.php @@ -11,7 +11,6 @@ namespace Friendica\Core; use Dice\Dice; use Friendica\Core\Logger\Capability\LogChannel; -use Friendica\Core\Logger\Handler\ErrorHandler; use Friendica\DI; use Psr\Log\LoggerInterface; @@ -49,7 +48,6 @@ final class DiceContainer implements Container { $this->setupContainerForLogger($logChannel); $this->setupLegacyServiceLocator(); - $this->registerErrorHandler(); } /** @@ -89,9 +87,4 @@ final class DiceContainer implements Container { DI::init($this->container); } - - private function registerErrorHandler(): void - { - ErrorHandler::register($this->container->create(LoggerInterface::class)); - } } From 08a50efb08be64e6a5f821f009a3e58fadf98d46 Mon Sep 17 00:00:00 2001 From: Art4 Date: Thu, 9 Jan 2025 09:08:14 +0000 Subject: [PATCH 12/21] remove unused Logger::devLog() method --- src/Core/Logger.php | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/src/Core/Logger.php b/src/Core/Logger.php index 48fd1293b3..f89044aa6d 100644 --- a/src/Core/Logger.php +++ b/src/Core/Logger.php @@ -191,21 +191,4 @@ class Logger { self::getInstance()->debug($message, $context); } - - /** - * An alternative logger for development. - * - * Works largely as log() but allows developers - * to isolate particular elements they are targeting - * personally without background noise - * - * @param string $message Message to log - * @param string $level Logging level - * @return void - * @throws \Exception - */ - public static function devLog(string $message, string $level = LogLevel::DEBUG) - { - DI::devLogger()->log($level, $message); - } } From 08691407565dde3afb733f5d31dcb3a0a2dbd65a Mon Sep 17 00:00:00 2001 From: Art4 Date: Thu, 9 Jan 2025 09:10:09 +0000 Subject: [PATCH 13/21] remove unused DI::devLogger() method --- src/DI.php | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/DI.php b/src/DI.php index 9381d6a782..ea1aafc8f9 100644 --- a/src/DI.php +++ b/src/DI.php @@ -323,14 +323,6 @@ abstract class DI return self::$dice->create(LoggerInterface::class); } - /** - * @return LoggerInterface - */ - public static function devLogger() - { - return self::$dice->create('$devLogger'); - } - /** * @return \Friendica\Core\Logger\Type\WorkerLogger */ From 27fa4c0cdff12458c7101254785d70566e2634d6 Mon Sep 17 00:00:00 2001 From: Art4 Date: Thu, 9 Jan 2025 09:16:52 +0000 Subject: [PATCH 14/21] Remove Dice rules for unused $devLogger service --- src/DI.php | 4 ++-- static/dependencies.config.php | 6 ------ tests/functional/DependencyCheckTest.php | 12 ------------ 3 files changed, 2 insertions(+), 20 deletions(-) diff --git a/src/DI.php b/src/DI.php index ea1aafc8f9..72be1f3aaa 100644 --- a/src/DI.php +++ b/src/DI.php @@ -305,8 +305,8 @@ abstract class DI public static function flushLogger() { $flushDice = self::$dice - ->addRule(LoggerInterface::class, self::$dice->getRule(LoggerInterface::class)) - ->addRule('$devLogger', self::$dice->getRule('$devLogger')); + ->addRule(LoggerInterface::class, self::$dice->getRule(LoggerInterface::class)); + static::init($flushDice); } diff --git a/static/dependencies.config.php b/static/dependencies.config.php index 28ae277c21..5d1edfea22 100644 --- a/static/dependencies.config.php +++ b/static/dependencies.config.php @@ -180,12 +180,6 @@ return (function(string $basepath, array $getVars, array $serverVars, array $coo \Friendica\Core\Logger\Capability\IHaveCallIntrospections::IGNORE_CLASS_LIST, ], ], - '$devLogger' => [ - 'instanceOf' => \Friendica\Core\Logger\Factory\StreamLogger::class, - 'call' => [ - ['createDev', [], Dice::CHAIN_CALL], - ], - ], \Friendica\Core\Cache\Capability\ICanCache::class => [ 'instanceOf' => \Friendica\Core\Cache\Factory\Cache::class, 'call' => [ diff --git a/tests/functional/DependencyCheckTest.php b/tests/functional/DependencyCheckTest.php index 866380fbdf..e06450e754 100644 --- a/tests/functional/DependencyCheckTest.php +++ b/tests/functional/DependencyCheckTest.php @@ -118,18 +118,6 @@ class DependencyCheckTest extends FixtureTestCase self::assertInstanceOf(LoggerInterface::class, $logger); } - public function testDevLogger() - { - /** @var IManageConfigValues $config */ - $config = $this->dice->create(IManageConfigValues::class); - $config->set('system', 'dlogfile', $this->root->url() . '/friendica.log'); - - /** @var LoggerInterface $logger */ - $logger = $this->dice->create('$devLogger', ['dev']); - - self::assertInstanceOf(LoggerInterface::class, $logger); - } - public function testCache() { /** @var ICanCache $cache */ From f40fb6b099b73c1d722522ef0b7fe6e13074ce63 Mon Sep 17 00:00:00 2001 From: Art4 Date: Thu, 9 Jan 2025 09:21:10 +0000 Subject: [PATCH 15/21] Fix code style --- src/Core/Logger.php | 1 - tests/functional/DependencyCheckTest.php | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Core/Logger.php b/src/Core/Logger.php index f89044aa6d..c15271e841 100644 --- a/src/Core/Logger.php +++ b/src/Core/Logger.php @@ -10,7 +10,6 @@ namespace Friendica\Core; use Friendica\DI; use Friendica\Core\Logger\Type\WorkerLogger; use Psr\Log\LoggerInterface; -use Psr\Log\LogLevel; /** * Logger functions diff --git a/tests/functional/DependencyCheckTest.php b/tests/functional/DependencyCheckTest.php index e06450e754..ec27d205d6 100644 --- a/tests/functional/DependencyCheckTest.php +++ b/tests/functional/DependencyCheckTest.php @@ -21,7 +21,7 @@ use Psr\Log\LoggerInterface; class DependencyCheckTest extends FixtureTestCase { - protected function setUp() : void + protected function setUp(): void { parent::setUp(); From 870b3b9a1cf13e7905342d84c477671f40513c78 Mon Sep 17 00:00:00 2001 From: Art4 Date: Thu, 9 Jan 2025 09:34:43 +0000 Subject: [PATCH 16/21] Remove default logchannel from Container::setup() --- src/Core/Container.php | 4 +--- src/Core/DiceContainer.php | 3 +-- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/src/Core/Container.php b/src/Core/Container.php index 44d770c644..84f8564a87 100644 --- a/src/Core/Container.php +++ b/src/Core/Container.php @@ -9,8 +9,6 @@ declare(strict_types=1); namespace Friendica\Core; -use Friendica\Core\Logger\Capability\LogChannel; - /** * Dependency Injection Container */ @@ -25,7 +23,7 @@ interface Container * * @return void */ - public function setup(string $logChannel = LogChannel::DEFAULT): void; + public function setup(string $logChannel): void; /** * Returns a fully constructed object based on $name using $args and $share as constructor arguments if supplied diff --git a/src/Core/DiceContainer.php b/src/Core/DiceContainer.php index bba5a18ab6..2c1f5faba7 100644 --- a/src/Core/DiceContainer.php +++ b/src/Core/DiceContainer.php @@ -10,7 +10,6 @@ declare(strict_types=1); namespace Friendica\Core; use Dice\Dice; -use Friendica\Core\Logger\Capability\LogChannel; use Friendica\DI; use Psr\Log\LoggerInterface; @@ -44,7 +43,7 @@ final class DiceContainer implements Container * * @return void */ - public function setup(string $logChannel = LogChannel::DEFAULT): void + public function setup(string $logChannel): void { $this->setupContainerForLogger($logChannel); $this->setupLegacyServiceLocator(); From 7f643aadd87965ab1f24da898f91c266ed98908c Mon Sep 17 00:00:00 2001 From: Art4 Date: Thu, 9 Jan 2025 10:13:33 +0000 Subject: [PATCH 17/21] Move setupContainerForLogger() and determine log channel into App class --- src/App.php | 36 ++++++++++++++++++++++++++++++++++-- src/Core/Console.php | 6 ------ src/Core/Container.php | 4 +--- src/Core/DiceContainer.php | 13 +------------ 4 files changed, 36 insertions(+), 23 deletions(-) diff --git a/src/App.php b/src/App.php index aa6dc51234..17254bb966 100644 --- a/src/App.php +++ b/src/App.php @@ -138,7 +138,9 @@ class App $this->setupContainerForAddons(); - $this->container->setup(LogChannel::APP); + $this->setupContainerForLogger(LogChannel::APP); + + $this->container->setup(); $this->registerErrorHandler(); @@ -178,6 +180,10 @@ class App { $this->setupContainerForAddons(); + $this->setupContainerForLogger($this->determineLogChannel($argv)); + + $this->container->setup(); + $this->registerErrorHandler(); $this->registerTemplateEngine(); @@ -189,7 +195,9 @@ class App { $this->setupContainerForAddons(); - $this->container->setup(LogChannel::AUTH_JABBERED); + $this->setupContainerForLogger(LogChannel::AUTH_JABBERED); + + $this->container->setup(); $this->registerErrorHandler(); @@ -218,6 +226,30 @@ class App } } + private function determineLogChannel(array $argv): string + { + $command = strtolower($argv[1]) ?? ''; + + if ($command === 'daemon' || $command === 'jetstream') { + return LogChannel::DAEMON; + } + + if ($command === 'worker') { + return LogChannel::WORKER; + } + + // @TODO Add support for jetstream + + return LogChannel::CONSOLE; + } + + private function setupContainerForLogger(string $logChannel): void + { + $this->container->addRule(LoggerInterface::class, [ + 'constructParams' => [$logChannel], + ]); + } + private function registerErrorHandler(): void { ErrorHandler::register($this->container->create(LoggerInterface::class)); diff --git a/src/Core/Console.php b/src/Core/Console.php index d138a9dfa9..67844f6033 100644 --- a/src/Core/Console.php +++ b/src/Core/Console.php @@ -187,12 +187,6 @@ 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->container->create($className, [$subargs]); diff --git a/src/Core/Container.php b/src/Core/Container.php index 84f8564a87..8d69799122 100644 --- a/src/Core/Container.php +++ b/src/Core/Container.php @@ -19,11 +19,9 @@ interface Container * * @deprecated * - * @param string $logChannel The Log Channel of this call - * * @return void */ - public function setup(string $logChannel): void; + public function setup(): void; /** * Returns a fully constructed object based on $name using $args and $share as constructor arguments if supplied diff --git a/src/Core/DiceContainer.php b/src/Core/DiceContainer.php index 2c1f5faba7..778192388b 100644 --- a/src/Core/DiceContainer.php +++ b/src/Core/DiceContainer.php @@ -11,7 +11,6 @@ namespace Friendica\Core; use Dice\Dice; use Friendica\DI; -use Psr\Log\LoggerInterface; /** * Wrapper for the Dice class to make some basic setups @@ -39,13 +38,10 @@ final class DiceContainer implements Container * * @deprecated * - * @param string $logChannel The Log Channel of this call - * * @return void */ - public function setup(string $logChannel): void + public function setup(): void { - $this->setupContainerForLogger($logChannel); $this->setupLegacyServiceLocator(); } @@ -75,13 +71,6 @@ final class DiceContainer implements Container $this->container = $this->container->addRule($name, $rule); } - private function setupContainerForLogger(string $logChannel): void - { - $this->container = $this->container->addRule(LoggerInterface::class, [ - 'constructParams' => [$logChannel], - ]); - } - private function setupLegacyServiceLocator(): void { DI::init($this->container); From 2f3ef5359f89d30fb0309ab3b9e4829ac6385297 Mon Sep 17 00:00:00 2001 From: Art4 Date: Thu, 9 Jan 2025 10:18:11 +0000 Subject: [PATCH 18/21] Remove unused import --- src/Core/Console.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Core/Console.php b/src/Core/Console.php index 67844f6033..5dec5be257 100644 --- a/src/Core/Console.php +++ b/src/Core/Console.php @@ -9,7 +9,6 @@ namespace Friendica\Core; use Friendica; use Friendica\App; -use Friendica\Core\Logger\Capability\LogChannel; /** * Description of Console From f1dba9f89e43026082ac0ca58c453862b5373bf0 Mon Sep 17 00:00:00 2001 From: Art4 Date: Thu, 9 Jan 2025 13:34:10 +0000 Subject: [PATCH 19/21] Move setupLegacyServiceLocator() into App class --- src/App.php | 14 +++++++++++--- src/Core/DiceContainer.php | 16 ++++++++++++---- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/App.php b/src/App.php index 17254bb966..c1ac8b2d43 100644 --- a/src/App.php +++ b/src/App.php @@ -27,6 +27,7 @@ use Friendica\Database\Definition\ViewDefinition; use Friendica\Module\Maintenance; use Friendica\Security\Authentication; use Friendica\Core\Config\Capability\IManageConfigValues; +use Friendica\Core\DiceContainer; use Friendica\Core\L10n; use Friendica\Core\Logger\Capability\LogChannel; use Friendica\Core\Logger\Handler\ErrorHandler; @@ -140,7 +141,7 @@ class App $this->setupContainerForLogger(LogChannel::APP); - $this->container->setup(); + $this->setupLegacyServiceLocator(); $this->registerErrorHandler(); @@ -182,7 +183,7 @@ class App $this->setupContainerForLogger($this->determineLogChannel($argv)); - $this->container->setup(); + $this->setupLegacyServiceLocator(); $this->registerErrorHandler(); @@ -197,7 +198,7 @@ class App $this->setupContainerForLogger(LogChannel::AUTH_JABBERED); - $this->container->setup(); + $this->setupLegacyServiceLocator(); $this->registerErrorHandler(); @@ -250,6 +251,13 @@ class App ]); } + private function setupLegacyServiceLocator(): void + { + if ($this->container instanceof DiceContainer) { + DI::init($this->container->getDice()); + } + } + private function registerErrorHandler(): void { ErrorHandler::register($this->container->create(LoggerInterface::class)); diff --git a/src/Core/DiceContainer.php b/src/Core/DiceContainer.php index 778192388b..acbb0862b9 100644 --- a/src/Core/DiceContainer.php +++ b/src/Core/DiceContainer.php @@ -10,7 +10,6 @@ declare(strict_types=1); namespace Friendica\Core; use Dice\Dice; -use Friendica\DI; /** * Wrapper for the Dice class to make some basic setups @@ -42,7 +41,7 @@ final class DiceContainer implements Container */ public function setup(): void { - $this->setupLegacyServiceLocator(); + // this method can be removed } /** @@ -71,8 +70,17 @@ final class DiceContainer implements Container $this->container = $this->container->addRule($name, $rule); } - private function setupLegacyServiceLocator(): void + /** + * Only used to inject Dice into DI class + * + * @see \Friendica\DI + * + * @internal + * + * @deprecated + */ + public function getDice(): Dice { - DI::init($this->container); + return $this->container; } } From 2ef943394d7d824f05198bc96de86eaecc163ddb Mon Sep 17 00:00:00 2001 From: Art4 Date: Thu, 9 Jan 2025 13:35:03 +0000 Subject: [PATCH 20/21] Remove Container::setup() --- src/Core/Container.php | 9 --------- src/Core/DiceContainer.php | 12 ------------ 2 files changed, 21 deletions(-) diff --git a/src/Core/Container.php b/src/Core/Container.php index 8d69799122..1017590a85 100644 --- a/src/Core/Container.php +++ b/src/Core/Container.php @@ -14,15 +14,6 @@ namespace Friendica\Core; */ interface Container { - /** - * Initialize the container with the given parameters - * - * @deprecated - * - * @return void - */ - public function setup(): void; - /** * 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 diff --git a/src/Core/DiceContainer.php b/src/Core/DiceContainer.php index acbb0862b9..304c1e411d 100644 --- a/src/Core/DiceContainer.php +++ b/src/Core/DiceContainer.php @@ -32,18 +32,6 @@ final class DiceContainer implements Container $this->container = $container; } - /** - * Initialize the container with the given parameters - * - * @deprecated - * - * @return void - */ - public function setup(): void - { - // this method can be removed - } - /** * 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 From 7107acb15cef2e0c4187dbf3573e13aa33623a86 Mon Sep 17 00:00:00 2001 From: Art4 Date: Thu, 9 Jan 2025 19:53:47 +0000 Subject: [PATCH 21/21] Fix namespace for DiceContainer test --- tests/Unit/Core/DiceContainerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Unit/Core/DiceContainerTest.php b/tests/Unit/Core/DiceContainerTest.php index 93e688c424..85d64de728 100644 --- a/tests/Unit/Core/DiceContainerTest.php +++ b/tests/Unit/Core/DiceContainerTest.php @@ -7,7 +7,7 @@ declare(strict_types=1); -namespace Core; +namespace Friendica\Test\Unit\Core; use Friendica\Core\Container; use Friendica\Core\DiceContainer;