1
0
Fork 0

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:
Philipp Holzer 2019-08-16 09:46:38 +02:00
commit 90b438e082
No known key found for this signature in database
GPG key ID: D8365C3D36B77D90
6 changed files with 148 additions and 58 deletions

View file

@ -2,6 +2,7 @@
namespace Friendica\App;
use Detection\MobileDetect;
use Friendica\Core\Config\Cache\ConfigCache;
use Friendica\Database\Database;
use Friendica\Util\BasePath;
@ -29,10 +30,28 @@ class Mode
*/
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->isBackend = $isBackend;
$this->isAjax = $isAjax;
$this->isMobile = $isMobile;
$this->isTablet = $isTablet;
}
/**
@ -81,23 +100,27 @@ class Mode
$mode |= Mode::MAINTENANCEDISABLED;
return new Mode($mode, $this->isBackend);
return new Mode($mode, $this->isBackend, $this->isAjax, $this->isMobile, $this->isTablet);
}
/**
* 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
* @param Module $module The pre-loaded module (just name, not class!)
* @param array $server The $_SERVER variable
* @param MobileDetect $mobileDetect The mobile detection library
*
* @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' ||
$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;
}
/**
* 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;
}
}

View file

@ -245,11 +245,12 @@ class Page implements ArrayAccess
* - footer.tpl template
*
* @param App $app The Friendica App instance
* @param Mode $mode The Friendica runtime mode
* @param L10n $l10n The l10n instance
*
* @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 (!empty($_SESSION['visitor_home'])) {
@ -265,7 +266,7 @@ class Page implements ArrayAccess
/*
* 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']) {
$link = 'toggle_mobile?address=' . urlencode(curPageURL());
} else {
@ -375,9 +376,9 @@ class Page implements ArrayAccess
/* Build the page ending -- this is stuff that goes right before
* 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']);
}