]> git.mxchange.org Git - friendica.git/commitdiff
Move isBackend logic to App\Mode
authorPhilipp Holzer <admin+github@philipp.info>
Thu, 15 Aug 2019 13:51:15 +0000 (15:51 +0200)
committerPhilipp Holzer <admin+github@philipp.info>
Thu, 15 Aug 2019 13:51:15 +0000 (15:51 +0200)
src/App.php
src/App/Mode.php
static/dependencies.config.php
tests/src/App/ModeTest.php

index 49721547316880afa852b7691d873a9b4d256a41..1ea5e914cf595f2f8c5619eb119f27adc0a3729f 100644 (file)
@@ -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();
        }
 
        /**
index 166d47a1740f13e1d5185cd0a79e322ea6053060..2a6722643eb9cd9cc1b10a6e398182eb39c51fee 100644 (file)
@@ -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;
+       }
 }
index c9f013a6f46d4271f543f2c9da04fe02c9cd4279..7a4adb5ea194929a062f51629794e06289be053d 100644 (file)
@@ -62,6 +62,7 @@ return [
        ],
        App\Mode::class                 => [
                'call' => [
+                       ['determineBackend', [$_SERVER], Dice::CHAIN_CALL],
                        ['determine', [], Dice::CHAIN_CALL],
                ],
        ],
index 30d0dc531ba3872ace4929f6fad2d0438dab5529..56b9cc913f21b14edcb10570c243780dd761aeae 100644 (file)
@@ -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());
+       }
 }