From 9477e1b55f19450bab9c4e451004440181e3039d Mon Sep 17 00:00:00 2001 From: Art4 Date: Fri, 8 Nov 2024 16:07:24 +0000 Subject: [PATCH] Move AppHelper code to AppLegacy, create interface for AppHelper --- src/App.php | 2 +- src/AppHelper.php | 304 ++----------------------- src/AppLegacy.php | 399 +++++++++++++++++++++++++++++++++ static/dependencies.config.php | 5 + 4 files changed, 426 insertions(+), 284 deletions(-) create mode 100644 src/AppLegacy.php diff --git a/src/App.php b/src/App.php index b4f7c520fe..e8b41c9996 100644 --- a/src/App.php +++ b/src/App.php @@ -46,7 +46,7 @@ use Psr\Log\LoggerInterface; * before we spit the page out. * */ -class App +class App implements AppHelper { const PLATFORM = 'Friendica'; const CODENAME = 'Yellow Archangel'; diff --git a/src/AppHelper.php b/src/AppHelper.php index 599c15d041..66d3c61d9a 100644 --- a/src/AppHelper.php +++ b/src/AppHelper.php @@ -7,21 +7,8 @@ namespace Friendica; -use DateTimeZone; use Exception; -use Friendica\App\BaseURL; -use Friendica\App\Mode; -use Friendica\Core\Config\Capability\IManageConfigValues; use Friendica\Core\Config\ValueObject\Cache; -use Friendica\Core\L10n; -use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues; -use Friendica\Core\Session\Capability\IHandleUserSessions; -use Friendica\Core\System; -use Friendica\Core\Theme; -use Friendica\Database\Database; -use Friendica\Network\HTTPException\InternalServerErrorException; -use Friendica\Util\DateTimeFormat; -use Friendica\Util\Strings; /** * Helper for our main application structure for the life of this page. @@ -33,153 +20,53 @@ use Friendica\Util\Strings; * before we spit the page out. * */ -final class AppHelper +interface AppHelper { - private $profile_owner = 0; - - private $timezone = ''; - - private $contact_id = 0; - - private $queue = []; - - /** @var string The name of the current theme */ - private $currentTheme; - - /** @var string The name of the current mobile theme */ - private $currentMobileTheme; - - // Allow themes to control internal parameters - // by changing App values in theme.php - private $theme_info = [ - 'videowidth' => 425, - 'videoheight' => 350, - ]; - - /** - * @var Database The Friendica database connection - */ - private $database; - - /** - * @var IManageConfigValues The config - */ - private $config; - - /** - * @var Mode The Mode of the Application - */ - private $mode; - - /** - * @var BaseURL - */ - private $baseURL; - - /** - * @var L10n The translator - */ - private $l10n; - - /** - * @var IManagePersonalConfigValues - */ - private $pConfig; - - /** - * @var IHandleUserSessions - */ - private $session; - - public function __construct( - Database $database, - IManageConfigValues $config, - Mode $mode, - BaseURL $baseURL, - L10n $l10n, - IManagePersonalConfigValues $pConfig, - IHandleUserSessions $session - ) { - $this->database = $database; - $this->config = $config; - $this->mode = $mode; - $this->l10n = $l10n; - $this->baseURL = $baseURL; - $this->pConfig = $pConfig; - $this->session = $session; - } - /** * Set the profile owner ID */ - public function setProfileOwner(int $owner_id): void - { - $this->profile_owner = $owner_id; - } + public function setProfileOwner(int $owner_id); /** * Get the profile owner ID */ - public function getProfileOwner(): int - { - return $this->profile_owner; - } + public function getProfileOwner(): int; /** * Set the timezone * * @param string $timezone A valid time zone identifier, see https://www.php.net/manual/en/timezones.php */ - public function setTimeZone(string $timezone): void - { - $this->timezone = (new DateTimeZone($timezone))->getName(); - - DateTimeFormat::setLocalTimeZone($this->timezone); - } + public function setTimeZone(string $timezone); /** * Get the timezone name */ - public function getTimeZone(): string - { - return $this->timezone; - } + public function getTimeZone(): string; /** * Set the contact ID */ - public function setContactId(int $contact_id): void - { - $this->contact_id = $contact_id; - } + public function setContactId(int $contact_id); /** * Get the contact ID */ - public function getContactId(): int - { - return $this->contact_id; - } + public function getContactId(): int; /** * Set workerqueue information * * @param array $queue */ - public function setQueue(array $queue): void - { - $this->queue = $queue; - } + public function setQueue(array $queue); /** * Fetch workerqueue information * * @return array Worker queue */ - public function getQueue(): array - { - return $this->queue; - } + public function getQueue(); /** * Fetch a specific workerqueue field @@ -188,10 +75,7 @@ final class AppHelper * * @return mixed|null Work queue item or NULL if not found */ - public function getQueueValue(string $index) - { - return $this->queue[$index] ?? null; - } + public function getQueueValue(string $index); /** * Returns the current theme name. May be overridden by the mobile theme name. @@ -199,28 +83,7 @@ final class AppHelper * @return string Current theme name or empty string in installation phase * @throws Exception */ - public function getCurrentTheme(): string - { - if ($this->mode->isInstall()) { - return ''; - } - - // Specific mobile theme override - if (($this->mode->isMobile() || $this->mode->isTablet()) && $this->session->get('show-mobile', true)) { - $user_mobile_theme = $this->getCurrentMobileTheme(); - - // --- means same mobile theme as desktop - if (!empty($user_mobile_theme) && $user_mobile_theme !== '---') { - return $user_mobile_theme; - } - } - - if (!$this->currentTheme) { - $this->computeCurrentTheme(); - } - - return $this->currentTheme; - } + public function getCurrentTheme(): string; /** * Returns the current mobile theme name. @@ -228,53 +91,27 @@ final class AppHelper * @return string Mobile theme name or empty string if installer * @throws Exception */ - public function getCurrentMobileTheme(): string - { - if ($this->mode->isInstall()) { - return ''; - } - - if (is_null($this->currentMobileTheme)) { - $this->computeCurrentMobileTheme(); - } - - return $this->currentMobileTheme; - } + public function getCurrentMobileTheme(): string; /** * Setter for current theme name * * @param string $theme Name of current theme */ - public function setCurrentTheme(string $theme): void - { - $this->currentTheme = $theme; - } + public function setCurrentTheme(string $theme); /** * Setter for current mobile theme name * * @param string $theme Name of current mobile theme */ - public function setCurrentMobileTheme(string $theme): void - { - $this->currentMobileTheme = $theme; - } + public function setCurrentMobileTheme(string $theme); - public function setThemeInfoValue(string $index, $value): void - { - $this->theme_info[$index] = $value; - } + public function setThemeInfoValue(string $index, $value); - public function getThemeInfo(): array - { - return $this->theme_info; - } + public function getThemeInfo(); - public function getThemeInfoValue(string $index, $default = null) - { - return $this->theme_info[$index] ?? $default; - } + public function getThemeInfoValue(string $index, $default = null); /** * Provide a sane default if nothing is chosen or the specified theme does not exist. @@ -282,120 +119,21 @@ final class AppHelper * @return string Current theme's stylesheet path * @throws Exception */ - public function getCurrentThemeStylesheetPath(): string - { - return Theme::getStylesheetPath($this->getCurrentTheme()); - } + public function getCurrentThemeStylesheetPath(): string; /** * Returns the current config cache of this node * * @return Cache */ - public function getConfigCache(): Cache - { - return $this->config->getCache(); - } + public function getConfigCache(); /** * The basepath of this app * * @return string Base path from configuration */ - public function getBasePath(): string - { - return $this->config->get('system', 'basepath'); - } - - /** - * Computes the current theme name based on the node settings, the page owner settings and the user settings - * - * @throws Exception - */ - private function computeCurrentTheme() - { - $system_theme = $this->config->get('system', 'theme'); - if (!$system_theme) { - throw new Exception($this->l10n->t('No system theme config value set.')); - } - - // Sane default - $this->setCurrentTheme($system_theme); - - $page_theme = null; - $profile_owner = $this->getProfileOwner(); - - // Find the theme that belongs to the user whose stuff we are looking at - if (!empty($profile_owner) && ($profile_owner != $this->session->getLocalUserId())) { - // Allow folks to override user themes and always use their own on their own site. - // This works only if the user is on the same server - $user = $this->database->selectFirst('user', ['theme'], ['uid' => $profile_owner]); - if ($this->database->isResult($user) && !$this->session->getLocalUserId()) { - $page_theme = $user['theme']; - } - } + public function getBasePath(): string; - $theme_name = $page_theme ?: $this->session->get('theme', $system_theme); - - $theme_name = Strings::sanitizeFilePathItem($theme_name); - if ($theme_name - && in_array($theme_name, Theme::getAllowedList()) - && (file_exists('view/theme/' . $theme_name . '/style.css') - || file_exists('view/theme/' . $theme_name . '/style.php')) - ) { - $this->setCurrentTheme($theme_name); - } - } - - /** - * Computes the current mobile theme name based on the node settings, the page owner settings and the user settings - */ - private function computeCurrentMobileTheme() - { - $system_mobile_theme = $this->config->get('system', 'mobile-theme', ''); - - // Sane default - $this->setCurrentMobileTheme($system_mobile_theme); - - $page_mobile_theme = null; - $profile_owner = $this->getProfileOwner(); - - // Find the theme that belongs to the user whose stuff we are looking at - if (!empty($profile_owner) && ($profile_owner != $this->session->getLocalUserId())) { - // Allow folks to override user themes and always use their own on their own site. - // This works only if the user is on the same server - if (!$this->session->getLocalUserId()) { - $page_mobile_theme = $this->pConfig->get($profile_owner, 'system', 'mobile-theme'); - } - } - - $mobile_theme_name = $page_mobile_theme ?: $this->session->get('mobile-theme', $system_mobile_theme); - - $mobile_theme_name = Strings::sanitizeFilePathItem($mobile_theme_name); - if ($mobile_theme_name == '---' - || - in_array($mobile_theme_name, Theme::getAllowedList()) - && (file_exists('view/theme/' . $mobile_theme_name . '/style.css') - || file_exists('view/theme/' . $mobile_theme_name . '/style.php')) - ) { - $this->setCurrentMobileTheme($mobile_theme_name); - } - } - - /** - * Automatically redirects to relative or absolute URL - * Should only be used if it isn't clear if the URL is either internal or external - * - * @param string $toUrl The target URL - * - * @throws InternalServerErrorException - */ - public function redirect(string $toUrl) - { - if (!empty(parse_url($toUrl, PHP_URL_SCHEME))) { - System::externalRedirect($toUrl); - } else { - $this->baseURL->redirect($toUrl); - } - } + public function redirect(string $toUrl); } diff --git a/src/AppLegacy.php b/src/AppLegacy.php new file mode 100644 index 0000000000..49375d02be --- /dev/null +++ b/src/AppLegacy.php @@ -0,0 +1,399 @@ + 425, + 'videoheight' => 350, + ]; + + /** + * @var Database The Friendica database connection + */ + private $database; + + /** + * @var IManageConfigValues The config + */ + private $config; + + /** + * @var Mode The Mode of the Application + */ + private $mode; + + /** + * @var BaseURL + */ + private $baseURL; + + /** + * @var L10n The translator + */ + private $l10n; + + /** + * @var IManagePersonalConfigValues + */ + private $pConfig; + + /** + * @var IHandleUserSessions + */ + private $session; + + public function __construct( + Database $database, + IManageConfigValues $config, + Mode $mode, + BaseURL $baseURL, + L10n $l10n, + IManagePersonalConfigValues $pConfig, + IHandleUserSessions $session + ) { + $this->database = $database; + $this->config = $config; + $this->mode = $mode; + $this->l10n = $l10n; + $this->baseURL = $baseURL; + $this->pConfig = $pConfig; + $this->session = $session; + } + + /** + * Set the profile owner ID + */ + public function setProfileOwner(int $owner_id): void + { + $this->profile_owner = $owner_id; + } + + /** + * Get the profile owner ID + */ + public function getProfileOwner(): int + { + return $this->profile_owner; + } + + /** + * Set the timezone + * + * @param string $timezone A valid time zone identifier, see https://www.php.net/manual/en/timezones.php + */ + public function setTimeZone(string $timezone): void + { + $this->timezone = (new DateTimeZone($timezone))->getName(); + + DateTimeFormat::setLocalTimeZone($this->timezone); + } + + /** + * Get the timezone name + */ + public function getTimeZone(): string + { + return $this->timezone; + } + + /** + * Set the contact ID + */ + public function setContactId(int $contact_id): void + { + $this->contact_id = $contact_id; + } + + /** + * Get the contact ID + */ + public function getContactId(): int + { + return $this->contact_id; + } + + /** + * Set workerqueue information + * + * @param array $queue + */ + public function setQueue(array $queue): void + { + $this->queue = $queue; + } + + /** + * Fetch workerqueue information + * + * @return array Worker queue + */ + public function getQueue(): array + { + return $this->queue; + } + + /** + * Fetch a specific workerqueue field + * + * @param string $index Work queue record to fetch + * + * @return mixed|null Work queue item or NULL if not found + */ + public function getQueueValue(string $index) + { + return $this->queue[$index] ?? null; + } + + /** + * Returns the current theme name. May be overridden by the mobile theme name. + * + * @return string Current theme name or empty string in installation phase + * @throws Exception + */ + public function getCurrentTheme(): string + { + if ($this->mode->isInstall()) { + return ''; + } + + // Specific mobile theme override + if (($this->mode->isMobile() || $this->mode->isTablet()) && $this->session->get('show-mobile', true)) { + $user_mobile_theme = $this->getCurrentMobileTheme(); + + // --- means same mobile theme as desktop + if (!empty($user_mobile_theme) && $user_mobile_theme !== '---') { + return $user_mobile_theme; + } + } + + if (!$this->currentTheme) { + $this->computeCurrentTheme(); + } + + return $this->currentTheme; + } + + /** + * Returns the current mobile theme name. + * + * @return string Mobile theme name or empty string if installer + * @throws Exception + */ + public function getCurrentMobileTheme(): string + { + if ($this->mode->isInstall()) { + return ''; + } + + if (is_null($this->currentMobileTheme)) { + $this->computeCurrentMobileTheme(); + } + + return $this->currentMobileTheme; + } + + /** + * Setter for current theme name + * + * @param string $theme Name of current theme + */ + public function setCurrentTheme(string $theme): void + { + $this->currentTheme = $theme; + } + + /** + * Setter for current mobile theme name + * + * @param string $theme Name of current mobile theme + */ + public function setCurrentMobileTheme(string $theme): void + { + $this->currentMobileTheme = $theme; + } + + public function setThemeInfoValue(string $index, $value): void + { + $this->theme_info[$index] = $value; + } + + public function getThemeInfo(): array + { + return $this->theme_info; + } + + public function getThemeInfoValue(string $index, $default = null) + { + return $this->theme_info[$index] ?? $default; + } + + /** + * Provide a sane default if nothing is chosen or the specified theme does not exist. + * + * @return string Current theme's stylesheet path + * @throws Exception + */ + public function getCurrentThemeStylesheetPath(): string + { + return Theme::getStylesheetPath($this->getCurrentTheme()); + } + + /** + * Returns the current config cache of this node + */ + public function getConfigCache(): Cache + { + return $this->config->getCache(); + } + + /** + * The basepath of this app + * + * @return string Base path from configuration + */ + public function getBasePath(): string + { + return $this->config->get('system', 'basepath'); + } + + /** + * Computes the current theme name based on the node settings, the page owner settings and the user settings + * + * @throws Exception + */ + private function computeCurrentTheme() + { + $system_theme = $this->config->get('system', 'theme'); + if (!$system_theme) { + throw new Exception($this->l10n->t('No system theme config value set.')); + } + + // Sane default + $this->setCurrentTheme($system_theme); + + $page_theme = null; + $profile_owner = $this->getProfileOwner(); + + // Find the theme that belongs to the user whose stuff we are looking at + if (!empty($profile_owner) && ($profile_owner != $this->session->getLocalUserId())) { + // Allow folks to override user themes and always use their own on their own site. + // This works only if the user is on the same server + $user = $this->database->selectFirst('user', ['theme'], ['uid' => $profile_owner]); + if ($this->database->isResult($user) && !$this->session->getLocalUserId()) { + $page_theme = $user['theme']; + } + } + + $theme_name = $page_theme ?: $this->session->get('theme', $system_theme); + + $theme_name = Strings::sanitizeFilePathItem($theme_name); + if ($theme_name + && in_array($theme_name, Theme::getAllowedList()) + && (file_exists('view/theme/' . $theme_name . '/style.css') + || file_exists('view/theme/' . $theme_name . '/style.php')) + ) { + $this->setCurrentTheme($theme_name); + } + } + + /** + * Computes the current mobile theme name based on the node settings, the page owner settings and the user settings + */ + private function computeCurrentMobileTheme() + { + $system_mobile_theme = $this->config->get('system', 'mobile-theme', ''); + + // Sane default + $this->setCurrentMobileTheme($system_mobile_theme); + + $page_mobile_theme = null; + $profile_owner = $this->getProfileOwner(); + + // Find the theme that belongs to the user whose stuff we are looking at + if (!empty($profile_owner) && ($profile_owner != $this->session->getLocalUserId())) { + // Allow folks to override user themes and always use their own on their own site. + // This works only if the user is on the same server + if (!$this->session->getLocalUserId()) { + $page_mobile_theme = $this->pConfig->get($profile_owner, 'system', 'mobile-theme'); + } + } + + $mobile_theme_name = $page_mobile_theme ?: $this->session->get('mobile-theme', $system_mobile_theme); + + $mobile_theme_name = Strings::sanitizeFilePathItem($mobile_theme_name); + if ($mobile_theme_name == '---' + || + in_array($mobile_theme_name, Theme::getAllowedList()) + && (file_exists('view/theme/' . $mobile_theme_name . '/style.css') + || file_exists('view/theme/' . $mobile_theme_name . '/style.php')) + ) { + $this->setCurrentMobileTheme($mobile_theme_name); + } + } + + /** + * Automatically redirects to relative or absolute URL + * Should only be used if it isn't clear if the URL is either internal or external + * + * @param string $toUrl The target URL + * + * @throws InternalServerErrorException + */ + public function redirect(string $toUrl) + { + if (!empty(parse_url($toUrl, PHP_URL_SCHEME))) { + System::externalRedirect($toUrl); + } else { + $this->baseURL->redirect($toUrl); + } + } +} diff --git a/static/dependencies.config.php b/static/dependencies.config.php index 0a1e56b873..615319336b 100644 --- a/static/dependencies.config.php +++ b/static/dependencies.config.php @@ -23,6 +23,8 @@ use Dice\Dice; use Friendica\App; +use Friendica\AppHelper; +use Friendica\AppLegacy; use Friendica\Core\Cache; use Friendica\Core\Config; use Friendica\Core\Hooks\Capability\ICanCreateInstances; @@ -93,6 +95,9 @@ return [ [Dice::INSTANCE => Dice::SELF], ], ], + AppHelper::class => [ + 'instanceOf' => AppLegacy::class, + ], ICanCreateInstances::class => [ 'instanceOf' => DiceInstanceManager::class, 'constructParams' => [ -- 2.39.5