From 562cd42344b46d3e89bc8b86d31d241114f8aad2 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Sun, 28 Jun 2020 11:35:39 +0300 Subject: [PATCH] portal config refactoring and type hinring --- application/Espo/Core/Loaders/Config.php | 4 - application/Espo/Core/Portal/Application.php | 3 + application/Espo/Core/Portal/Container.php | 33 +------- .../Espo/Core/Portal/Loaders/Config.php | 42 ++++++++++ application/Espo/Core/Portal/Utils/Config.php | 65 ++++++++++++++++ application/Espo/Core/Utils/Config.php | 76 +++++++++---------- 6 files changed, 149 insertions(+), 74 deletions(-) create mode 100644 application/Espo/Core/Portal/Loaders/Config.php create mode 100644 application/Espo/Core/Portal/Utils/Config.php diff --git a/application/Espo/Core/Loaders/Config.php b/application/Espo/Core/Loaders/Config.php index 38f7bb7202..432ea75802 100644 --- a/application/Espo/Core/Loaders/Config.php +++ b/application/Espo/Core/Loaders/Config.php @@ -34,10 +34,6 @@ use Espo\Core\Utils\File\Manager as FileManager; class Config implements Loader { - public function __construct() - { - } - public function load() { return new ConfigService(new FileManager()); diff --git a/application/Espo/Core/Portal/Application.php b/application/Espo/Core/Portal/Application.php index 9250385c78..5620db4ed2 100644 --- a/application/Espo/Core/Portal/Application.php +++ b/application/Espo/Core/Portal/Application.php @@ -38,6 +38,7 @@ use Espo\Core\Exceptions\{ use Espo\Core\{ Portal\Container as PortalContainer, Portal\ContainerConfiguration as PortalContainerConfiguration, + Portal\Loaders\Config as ConfigLoader, Application as BaseApplication, }; @@ -57,6 +58,8 @@ class Application extends BaseApplication protected function initContainer() { + $this->loaderClassNames['config'] = ConfigLoader::class; + $this->container = new PortalContainer(PortalContainerConfiguration::class, $this->loaderClassNames); } diff --git a/application/Espo/Core/Portal/Container.php b/application/Espo/Core/Portal/Container.php index d7113f35ce..3d49083476 100644 --- a/application/Espo/Core/Portal/Container.php +++ b/application/Espo/Core/Portal/Container.php @@ -43,38 +43,7 @@ class Container extends BaseContainer foreach ($this->get('portal')->getSettingsAttributeList() as $attribute) { $data[$attribute] = $this->get('portal')->get($attribute); } - if (empty($data['language'])) { - unset($data['language']); - } - if (empty($data['theme'])) { - unset($data['theme']); - } - if (empty($data['timeZone'])) { - unset($data['timeZone']); - } - if (empty($data['dateFormat'])) { - unset($data['dateFormat']); - } - if (empty($data['timeFormat'])) { - unset($data['timeFormat']); - } - if (isset($data['weekStart']) && $data['weekStart'] === -1) { - unset($data['weekStart']); - } - if (array_key_exists('weekStart', $data) && is_null($data['weekStart'])) { - unset($data['weekStart']); - } - if (empty($data['defaultCurrency'])) { - unset($data['defaultCurrency']); - } - - if ($this->get('config')->get('webSocketInPortalDisabled')) { - $this->get('config')->set('useWebSocket', false); - } - - foreach ($data as $attribute => $value) { - $this->get('config')->set($attribute, $value, true); - } + $this->get('config')->setPortalParameters($data); $this->get('aclManager')->setPortal($portal); } diff --git a/application/Espo/Core/Portal/Loaders/Config.php b/application/Espo/Core/Portal/Loaders/Config.php new file mode 100644 index 0000000000..775d2e9c15 --- /dev/null +++ b/application/Espo/Core/Portal/Loaders/Config.php @@ -0,0 +1,42 @@ +get('webSocketInPortalDisabled')) { + $this->set('useWebSocket', false, true); + } + + foreach ($data as $attribute => $value) { + $this->set($attribute, $value, true); + } + } + + /** + * Save is disabled. + */ + public function save() + { + } +} diff --git a/application/Espo/Core/Utils/Config.php b/application/Espo/Core/Utils/Config.php index d99f335d66..238edcd42e 100644 --- a/application/Espo/Core/Utils/Config.php +++ b/application/Espo/Core/Utils/Config.php @@ -31,6 +31,8 @@ namespace Espo\Core\Utils; use Espo\Core\Exceptions\Error; +use Espo\Core\Utils\File\Manager as FileManager; + /** * Reads and writes the main config file. */ @@ -61,8 +63,7 @@ class Config private $fileManager; - - public function __construct(\Espo\Core\Utils\File\Manager $fileManager) + public function __construct(FileManager $fileManager) { $this->fileManager = $fileManager; } @@ -78,13 +79,11 @@ class Config } /** - * Get an option from config + * Get a parameter value. * - * @param string $name - * @param string $default - * @return string | array + * @return mixed */ - public function get($name, $default = null) + public function get(string $name, $default = null) { $keys = explode('.', $name); @@ -105,12 +104,9 @@ class Config } /** - * Whether parameter is set - * - * @param string $name - * @return bool + * Whether a parameter is set. */ - public function has($name) + public function has(string $name) : bool { $keys = explode('.', $name); @@ -131,13 +127,9 @@ class Config } /** - * Set an option to the config - * - * @param string $name - * @param string $value - * @return bool + * Set a parameter or multiple parameters. */ - public function set($name, $value = null, $dontMarkDirty = false) + public function set($name, $value = null, bool $dontMarkDirty = false) { if (is_object($name)) { $name = get_object_vars($name); @@ -159,12 +151,9 @@ class Config } /** - * Remove an option in config - * - * @param string $name - * @return bool | null - null if an option doesn't exist + * Remove a parameter.. */ - public function remove($name) + public function remove(string $name) : bool { if (array_key_exists($name, $this->data)) { unset($this->data[$name]); @@ -172,9 +161,12 @@ class Config return true; } - return null; + return false; } + /** + * Save config changes to the file. + */ public function save() { $values = $this->changedData; @@ -233,12 +225,15 @@ class Config return $result; } - public function getDefaults() + /** + * Get system default config parameters. + */ + public function getDefaults() : array { return $this->getFileManager()->getPhpContents($this->defaultConfigPath); } - protected function loadConfig($reload = false) + protected function loadConfig(bool $reload = false) { if (!$reload && isset($this->data) && !empty($this->data)) { return $this->data; @@ -254,18 +249,23 @@ class Config return $this->data; } - public function getAllData() + /** + * Get all parameters. + */ + public function getAllData() : \StdClass { return (object) $this->loadConfig(); } - public function getData($isAdmin = null) + /** @deprecated */ + public function getData() { $data = $this->loadConfig(); return $data; } + /** @deprecated */ public function setData($data) { if (is_object($data)) { @@ -276,50 +276,50 @@ class Config } /** - * Update cache timestamp + * Update cache timestamp. * - * @param $onlyValue - If need to return just timestamp array + * @param $returnOnlyValue - To return an array with timestamp. * @return bool | array */ - public function updateCacheTimestamp($onlyValue = false) + public function updateCacheTimestamp(bool $returnOnlyValue = false) { $timestamp = [ $this->cacheTimestamp => time() ]; - if ($onlyValue) { + if ($returnOnlyValue) { return $timestamp; } return $this->set($timestamp); } - public function getAdminOnlyItemList() + public function getAdminOnlyItemList() : array { return $this->get('adminItems', []); } - public function getSuperAdminOnlyItemList() + public function getSuperAdminOnlyItemList() : array { return $this->get('superAdminItems', []); } - public function getSystemOnlyItemList() + public function getSystemOnlyItemList() : array { return $this->get('systemItems', []); } - public function getSuperAdminOnlySystemItemList() + public function getSuperAdminOnlySystemItemList() : array { return $this->get('superAdminSystemItems', []); } - public function getUserOnlyItemList() + public function getUserOnlyItemList() : array { return $this->get('userItems', []); } - public function getSiteUrl() + public function getSiteUrl() : string { return rtrim($this->get('siteUrl'), '/'); }