From 1c7446bf9a72b51abab64475656418e260431cad Mon Sep 17 00:00:00 2001 From: Art4 Date: Wed, 18 Dec 2024 22:16:34 +0000 Subject: [PATCH 01/15] Create App::fromDice() factory method --- index.php | 2 +- src/App.php | 25 +++++++++++++++++++++++-- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/index.php b/index.php index 09620ab4c1..d3483cb7cd 100644 --- a/index.php +++ b/index.php @@ -25,7 +25,7 @@ $dice = $dice->addRule(Friendica\App\Mode::class, ['call' => [['determineRunMode \Friendica\Core\Logger\Handler\ErrorHandler::register($dice->create(\Psr\Log\LoggerInterface::class)); -$a = \Friendica\DI::app(); +$a = \Friendica\App::fromDice($dice); \Friendica\DI::mode()->setExecutor(\Friendica\App\Mode::INDEX); diff --git a/src/App.php b/src/App.php index d3ff15bada..2fedaa24e8 100644 --- a/src/App.php +++ b/src/App.php @@ -7,6 +7,7 @@ namespace Friendica; +use Dice\Dice; use Friendica\App\Arguments; use Friendica\App\BaseURL; use Friendica\App\Mode; @@ -51,6 +52,25 @@ class App const CODENAME = 'Yellow Archangel'; const VERSION = '2024.12-dev'; + public static function fromDice(Dice $dice): self + { + return new self( + $dice->create(Request::class), + $dice->create(Authentication::class), + $dice->create(IManageConfigValues::class), + $dice->create(Mode::class), + $dice->create(BaseURL::class), + $dice->create(LoggerInterface::class), + $dice->create(Profiler::class), + $dice->create(L10n::class), + $dice->create(Arguments::class), + $dice->create(IHandleUserSessions::class), + $dice->create(DbaDefinition::class), + $dice->create(ViewDefinition::class), + $dice->create(AppHelper::class) + ); + } + /** * @var Mode The Mode of the Application */ @@ -114,7 +134,8 @@ class App Arguments $args, IHandleUserSessions $session, DbaDefinition $dbaDefinition, - ViewDefinition $viewDefinition + ViewDefinition $viewDefinition, + AppHelper $appHelper = null, ) { $this->requestId = $request->getRequestId(); $this->auth = $auth; @@ -126,7 +147,7 @@ class App $this->l10n = $l10n; $this->args = $args; $this->session = $session; - $this->appHelper = DI::appHelper(); + $this->appHelper = $appHelper ?? DI::appHelper(); $this->load($dbaDefinition, $viewDefinition); } From d6ec1d7d6b21e253dc85df8a02f348c768ffcb63 Mon Sep 17 00:00:00 2001 From: Art4 Date: Thu, 19 Dec 2024 20:03:42 +0000 Subject: [PATCH 02/15] Prevent App class to be used with dependency injection --- src/App.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/App.php b/src/App.php index 2fedaa24e8..919d76402e 100644 --- a/src/App.php +++ b/src/App.php @@ -122,7 +122,7 @@ class App */ private $appHelper; - public function __construct( + private function __construct( Request $request, Authentication $auth, IManageConfigValues $config, From 16bdd76be162bd7d738290f9498fc70f7886d1ac Mon Sep 17 00:00:00 2001 From: Art4 Date: Thu, 19 Dec 2024 20:07:37 +0000 Subject: [PATCH 03/15] Create PSR-7 request --- index.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/index.php b/index.php index d3483cb7cd..a9d4135b16 100644 --- a/index.php +++ b/index.php @@ -15,11 +15,13 @@ if (!file_exists(__DIR__ . '/vendor/autoload.php')) { require __DIR__ . '/vendor/autoload.php'; +$request = \GuzzleHttp\Psr7\ServerRequest::fromGlobals(); + $dice = (new Dice())->addRules(include __DIR__ . '/static/dependencies.config.php'); /** @var \Friendica\Core\Addon\Capability\ICanLoadAddons $addonLoader */ $addonLoader = $dice->create(\Friendica\Core\Addon\Capability\ICanLoadAddons::class); $dice = $dice->addRules($addonLoader->getActiveAddonConfig('dependencies')); -$dice = $dice->addRule(Friendica\App\Mode::class, ['call' => [['determineRunMode', [false, $_SERVER], Dice::CHAIN_CALL]]]); +$dice = $dice->addRule(Friendica\App\Mode::class, ['call' => [['determineRunMode', [false, $request->getServerParams()], Dice::CHAIN_CALL]]]); \Friendica\DI::init($dice); @@ -36,7 +38,7 @@ $a->runFrontend( $dice->create(\Friendica\App\Page::class), $dice->create(\Friendica\Content\Nav::class), $dice->create(Friendica\Module\Special\HTTPException::class), - new \Friendica\Util\HTTPInputData($_SERVER), + new \Friendica\Util\HTTPInputData($request->getServerParams()), $start_time, - $_SERVER + $request->getServerParams() ); From 241031f1fa156d679c3a28f6f55f8d9d4f010617 Mon Sep 17 00:00:00 2001 From: Art4 Date: Thu, 19 Dec 2024 20:24:24 +0000 Subject: [PATCH 04/15] inject container inside app instance --- src/App.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/App.php b/src/App.php index 919d76402e..cd1c1753fc 100644 --- a/src/App.php +++ b/src/App.php @@ -55,6 +55,7 @@ class App public static function fromDice(Dice $dice): self { return new self( + $dice, $dice->create(Request::class), $dice->create(Authentication::class), $dice->create(IManageConfigValues::class), @@ -71,6 +72,11 @@ class App ); } + /** + * @var Dice + */ + private $container; + /** * @var Mode The Mode of the Application */ @@ -123,6 +129,7 @@ class App private $appHelper; private function __construct( + Dice $container, Request $request, Authentication $auth, IManageConfigValues $config, @@ -137,6 +144,7 @@ class App ViewDefinition $viewDefinition, AppHelper $appHelper = null, ) { + $this->container = $container; $this->requestId = $request->getRequestId(); $this->auth = $auth; $this->config = $config; From af51441d88ccc31541371ae57618e3b1ff934b1d Mon Sep 17 00:00:00 2001 From: Art4 Date: Thu, 19 Dec 2024 20:26:38 +0000 Subject: [PATCH 05/15] require AppHelper in App --- src/App.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/App.php b/src/App.php index cd1c1753fc..d0881c5e55 100644 --- a/src/App.php +++ b/src/App.php @@ -142,7 +142,7 @@ class App IHandleUserSessions $session, DbaDefinition $dbaDefinition, ViewDefinition $viewDefinition, - AppHelper $appHelper = null, + AppHelper $appHelper, ) { $this->container = $container; $this->requestId = $request->getRequestId(); @@ -155,7 +155,7 @@ class App $this->l10n = $l10n; $this->args = $args; $this->session = $session; - $this->appHelper = $appHelper ?? DI::appHelper(); + $this->appHelper = $appHelper; $this->load($dbaDefinition, $viewDefinition); } From 23e3af4a096a4a9062364224a11f1aee56bd98f4 Mon Sep 17 00:00:00 2001 From: Art4 Date: Thu, 19 Dec 2024 20:37:56 +0000 Subject: [PATCH 06/15] Create tests for App --- tests/Unit/AppTest.php | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 tests/Unit/AppTest.php diff --git a/tests/Unit/AppTest.php b/tests/Unit/AppTest.php new file mode 100644 index 0000000000..c7b9599627 --- /dev/null +++ b/tests/Unit/AppTest.php @@ -0,0 +1,29 @@ +createMock(Dice::class); + $dice->expects($this->exactly(13))->method('create')->willReturnCallback(function($classname) { + return $this->createMock($classname); + }); + + $app = App::fromDice($dice); + + $this->assertInstanceOf(App::class, $app); + } +} From 9ef3fdc857344aaba92695c5d182a2f58c50dc14 Mon Sep 17 00:00:00 2001 From: Art4 Date: Thu, 19 Dec 2024 21:01:41 +0000 Subject: [PATCH 07/15] move load() call outside of constructor to remove side effects --- index.php | 5 +++++ src/App.php | 8 +------- tests/Unit/AppTest.php | 2 +- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/index.php b/index.php index a9d4135b16..ce6daa5edb 100644 --- a/index.php +++ b/index.php @@ -29,6 +29,11 @@ $dice = $dice->addRule(Friendica\App\Mode::class, ['call' => [['determineRunMode $a = \Friendica\App::fromDice($dice); +$a->load( + $dice->create(\Friendica\Database\Definition\DbaDefinition::class), + $dice->create(\Friendica\Database\Definition\ViewDefinition::class), +); + \Friendica\DI::mode()->setExecutor(\Friendica\App\Mode::INDEX); $a->runFrontend( diff --git a/src/App.php b/src/App.php index d0881c5e55..479abe7c89 100644 --- a/src/App.php +++ b/src/App.php @@ -66,8 +66,6 @@ class App $dice->create(L10n::class), $dice->create(Arguments::class), $dice->create(IHandleUserSessions::class), - $dice->create(DbaDefinition::class), - $dice->create(ViewDefinition::class), $dice->create(AppHelper::class) ); } @@ -140,8 +138,6 @@ class App L10n $l10n, Arguments $args, IHandleUserSessions $session, - DbaDefinition $dbaDefinition, - ViewDefinition $viewDefinition, AppHelper $appHelper, ) { $this->container = $container; @@ -156,14 +152,12 @@ class App $this->args = $args; $this->session = $session; $this->appHelper = $appHelper; - - $this->load($dbaDefinition, $viewDefinition); } /** * Load the whole app instance */ - protected function load(DbaDefinition $dbaDefinition, ViewDefinition $viewDefinition) + public function load(DbaDefinition $dbaDefinition, ViewDefinition $viewDefinition) { if ($this->config->get('system', 'ini_max_execution_time') !== false) { set_time_limit((int)$this->config->get('system', 'ini_max_execution_time')); diff --git a/tests/Unit/AppTest.php b/tests/Unit/AppTest.php index c7b9599627..a0b0270e39 100644 --- a/tests/Unit/AppTest.php +++ b/tests/Unit/AppTest.php @@ -18,7 +18,7 @@ class AppTest extends TestCase public function testFromDiceReturnsApp(): void { $dice = $this->createMock(Dice::class); - $dice->expects($this->exactly(13))->method('create')->willReturnCallback(function($classname) { + $dice->expects($this->exactly(11))->method('create')->willReturnCallback(function($classname) { return $this->createMock($classname); }); From 7a7b8d3e2724c7083e2d63f38ab957c63780a3d1 Mon Sep 17 00:00:00 2001 From: Art4 Date: Fri, 20 Dec 2024 10:04:02 +0000 Subject: [PATCH 08/15] Inline App::load() call in new processRequest() method --- index.php | 5 +---- src/App.php | 8 ++++++++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/index.php b/index.php index ce6daa5edb..d98b6db73a 100644 --- a/index.php +++ b/index.php @@ -29,10 +29,7 @@ $dice = $dice->addRule(Friendica\App\Mode::class, ['call' => [['determineRunMode $a = \Friendica\App::fromDice($dice); -$a->load( - $dice->create(\Friendica\Database\Definition\DbaDefinition::class), - $dice->create(\Friendica\Database\Definition\ViewDefinition::class), -); +$a->processRequest(); \Friendica\DI::mode()->setExecutor(\Friendica\App\Mode::INDEX); diff --git a/src/App.php b/src/App.php index 479abe7c89..04520db2fe 100644 --- a/src/App.php +++ b/src/App.php @@ -154,6 +154,14 @@ class App $this->appHelper = $appHelper; } + public function processRequest(): void + { + $this->load( + $this->container->create(DbaDefinition::class), + $this->container->create(ViewDefinition::class), + ); + } + /** * Load the whole app instance */ From ee5b21068258488ea39a23719ee71657d285af63 Mon Sep 17 00:00:00 2001 From: Art4 Date: Fri, 20 Dec 2024 10:05:13 +0000 Subject: [PATCH 09/15] Inline mode call into App::processRequest() --- index.php | 2 -- src/App.php | 2 ++ 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/index.php b/index.php index d98b6db73a..66b5f224ad 100644 --- a/index.php +++ b/index.php @@ -31,8 +31,6 @@ $a = \Friendica\App::fromDice($dice); $a->processRequest(); -\Friendica\DI::mode()->setExecutor(\Friendica\App\Mode::INDEX); - $a->runFrontend( $dice->create(\Friendica\App\Router::class), $dice->create(\Friendica\Core\PConfig\Capability\IManagePersonalConfigValues::class), diff --git a/src/App.php b/src/App.php index 04520db2fe..b151f78b39 100644 --- a/src/App.php +++ b/src/App.php @@ -160,6 +160,8 @@ class App $this->container->create(DbaDefinition::class), $this->container->create(ViewDefinition::class), ); + + $this->mode->setExecutor(Mode::INDEX); } /** From 9e4f39b4fb65732de0a1eba4f2eff0cb0fbdced3 Mon Sep 17 00:00:00 2001 From: Art4 Date: Fri, 20 Dec 2024 10:07:59 +0000 Subject: [PATCH 10/15] Inline runFrontend() call into App::processRequest() method --- index.php | 14 +------------- src/App.php | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/index.php b/index.php index 66b5f224ad..f936cd1a3a 100644 --- a/index.php +++ b/index.php @@ -29,16 +29,4 @@ $dice = $dice->addRule(Friendica\App\Mode::class, ['call' => [['determineRunMode $a = \Friendica\App::fromDice($dice); -$a->processRequest(); - -$a->runFrontend( - $dice->create(\Friendica\App\Router::class), - $dice->create(\Friendica\Core\PConfig\Capability\IManagePersonalConfigValues::class), - $dice->create(\Friendica\Security\Authentication::class), - $dice->create(\Friendica\App\Page::class), - $dice->create(\Friendica\Content\Nav::class), - $dice->create(Friendica\Module\Special\HTTPException::class), - new \Friendica\Util\HTTPInputData($request->getServerParams()), - $start_time, - $request->getServerParams() -); +$a->processRequest($request, $start_time); diff --git a/src/App.php b/src/App.php index b151f78b39..cfd6e52cf2 100644 --- a/src/App.php +++ b/src/App.php @@ -34,6 +34,7 @@ use Friendica\Util\DateTimeFormat; use Friendica\Util\HTTPInputData; use Friendica\Util\HTTPSignature; use Friendica\Util\Profiler; +use Psr\Http\Message\ServerRequestInterface; use Psr\Log\LoggerInterface; /** @@ -154,7 +155,7 @@ class App $this->appHelper = $appHelper; } - public function processRequest(): void + public function processRequest(ServerRequestInterface $request, float $start_time): void { $this->load( $this->container->create(DbaDefinition::class), @@ -162,6 +163,18 @@ class App ); $this->mode->setExecutor(Mode::INDEX); + + $this->runFrontend( + $this->container->create(\Friendica\App\Router::class), + $this->container->create(\Friendica\Core\PConfig\Capability\IManagePersonalConfigValues::class), + $this->container->create(\Friendica\Security\Authentication::class), + $this->container->create(\Friendica\App\Page::class), + $this->container->create(\Friendica\Content\Nav::class), + $this->container->create(\Friendica\Module\Special\HTTPException::class), + new \Friendica\Util\HTTPInputData($request->getServerParams()), + $start_time, + $request->getServerParams() + ); } /** From 0ab9c23bb91d90509a96f85c2844e3267f2dea2f Mon Sep 17 00:00:00 2001 From: Art4 Date: Fri, 20 Dec 2024 12:30:05 +0000 Subject: [PATCH 11/15] Refactor App::fromDice() --- src/App.php | 53 ++++++++++++++--------------------------------------- 1 file changed, 14 insertions(+), 39 deletions(-) diff --git a/src/App.php b/src/App.php index cfd6e52cf2..bcce850270 100644 --- a/src/App.php +++ b/src/App.php @@ -55,20 +55,7 @@ class App public static function fromDice(Dice $dice): self { - return new self( - $dice, - $dice->create(Request::class), - $dice->create(Authentication::class), - $dice->create(IManageConfigValues::class), - $dice->create(Mode::class), - $dice->create(BaseURL::class), - $dice->create(LoggerInterface::class), - $dice->create(Profiler::class), - $dice->create(L10n::class), - $dice->create(Arguments::class), - $dice->create(IHandleUserSessions::class), - $dice->create(AppHelper::class) - ); + return new self($dice); } /** @@ -127,32 +114,20 @@ class App */ private $appHelper; - private function __construct( - Dice $container, - Request $request, - Authentication $auth, - IManageConfigValues $config, - Mode $mode, - BaseURL $baseURL, - LoggerInterface $logger, - Profiler $profiler, - L10n $l10n, - Arguments $args, - IHandleUserSessions $session, - AppHelper $appHelper, - ) { + private function __construct(Dice $container) + { $this->container = $container; - $this->requestId = $request->getRequestId(); - $this->auth = $auth; - $this->config = $config; - $this->mode = $mode; - $this->baseURL = $baseURL; - $this->profiler = $profiler; - $this->logger = $logger; - $this->l10n = $l10n; - $this->args = $args; - $this->session = $session; - $this->appHelper = $appHelper; + $this->requestId = $this->container->create(Request::class)->getRequestId(); + $this->auth = $this->container->create(Authentication::class); + $this->config = $this->container->create(IManageConfigValues::class); + $this->mode = $this->container->create(Mode::class); + $this->baseURL = $this->container->create(BaseURL::class); + $this->logger = $this->container->create(LoggerInterface::class); + $this->profiler = $this->container->create(Profiler::class); + $this->l10n = $this->container->create(L10n::class); + $this->args = $this->container->create(Arguments::class); + $this->session = $this->container->create(IHandleUserSessions::class); + $this->appHelper = $this->container->create(AppHelper::class); } public function processRequest(ServerRequestInterface $request, float $start_time): void From ea07b83fd0425934fb94b690ef452e2ff462a9d3 Mon Sep 17 00:00:00 2001 From: Art4 Date: Fri, 20 Dec 2024 13:21:35 +0000 Subject: [PATCH 12/15] adjust tests --- tests/Unit/AppTest.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/Unit/AppTest.php b/tests/Unit/AppTest.php index a0b0270e39..124e63472a 100644 --- a/tests/Unit/AppTest.php +++ b/tests/Unit/AppTest.php @@ -18,9 +18,7 @@ class AppTest extends TestCase public function testFromDiceReturnsApp(): void { $dice = $this->createMock(Dice::class); - $dice->expects($this->exactly(11))->method('create')->willReturnCallback(function($classname) { - return $this->createMock($classname); - }); + $dice->expects($this->never())->method('create'); $app = App::fromDice($dice); From 7a4542cd50a0d261af2dc44db804d974460a472a Mon Sep 17 00:00:00 2001 From: Art4 Date: Fri, 20 Dec 2024 13:24:25 +0000 Subject: [PATCH 13/15] refactor App --- src/App.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/App.php b/src/App.php index bcce850270..521e310b8f 100644 --- a/src/App.php +++ b/src/App.php @@ -117,6 +117,10 @@ class App private function __construct(Dice $container) { $this->container = $container; + } + + public function processRequest(ServerRequestInterface $request, float $start_time): void + { $this->requestId = $this->container->create(Request::class)->getRequestId(); $this->auth = $this->container->create(Authentication::class); $this->config = $this->container->create(IManageConfigValues::class); @@ -128,10 +132,7 @@ class App $this->args = $this->container->create(Arguments::class); $this->session = $this->container->create(IHandleUserSessions::class); $this->appHelper = $this->container->create(AppHelper::class); - } - public function processRequest(ServerRequestInterface $request, float $start_time): void - { $this->load( $this->container->create(DbaDefinition::class), $this->container->create(ViewDefinition::class), @@ -155,7 +156,7 @@ class App /** * Load the whole app instance */ - public function load(DbaDefinition $dbaDefinition, ViewDefinition $viewDefinition) + private function load(DbaDefinition $dbaDefinition, ViewDefinition $viewDefinition) { if ($this->config->get('system', 'ini_max_execution_time') !== false) { set_time_limit((int)$this->config->get('system', 'ini_max_execution_time')); @@ -229,7 +230,7 @@ class App * @throws HTTPException\InternalServerErrorException * @throws \ImagickException */ - public function runFrontend( + private function runFrontend( Router $router, IManagePersonalConfigValues $pconfig, Authentication $auth, From 2f9f873f4b65aca20ccd0614fc6cff0d79a590be Mon Sep 17 00:00:00 2001 From: Art4 Date: Fri, 20 Dec 2024 13:38:38 +0000 Subject: [PATCH 14/15] refactor App::runFrontend() --- src/App.php | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/src/App.php b/src/App.php index 521e310b8f..bc3efde176 100644 --- a/src/App.php +++ b/src/App.php @@ -141,13 +141,12 @@ class App $this->mode->setExecutor(Mode::INDEX); $this->runFrontend( - $this->container->create(\Friendica\App\Router::class), - $this->container->create(\Friendica\Core\PConfig\Capability\IManagePersonalConfigValues::class), - $this->container->create(\Friendica\Security\Authentication::class), - $this->container->create(\Friendica\App\Page::class), - $this->container->create(\Friendica\Content\Nav::class), - $this->container->create(\Friendica\Module\Special\HTTPException::class), - new \Friendica\Util\HTTPInputData($request->getServerParams()), + $this->container->create(Router::class), + $this->container->create(IManagePersonalConfigValues::class), + $this->container->create(Page::class), + $this->container->create(Nav::class), + $this->container->create(ModuleHTTPException::class), + new HTTPInputData($request->getServerParams()), $start_time, $request->getServerParams() ); @@ -220,7 +219,6 @@ class App * * @param Router $router * @param IManagePersonalConfigValues $pconfig - * @param Authentication $auth The Authentication backend of the node * @param Page $page The Friendica page printing container * @param ModuleHTTPException $httpException The possible HTTP Exception container * @param HTTPInputData $httpInput A library for processing PHP input streams @@ -233,7 +231,6 @@ class App private function runFrontend( Router $router, IManagePersonalConfigValues $pconfig, - Authentication $auth, Page $page, Nav $nav, ModuleHTTPException $httpException, @@ -301,7 +298,7 @@ class App } if (!$this->mode->isBackend()) { - $auth->withSession(); + $this->auth->withSession(); } if ($this->session->isUnauthenticated()) { From 0bdc1d9f57c6acb50da1afb7de45ee6f943c51a8 Mon Sep 17 00:00:00 2001 From: Art4 Date: Fri, 20 Dec 2024 20:56:26 +0000 Subject: [PATCH 15/15] Let phpstan check index.php --- .phpstan.neon | 1 + 1 file changed, 1 insertion(+) diff --git a/.phpstan.neon b/.phpstan.neon index 2a89c7ae1a..687b9d2f98 100644 --- a/.phpstan.neon +++ b/.phpstan.neon @@ -8,6 +8,7 @@ parameters: paths: - addon/ - src/ + - index.php excludePaths: analyse: