Move mode settings to App\Mode
- Move isAjax() to App\Mode - Move isTablet() to App\Mode - Move isMobile() to App\Mode - Refactor last usage of App->isBackend()
This commit is contained in:
parent
e7c3d327cc
commit
90b438e082
51
src/App.php
51
src/App.php
|
@ -4,7 +4,6 @@
|
||||||
*/
|
*/
|
||||||
namespace Friendica;
|
namespace Friendica;
|
||||||
|
|
||||||
use Detection\MobileDetect;
|
|
||||||
use Exception;
|
use Exception;
|
||||||
use Friendica\App\Arguments;
|
use Friendica\App\Arguments;
|
||||||
use Friendica\App\BaseURL;
|
use Friendica\App\BaseURL;
|
||||||
|
@ -67,7 +66,9 @@ class App
|
||||||
public $timezone;
|
public $timezone;
|
||||||
public $interactive = true;
|
public $interactive = true;
|
||||||
public $identities;
|
public $identities;
|
||||||
|
/** @deprecated 2019.09 - Use App\Mode->isMobile() instead */
|
||||||
public $is_mobile;
|
public $is_mobile;
|
||||||
|
/** @deprecated 2019.09 - Use App\Mode->isTable() instead */
|
||||||
public $is_tablet;
|
public $is_tablet;
|
||||||
public $theme_info = [];
|
public $theme_info = [];
|
||||||
public $category;
|
public $category;
|
||||||
|
@ -86,11 +87,6 @@ class App
|
||||||
*/
|
*/
|
||||||
private $mode;
|
private $mode;
|
||||||
|
|
||||||
/**
|
|
||||||
* @var App\Router
|
|
||||||
*/
|
|
||||||
private $router;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var BaseURL
|
* @var BaseURL
|
||||||
*/
|
*/
|
||||||
|
@ -101,16 +97,6 @@ class App
|
||||||
*/
|
*/
|
||||||
private $currentTheme;
|
private $currentTheme;
|
||||||
|
|
||||||
/**
|
|
||||||
* @var bool check if request was an AJAX (xmlhttprequest) request
|
|
||||||
*/
|
|
||||||
private $isAjax;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var MobileDetect
|
|
||||||
*/
|
|
||||||
public $mobileDetect;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var Configuration The config
|
* @var Configuration The config
|
||||||
*/
|
*/
|
||||||
|
@ -234,26 +220,22 @@ class App
|
||||||
* @param Database $database The Friendica Database
|
* @param Database $database The Friendica Database
|
||||||
* @param Configuration $config The Configuration
|
* @param Configuration $config The Configuration
|
||||||
* @param App\Mode $mode The mode of this Friendica app
|
* @param App\Mode $mode The mode of this Friendica app
|
||||||
* @param App\Router $router The router of this Friendica app
|
|
||||||
* @param BaseURL $baseURL The full base URL of this Friendica app
|
* @param BaseURL $baseURL The full base URL of this Friendica app
|
||||||
* @param LoggerInterface $logger The current app logger
|
* @param LoggerInterface $logger The current app logger
|
||||||
* @param Profiler $profiler The profiler of this application
|
* @param Profiler $profiler The profiler of this application
|
||||||
* @param L10n $l10n The translator instance
|
* @param L10n $l10n The translator instance
|
||||||
* @param App\Arguments $args The Friendica Arguments of the call
|
* @param App\Arguments $args The Friendica Arguments of the call
|
||||||
* @param MobileDetect $mobileDetect A mobile detection class
|
|
||||||
*/
|
*/
|
||||||
public function __construct(Database $database, Configuration $config, App\Mode $mode, App\Router $router, BaseURL $baseURL, LoggerInterface $logger, Profiler $profiler, L10n $l10n, Arguments $args, App\Module $module, App\Page $page, MobileDetect $mobileDetect)
|
public function __construct(Database $database, Configuration $config, App\Mode $mode, BaseURL $baseURL, LoggerInterface $logger, Profiler $profiler, L10n $l10n, Arguments $args, App\Module $module, App\Page $page)
|
||||||
{
|
{
|
||||||
$this->database = $database;
|
$this->database = $database;
|
||||||
$this->config = $config;
|
$this->config = $config;
|
||||||
$this->mode = $mode;
|
$this->mode = $mode;
|
||||||
$this->router = $router;
|
|
||||||
$this->baseURL = $baseURL;
|
$this->baseURL = $baseURL;
|
||||||
$this->profiler = $profiler;
|
$this->profiler = $profiler;
|
||||||
$this->logger = $logger;
|
$this->logger = $logger;
|
||||||
$this->l10n = $l10n;
|
$this->l10n = $l10n;
|
||||||
$this->args = $args;
|
$this->args = $args;
|
||||||
$this->mobileDetect = $mobileDetect;
|
|
||||||
|
|
||||||
$this->cmd = $args->getCommand();
|
$this->cmd = $args->getCommand();
|
||||||
$this->argv = $args->getArgv();
|
$this->argv = $args->getArgv();
|
||||||
|
@ -262,10 +244,8 @@ class App
|
||||||
$this->module = $module->getName();
|
$this->module = $module->getName();
|
||||||
$this->page = $page;
|
$this->page = $page;
|
||||||
|
|
||||||
$this->is_mobile = $mobileDetect->isMobile();
|
$this->is_mobile = $mode->isMobile();
|
||||||
$this->is_tablet = $mobileDetect->isTablet();
|
$this->is_tablet = $mode->isTablet();
|
||||||
|
|
||||||
$this->isAjax = strtolower(defaults($_SERVER, 'HTTP_X_REQUESTED_WITH', '')) == 'xmlhttprequest';
|
|
||||||
|
|
||||||
$this->load();
|
$this->load();
|
||||||
}
|
}
|
||||||
|
@ -417,20 +397,6 @@ class App
|
||||||
$this->getBaseURL();
|
$this->getBaseURL();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 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()
|
|
||||||
* Use BaseObject::getClass(App\Mode::class) to get the global instance of Mode
|
|
||||||
*/
|
|
||||||
public function isBackend()
|
|
||||||
{
|
|
||||||
return $this->mode->isBackend();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Checks if the maximum number of database processes is reached
|
* @brief Checks if the maximum number of database processes is reached
|
||||||
*
|
*
|
||||||
|
@ -705,13 +671,12 @@ class App
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if request was an AJAX (xmlhttprequest) request.
|
* @deprecated 2019.09 - use App\Mode->isAjax() instead
|
||||||
*
|
* @see App\Mode::isAjax()
|
||||||
* @return boolean true if it was an AJAX request
|
|
||||||
*/
|
*/
|
||||||
public function isAjax()
|
public function isAjax()
|
||||||
{
|
{
|
||||||
return $this->isAjax;
|
return $this->mode->isAjax();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
namespace Friendica\App;
|
namespace Friendica\App;
|
||||||
|
|
||||||
|
use Detection\MobileDetect;
|
||||||
use Friendica\Core\Config\Cache\ConfigCache;
|
use Friendica\Core\Config\Cache\ConfigCache;
|
||||||
use Friendica\Database\Database;
|
use Friendica\Database\Database;
|
||||||
use Friendica\Util\BasePath;
|
use Friendica\Util\BasePath;
|
||||||
|
@ -29,10 +30,28 @@ class Mode
|
||||||
*/
|
*/
|
||||||
private $isBackend;
|
private $isBackend;
|
||||||
|
|
||||||
public function __construct(int $mode = 0, bool $isBackend = false)
|
/**
|
||||||
|
* @var bool True, if the call is a ajax call
|
||||||
|
*/
|
||||||
|
private $isAjax;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var bool True, if the call is from a mobile device
|
||||||
|
*/
|
||||||
|
private $isMobile;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var bool True, if the call is from a tablet device
|
||||||
|
*/
|
||||||
|
private $isTablet;
|
||||||
|
|
||||||
|
public function __construct(int $mode = 0, bool $isBackend = false, bool $isAjax = false, bool $isMobile = false, bool $isTablet = false)
|
||||||
{
|
{
|
||||||
$this->mode = $mode;
|
$this->mode = $mode;
|
||||||
$this->isBackend = $isBackend;
|
$this->isBackend = $isBackend;
|
||||||
|
$this->isAjax = $isAjax;
|
||||||
|
$this->isMobile = $isMobile;
|
||||||
|
$this->isTablet = $isTablet;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -81,7 +100,7 @@ class Mode
|
||||||
|
|
||||||
$mode |= Mode::MAINTENANCEDISABLED;
|
$mode |= Mode::MAINTENANCEDISABLED;
|
||||||
|
|
||||||
return new Mode($mode, $this->isBackend);
|
return new Mode($mode, $this->isBackend, $this->isAjax, $this->isMobile, $this->isTablet);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -89,15 +108,19 @@ class Mode
|
||||||
*
|
*
|
||||||
* @param Module $module The pre-loaded module (just name, not class!)
|
* @param Module $module The pre-loaded module (just name, not class!)
|
||||||
* @param array $server The $_SERVER variable
|
* @param array $server The $_SERVER variable
|
||||||
|
* @param MobileDetect $mobileDetect The mobile detection library
|
||||||
*
|
*
|
||||||
* @return Mode returns the determined mode
|
* @return Mode returns the determined mode
|
||||||
*/
|
*/
|
||||||
public function determineBackend(Module $module, array $server)
|
public function determineRunMode(Module $module, array $server, MobileDetect $mobileDetect)
|
||||||
{
|
{
|
||||||
$isBackend = basename(($server['PHP_SELF'] ?? ''), '.php') !== 'index' ||
|
$isBackend = basename(($server['PHP_SELF'] ?? ''), '.php') !== 'index' ||
|
||||||
$module->isBackend();
|
$module->isBackend();
|
||||||
|
$isMobile = $mobileDetect->isMobile();
|
||||||
|
$isTablet = $mobileDetect->isTablet();
|
||||||
|
$isAjax = strtolower($server['HTTP_X_REQUESTED_WITH'] ?? '') == 'xmlhttprequest';
|
||||||
|
|
||||||
return new Mode($this->mode, $isBackend);
|
return new Mode($this->mode, $isBackend, $isAjax, $isMobile, $isTablet);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -146,4 +169,34 @@ class Mode
|
||||||
{
|
{
|
||||||
return $this->isBackend;
|
return $this->isBackend;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if request was an AJAX (xmlhttprequest) request.
|
||||||
|
*
|
||||||
|
* @return bool true if it was an AJAX request
|
||||||
|
*/
|
||||||
|
public function isAjax()
|
||||||
|
{
|
||||||
|
return $this->isAjax;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if request was a mobile request.
|
||||||
|
*
|
||||||
|
* @return bool true if it was an mobile request
|
||||||
|
*/
|
||||||
|
public function isMobile()
|
||||||
|
{
|
||||||
|
return $this->isMobile;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if request was a tablet request.
|
||||||
|
*
|
||||||
|
* @return bool true if it was an tablet request
|
||||||
|
*/
|
||||||
|
public function isTablet()
|
||||||
|
{
|
||||||
|
return $this->isTablet;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -245,11 +245,12 @@ class Page implements ArrayAccess
|
||||||
* - footer.tpl template
|
* - footer.tpl template
|
||||||
*
|
*
|
||||||
* @param App $app The Friendica App instance
|
* @param App $app The Friendica App instance
|
||||||
|
* @param Mode $mode The Friendica runtime mode
|
||||||
* @param L10n $l10n The l10n instance
|
* @param L10n $l10n The l10n instance
|
||||||
*
|
*
|
||||||
* @throws HTTPException\InternalServerErrorException
|
* @throws HTTPException\InternalServerErrorException
|
||||||
*/
|
*/
|
||||||
private function initFooter(App $app, L10n $l10n)
|
private function initFooter(App $app, Mode $mode, L10n $l10n)
|
||||||
{
|
{
|
||||||
// If you're just visiting, let javascript take you home
|
// If you're just visiting, let javascript take you home
|
||||||
if (!empty($_SESSION['visitor_home'])) {
|
if (!empty($_SESSION['visitor_home'])) {
|
||||||
|
@ -265,7 +266,7 @@ class Page implements ArrayAccess
|
||||||
/*
|
/*
|
||||||
* Add a "toggle mobile" link if we're using a mobile device
|
* Add a "toggle mobile" link if we're using a mobile device
|
||||||
*/
|
*/
|
||||||
if ($app->is_mobile || $app->is_tablet) {
|
if ($mode->isMobile() || $mode->isTablet()) {
|
||||||
if (isset($_SESSION['show-mobile']) && !$_SESSION['show-mobile']) {
|
if (isset($_SESSION['show-mobile']) && !$_SESSION['show-mobile']) {
|
||||||
$link = 'toggle_mobile?address=' . urlencode(curPageURL());
|
$link = 'toggle_mobile?address=' . urlencode(curPageURL());
|
||||||
} else {
|
} else {
|
||||||
|
@ -375,9 +376,9 @@ class Page implements ArrayAccess
|
||||||
/* Build the page ending -- this is stuff that goes right before
|
/* Build the page ending -- this is stuff that goes right before
|
||||||
* the closing </body> tag
|
* the closing </body> tag
|
||||||
*/
|
*/
|
||||||
$this->initFooter($app, $l10n);
|
$this->initFooter($app, $mode, $l10n);
|
||||||
|
|
||||||
if (!$app->isAjax()) {
|
if (!$mode->isAjax()) {
|
||||||
Hook::callAll('page_end', $this->page['content']);
|
Hook::callAll('page_end', $this->page['content']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1124,7 +1124,7 @@ class Worker
|
||||||
|
|
||||||
$priority = PRIORITY_MEDIUM;
|
$priority = PRIORITY_MEDIUM;
|
||||||
// Don't fork from frontend tasks by default
|
// Don't fork from frontend tasks by default
|
||||||
$dont_fork = Config::get("system", "worker_dont_fork", false) || !\get_app()->isBackend();
|
$dont_fork = Config::get("system", "worker_dont_fork", false) || !\get_app()->getMode()->isBackend();
|
||||||
$created = DateTimeFormat::utcNow();
|
$created = DateTimeFormat::utcNow();
|
||||||
$force_priority = false;
|
$force_priority = false;
|
||||||
|
|
||||||
|
|
|
@ -62,7 +62,7 @@ return [
|
||||||
],
|
],
|
||||||
App\Mode::class => [
|
App\Mode::class => [
|
||||||
'call' => [
|
'call' => [
|
||||||
['determineBackend', [$_SERVER], Dice::CHAIN_CALL],
|
['determineRunMode', [$_SERVER], Dice::CHAIN_CALL],
|
||||||
['determine', [], Dice::CHAIN_CALL],
|
['determine', [], Dice::CHAIN_CALL],
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
namespace Friendica\Test\src\App;
|
namespace Friendica\Test\src\App;
|
||||||
|
|
||||||
|
use Detection\MobileDetect;
|
||||||
use Friendica\App\Mode;
|
use Friendica\App\Mode;
|
||||||
use Friendica\App\Module;
|
use Friendica\App\Module;
|
||||||
use Friendica\Core\Config;
|
use Friendica\Core\Config;
|
||||||
|
@ -186,8 +187,9 @@ class ModeTest extends MockedTest
|
||||||
{
|
{
|
||||||
$server = ['PHP_SELF' => '/daemon.php'];
|
$server = ['PHP_SELF' => '/daemon.php'];
|
||||||
$module = new Module();
|
$module = new Module();
|
||||||
|
$mobileDetect = new MobileDetect();
|
||||||
|
|
||||||
$mode = (new Mode())->determineBackend($module, $server);
|
$mode = (new Mode())->determineRunMode($module, $server, $mobileDetect);
|
||||||
|
|
||||||
$this->assertTrue($mode->isBackend());
|
$this->assertTrue($mode->isBackend());
|
||||||
}
|
}
|
||||||
|
@ -199,8 +201,9 @@ class ModeTest extends MockedTest
|
||||||
{
|
{
|
||||||
$server = ['PHP_SELF' => '/index.php'];
|
$server = ['PHP_SELF' => '/index.php'];
|
||||||
$module = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, true);
|
$module = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, true);
|
||||||
|
$mobileDetect = new MobileDetect();
|
||||||
|
|
||||||
$mode = (new Mode())->determineBackend($module, $server);
|
$mode = (new Mode())->determineRunMode($module, $server, $mobileDetect);
|
||||||
|
|
||||||
$this->assertTrue($mode->isBackend());
|
$this->assertTrue($mode->isBackend());
|
||||||
}
|
}
|
||||||
|
@ -212,9 +215,77 @@ class ModeTest extends MockedTest
|
||||||
{
|
{
|
||||||
$server = ['PHP_SELF' => '/index.php'];
|
$server = ['PHP_SELF' => '/index.php'];
|
||||||
$module = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, false);
|
$module = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, false);
|
||||||
|
$mobileDetect = new MobileDetect();
|
||||||
|
|
||||||
$mode = (new Mode())->determineBackend($module, $server);
|
$mode = (new Mode())->determineRunMode($module, $server, $mobileDetect);
|
||||||
|
|
||||||
$this->assertFalse($mode->isBackend());
|
$this->assertFalse($mode->isBackend());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test if the call is an ajax call
|
||||||
|
*/
|
||||||
|
public function testIsAjax()
|
||||||
|
{
|
||||||
|
// This is the server environment variable to determine ajax calls
|
||||||
|
$server = [
|
||||||
|
'HTTP_X_REQUESTED_WITH' => 'xmlhttprequest',
|
||||||
|
];
|
||||||
|
|
||||||
|
$module = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, false);
|
||||||
|
$mobileDetect = new MobileDetect();
|
||||||
|
|
||||||
|
$mode = (new Mode())->determineRunMode($module, $server, $mobileDetect);
|
||||||
|
|
||||||
|
$this->assertTrue($mode->isAjax());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test if the call is not nan ajax call
|
||||||
|
*/
|
||||||
|
public function testIsNotAjax()
|
||||||
|
{
|
||||||
|
$server = [];
|
||||||
|
$module = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, false);
|
||||||
|
$mobileDetect = new MobileDetect();
|
||||||
|
|
||||||
|
$mode = (new Mode())->determineRunMode($module, $server, $mobileDetect);
|
||||||
|
|
||||||
|
$this->assertFalse($mode->isAjax());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test if the call is a mobile and is a tablet call
|
||||||
|
*/
|
||||||
|
public function testIsMobileIsTablet()
|
||||||
|
{
|
||||||
|
$server = [];
|
||||||
|
$module = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, false);
|
||||||
|
$mobileDetect = \Mockery::mock(MobileDetect::class);
|
||||||
|
$mobileDetect->shouldReceive('isMobile')->andReturn(true);
|
||||||
|
$mobileDetect->shouldReceive('isTablet')->andReturn(true);
|
||||||
|
|
||||||
|
$mode = (new Mode())->determineRunMode($module, $server, $mobileDetect);
|
||||||
|
|
||||||
|
$this->assertTrue($mode->isMobile());
|
||||||
|
$this->assertTrue($mode->isTablet());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test if the call is not a mobile and is not a tablet call
|
||||||
|
*/
|
||||||
|
public function testIsNotMobileIsNotTablet()
|
||||||
|
{
|
||||||
|
$server = [];
|
||||||
|
$module = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, false);
|
||||||
|
$mobileDetect = \Mockery::mock(MobileDetect::class);
|
||||||
|
$mobileDetect->shouldReceive('isMobile')->andReturn(false);
|
||||||
|
$mobileDetect->shouldReceive('isTablet')->andReturn(false);
|
||||||
|
|
||||||
|
$mode = (new Mode())->determineRunMode($module, $server, $mobileDetect);
|
||||||
|
|
||||||
|
$this->assertFalse($mode->isMobile());
|
||||||
|
$this->assertFalse($mode->isTablet());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user