Merge pull request #7516 from nupplaphil/task/isBackend

Move isBackend logic to App\Mode
This commit is contained in:
Hypolite Petovan 2019-08-15 10:30:55 -04:00 committed by GitHub
commit cfc5d730de
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 83 additions and 31 deletions

View file

@ -9,7 +9,6 @@ use DOMDocument;
use DOMXPath; use DOMXPath;
use Exception; use Exception;
use Friendica\App\Arguments; use Friendica\App\Arguments;
use Friendica\App\Module;
use Friendica\Core\Config\Cache\ConfigCache; use Friendica\Core\Config\Cache\ConfigCache;
use Friendica\Core\Config\Configuration; use Friendica\Core\Config\Configuration;
use Friendica\Core\Config\PConfiguration; use Friendica\Core\Config\PConfiguration;
@ -98,11 +97,6 @@ class App
*/ */
private $baseURL; 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 * @var string The name of the current theme
*/ */
@ -148,11 +142,6 @@ class App
*/ */
private $args; private $args;
/**
* @var App\Module
*/
private $moduleClass;
/** /**
* Returns the current config cache of this node * Returns the current config cache of this node
* *
@ -274,7 +263,7 @@ class App
* *
* @throws Exception if the Basepath is not usable * @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->database = $database;
$this->config = $config; $this->config = $config;
@ -285,7 +274,6 @@ class App
$this->logger = $logger; $this->logger = $logger;
$this->l10n = $l10n; $this->l10n = $l10n;
$this->args = $args; $this->args = $args;
$this->isBackend = $this->checkBackend($module);
$this->profiler->reset(); $this->profiler->reset();
@ -573,27 +561,18 @@ class App
$this->getBaseURL(); $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) * Returns true, if the call is from a backend node (f.e. from a worker)
* *
* @return bool Is it a known backend? * @return bool Is it a known backend?
*
* @deprecated 2019.09 - use App\Mode->isBackend() instead
* @see App\Mode::isBackend()
* Use BaseObject::getClass(App\Mode::class) to get the global instance of Mode
*/ */
public function isBackend() public function isBackend()
{ {
return $this->isBackend; return $this->mode->isBackend();
} }
/** /**

View file

@ -19,14 +19,20 @@ class Mode
const MAINTENANCEDISABLED = 8; const MAINTENANCEDISABLED = 8;
/*** /***
* @var int the mode of this Application * @var int The mode of this Application
* *
*/ */
private $mode; 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; $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::DBCONFIGAVAILABLE) &&
$this->has(Mode::MAINTENANCEDISABLED); $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;
}
} }

View file

@ -62,6 +62,7 @@ return [
], ],
App\Mode::class => [ App\Mode::class => [
'call' => [ 'call' => [
['determineBackend', [$_SERVER], Dice::CHAIN_CALL],
['determine', [], Dice::CHAIN_CALL], ['determine', [], Dice::CHAIN_CALL],
], ],
], ],

View file

@ -3,6 +3,7 @@
namespace Friendica\Test\src\App; namespace Friendica\Test\src\App;
use Friendica\App\Mode; use Friendica\App\Mode;
use Friendica\App\Module;
use Friendica\Core\Config; use Friendica\Core\Config;
use Friendica\Database\Database; use Friendica\Database\Database;
use Friendica\Test\MockedTest; use Friendica\Test\MockedTest;
@ -177,4 +178,43 @@ class ModeTest extends MockedTest
$this->assertNotSame($modeNew, $mode); $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());
}
} }