From cba9fa246749f3205dda52cb565b8b8bed32f68e Mon Sep 17 00:00:00 2001 From: Philipp Holzer Date: Thu, 15 Aug 2019 15:51:15 +0200 Subject: [PATCH 1/3] Move isBackend logic to App\Mode --- src/App.php | 32 +++++---------------------- src/App/Mode.php | 40 ++++++++++++++++++++++++++++++---- static/dependencies.config.php | 1 + tests/src/App/ModeTest.php | 40 ++++++++++++++++++++++++++++++++++ 4 files changed, 82 insertions(+), 31 deletions(-) diff --git a/src/App.php b/src/App.php index 4972154731..1ea5e914cf 100644 --- a/src/App.php +++ b/src/App.php @@ -9,7 +9,6 @@ use DOMDocument; use DOMXPath; use Exception; use Friendica\App\Arguments; -use Friendica\App\Module; use Friendica\Core\Config\Cache\ConfigCache; use Friendica\Core\Config\Configuration; use Friendica\Core\Config\PConfiguration; @@ -98,11 +97,6 @@ class App */ private $baseURL; - /** - * @var bool true, if the call is from an backend node (f.e. worker) - */ - private $isBackend; - /** * @var string The name of the current theme */ @@ -148,11 +142,6 @@ class App */ private $args; - /** - * @var App\Module - */ - private $moduleClass; - /** * Returns the current config cache of this node * @@ -274,7 +263,7 @@ class App * * @throws Exception if the Basepath is not usable */ - public function __construct(Database $database, Configuration $config, App\Mode $mode, App\Router $router, BaseURL $baseURL, LoggerInterface $logger, Profiler $profiler, L10n $l10n, Arguments $args, Module $module) + public function __construct(Database $database, Configuration $config, App\Mode $mode, App\Router $router, BaseURL $baseURL, LoggerInterface $logger, Profiler $profiler, L10n $l10n, Arguments $args) { $this->database = $database; $this->config = $config; @@ -285,7 +274,6 @@ class App $this->logger = $logger; $this->l10n = $l10n; $this->args = $args; - $this->isBackend = $this->checkBackend($module); $this->profiler->reset(); @@ -573,27 +561,17 @@ class App $this->getBaseURL(); } - /** - * Checks if the site is called via a backend process - * - * @param Module $module The pre-loaded module (just name, not class!) - - * @return bool True, if the call is a backend call - */ - private function checkBackend(Module $module) - { - return basename(($_SERVER['PHP_SELF'] ?? ''), '.php') !== 'index' || - $module->isBackend(); - } - /** * Returns true, if the call is from a backend node (f.e. from a worker) * * @return bool Is it a known backend? + * + * @deprecated 2019.09 - use App\Mode->isBackend() instead + * @see App\Mode::isBackend() */ public function isBackend() { - return $this->isBackend; + return $this->mode->isBackend(); } /** diff --git a/src/App/Mode.php b/src/App/Mode.php index 166d47a174..2a6722643e 100644 --- a/src/App/Mode.php +++ b/src/App/Mode.php @@ -19,14 +19,20 @@ class Mode const MAINTENANCEDISABLED = 8; /*** - * @var int the mode of this Application + * @var int The mode of this Application * */ private $mode; - public function __construct(int $mode = 0) + /** + * @var bool True, if the call is a backend call + */ + private $isBackend; + + public function __construct(int $mode = 0, bool $isBackend = false) { - $this->mode = $mode; + $this->mode = $mode; + $this->isBackend = $isBackend; } /** @@ -75,7 +81,23 @@ class Mode $mode |= Mode::MAINTENANCEDISABLED; - return new Mode($mode); + return new Mode($mode, $this->isBackend); + } + + /** + * Checks if the site is called via a backend process + * + * @param Module $module The pre-loaded module (just name, not class!) + * @param array $server The $_SERVER variable + * + * @return Mode returns the determined mode + */ + public function determineBackend(Module $module, array $server) + { + $isBackend = basename(($server['PHP_SELF'] ?? ''), '.php') !== 'index' || + $module->isBackend(); + + return new Mode($this->mode, $isBackend); } /** @@ -114,4 +136,14 @@ class Mode $this->has(Mode::DBCONFIGAVAILABLE) && $this->has(Mode::MAINTENANCEDISABLED); } + + /** + * Returns true, if the call is from a backend node (f.e. from a worker) + * + * @return bool Is it a backend call + */ + public function isBackend() + { + return $this->isBackend; + } } diff --git a/static/dependencies.config.php b/static/dependencies.config.php index c9f013a6f4..7a4adb5ea1 100644 --- a/static/dependencies.config.php +++ b/static/dependencies.config.php @@ -62,6 +62,7 @@ return [ ], App\Mode::class => [ 'call' => [ + ['determineBackend', [$_SERVER], Dice::CHAIN_CALL], ['determine', [], Dice::CHAIN_CALL], ], ], diff --git a/tests/src/App/ModeTest.php b/tests/src/App/ModeTest.php index 30d0dc531b..56b9cc913f 100644 --- a/tests/src/App/ModeTest.php +++ b/tests/src/App/ModeTest.php @@ -3,6 +3,7 @@ namespace Friendica\Test\src\App; use Friendica\App\Mode; +use Friendica\App\Module; use Friendica\Core\Config; use Friendica\Database\Database; use Friendica\Test\MockedTest; @@ -177,4 +178,43 @@ class ModeTest extends MockedTest $this->assertNotSame($modeNew, $mode); } + + /** + * Test if not called by index is backend + */ + public function testIsBackendNotIndex() + { + $server = ['PHP_SELF' => '/daemon.php']; + $module = new Module(); + + $mode = (new Mode())->determineBackend($module, $server); + + $this->assertTrue($mode->isBackend()); + } + + /** + * Test is called by index but module is backend + */ + public function testIsBackendButIndex() + { + $server = ['PHP_SELF' => '/index.php']; + $module = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, true); + + $mode = (new Mode())->determineBackend($module, $server); + + $this->assertTrue($mode->isBackend()); + } + + /** + * Test is called by index and module is not backend + */ + public function testIsNotBackend() + { + $server = ['PHP_SELF' => '/index.php']; + $module = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, false); + + $mode = (new Mode())->determineBackend($module, $server); + + $this->assertFalse($mode->isBackend()); + } } From f3269bf826f3d323cead9ddf053cecfcae145fca Mon Sep 17 00:00:00 2001 From: Philipp Holzer Date: Thu, 15 Aug 2019 16:19:23 +0200 Subject: [PATCH 2/3] Add hint for getClass --- src/App.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/App.php b/src/App.php index 1ea5e914cf..5e7ad36d80 100644 --- a/src/App.php +++ b/src/App.php @@ -568,6 +568,7 @@ class App * * @deprecated 2019.09 - use App\Mode->isBackend() instead * @see App\Mode::isBackend() + * Use BaseObject::getClass(App\Mode::class) to get these class directly per global registry */ public function isBackend() { From 9abfd55d374e5cd39bf485e5862a330dcc2de309 Mon Sep 17 00:00:00 2001 From: Philipp Holzer Date: Thu, 15 Aug 2019 16:25:34 +0200 Subject: [PATCH 3/3] modified hint --- src/App.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/App.php b/src/App.php index 5e7ad36d80..505f502e5f 100644 --- a/src/App.php +++ b/src/App.php @@ -568,7 +568,7 @@ class App * * @deprecated 2019.09 - use App\Mode->isBackend() instead * @see App\Mode::isBackend() - * Use BaseObject::getClass(App\Mode::class) to get these class directly per global registry + * Use BaseObject::getClass(App\Mode::class) to get the global instance of Mode */ public function isBackend() {