portal config refactoring and type hinring

This commit is contained in:
Yuri Kuznetsov
2020-06-28 11:35:39 +03:00
parent 078bdfc28f
commit 562cd42344
6 changed files with 149 additions and 74 deletions

View File

@@ -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());

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -0,0 +1,42 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2020 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
* Website: https://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Portal\Loaders;
use Espo\Core\Portal\Utils\Config as ConfigService;
use Espo\Core\Loaders\Loader;
use Espo\Core\Utils\File\Manager as FileManager;
class Config implements Loader
{
public function load()
{
return new ConfigService(new FileManager());
}
}

View File

@@ -0,0 +1,65 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2020 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
* Website: https://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Portal\Utils;
use Espo\Core\Utils\Config as BaseConfig;
class Config extends BaseConfig
{
/**
* Override parameters for a portal.
*/
public function setPortalParameters(array $data = [])
{
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 (empty($data['defaultCurrency'])) unset($data['defaultCurrency']);
if (isset($data['weekStart']) && $data['weekStart'] === -1) unset($data['weekStart']);
if (array_key_exists('weekStart', $data) && is_null($data['weekStart'])) unset($data['weekStart']);
if ($this->get('webSocketInPortalDisabled')) {
$this->set('useWebSocket', false, true);
}
foreach ($data as $attribute => $value) {
$this->set($attribute, $value, true);
}
}
/**
* Save is disabled.
*/
public function save()
{
}
}

View File

@@ -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'), '/');
}