mirror of
https://github.com/espocrm/espocrm.git
synced 2026-06-28 06:56:05 +00:00
module refactoring
This commit is contained in:
@@ -35,7 +35,7 @@ use Espo\Core\{
|
||||
|
||||
class EspoBindingLoader implements BindingLoader
|
||||
{
|
||||
protected $moduleNameList;
|
||||
private $moduleNameList;
|
||||
|
||||
public function __construct(Module $module)
|
||||
{
|
||||
|
||||
@@ -1,118 +0,0 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM - Open Source CRM application.
|
||||
* Copyright (C) 2014-2021 Yurii Kuznietsov, Taras Machyshyn, Oleksii 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\Utils\File;
|
||||
|
||||
use Espo\Core\Utils\Util;
|
||||
use Espo\Core\Utils\Json;
|
||||
use Espo\Core\Utils\File\Manager;
|
||||
use Espo\Core\Utils\Metadata;
|
||||
|
||||
class FileUnifier
|
||||
{
|
||||
private $fileManager;
|
||||
|
||||
private $metadata;
|
||||
|
||||
public function __construct(Manager $fileManager, ?Metadata $metadata = null)
|
||||
{
|
||||
$this->fileManager = $fileManager;
|
||||
$this->metadata = $metadata;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unite files content.
|
||||
*
|
||||
* @param array $paths
|
||||
* @param bool $returnModuleNames If need to return data with module names.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function unify(array $paths, bool $returnModuleNames = false): array
|
||||
{
|
||||
$data = $this->loadData($paths['corePath']);
|
||||
|
||||
if (!empty($paths['modulePath'])) {
|
||||
$moduleDir = strstr($paths['modulePath'], '{*}', true);
|
||||
|
||||
$moduleList = isset($this->metadata) ?
|
||||
$this->metadata->getModuleList() :
|
||||
$this->fileManager->getFileList($moduleDir, false, '', false);
|
||||
|
||||
foreach ($moduleList as $moduleName) {
|
||||
$moduleFilePath = str_replace('{*}', $moduleName, $paths['modulePath']);
|
||||
|
||||
if ($returnModuleNames) {
|
||||
if (!isset($data[$moduleName])) {
|
||||
$data[$moduleName] = [];
|
||||
}
|
||||
|
||||
$data[$moduleName] = Util::merge(
|
||||
$data[$moduleName],
|
||||
$this->loadData($moduleFilePath)
|
||||
);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$data = Util::merge(
|
||||
$data,
|
||||
$this->loadData($moduleFilePath)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($paths['customPath'])) {
|
||||
$data = Util::merge(
|
||||
$data,
|
||||
$this->loadData($paths['customPath'])
|
||||
);
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load data from a file.
|
||||
*
|
||||
* @param string $filePath
|
||||
* @param array $returns
|
||||
* @return array
|
||||
*/
|
||||
protected function loadData(string $filePath): array
|
||||
{
|
||||
if (!$this->fileManager->isFile($filePath)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$content = $this->fileManager->getContents($filePath);
|
||||
|
||||
return Json::decode($content, true);
|
||||
}
|
||||
}
|
||||
@@ -76,7 +76,16 @@ class Manager
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of files in specified directory.
|
||||
* Get a list of directories in a specified directory.
|
||||
* @return string[]
|
||||
*/
|
||||
public function getDirList(string $path): array
|
||||
{
|
||||
return $this->getFileList($path, false, '', false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of files in a specified directory.
|
||||
*
|
||||
* @param string $path A folder path.
|
||||
* @param bool|int $recursively Find files in sub-folders.
|
||||
|
||||
@@ -31,8 +31,8 @@ namespace Espo\Core\Utils;
|
||||
|
||||
use Espo\Core\{
|
||||
Utils\File\Manager as FileManager,
|
||||
Utils\File\FileUnifier,
|
||||
Utils\DataCache,
|
||||
Utils\Json,
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -40,23 +40,19 @@ use Espo\Core\{
|
||||
*/
|
||||
class Module
|
||||
{
|
||||
private const DEFAULT_ORDER = 10;
|
||||
private const DEFAULT_ORDER = 11;
|
||||
|
||||
private $useCache;
|
||||
|
||||
private $unifier;
|
||||
|
||||
private $data = null;
|
||||
|
||||
private $list = null;
|
||||
|
||||
private $cacheKey = 'modules';
|
||||
|
||||
private $pathToModules = 'application/Espo/Modules';
|
||||
|
||||
private $paths = [
|
||||
'corePath' => 'application/Espo/Resources/module.json',
|
||||
'modulePath' => 'application/Espo/Modules/{*}/Resources/module.json',
|
||||
'customPath' => 'custom/Espo/Custom/Resources/module.json',
|
||||
];
|
||||
private $moduleFilePath = 'Resources/module.json';
|
||||
|
||||
private $fileManager;
|
||||
|
||||
@@ -71,38 +67,30 @@ class Module
|
||||
$this->fileManager = $fileManager;
|
||||
$this->dataCache = $dataCache;
|
||||
|
||||
$this->unifier = new FileUnifier($this->fileManager);
|
||||
|
||||
$this->useCache = $useCache;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get module parameters.
|
||||
*
|
||||
* @param string|array|null $key
|
||||
* @param mixed $defaultValue
|
||||
* @return mixed
|
||||
*/
|
||||
public function get($key = '', $returns = null)
|
||||
public function get($key = null, $defaultValue = null)
|
||||
{
|
||||
if (!isset($this->data)) {
|
||||
if ($this->data === null) {
|
||||
$this->init();
|
||||
}
|
||||
|
||||
if (empty($key)) {
|
||||
if ($key === null) {
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
return Util::getValueByKey($this->data, $key, $returns);
|
||||
return Util::getValueByKey($this->data, $key, $defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get parameters of all modules.
|
||||
*/
|
||||
public function getAll(): array
|
||||
{
|
||||
return $this->get();
|
||||
}
|
||||
|
||||
protected function init(): void
|
||||
private function init(): void
|
||||
{
|
||||
if ($this->useCache && $this->dataCache->has($this->cacheKey)) {
|
||||
$this->data = $this->dataCache->get($this->cacheKey);
|
||||
@@ -110,7 +98,7 @@ class Module
|
||||
return;
|
||||
}
|
||||
|
||||
$this->data = $this->unifier->unify($this->paths, true);
|
||||
$this->data = $this->loadData();
|
||||
|
||||
if ($this->useCache) {
|
||||
$this->dataCache->store($this->cacheKey, $this->data);
|
||||
@@ -120,19 +108,17 @@ class Module
|
||||
/**
|
||||
* Get an ordered list of modules.
|
||||
*
|
||||
* @return string[]
|
||||
*
|
||||
* @todo Use cache if available.
|
||||
*/
|
||||
public function getOrderedList(): array
|
||||
{
|
||||
$modules = $this->fileManager->getFileList($this->pathToModules, false, '', false);
|
||||
$moduleNameList = $this->getList();
|
||||
|
||||
$modulesToSort = [];
|
||||
|
||||
if (!is_array($modules)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
foreach ($modules as $moduleName) {
|
||||
foreach ($moduleNameList as $moduleName) {
|
||||
if (empty($moduleName)) {
|
||||
continue;
|
||||
}
|
||||
@@ -154,4 +140,30 @@ class Module
|
||||
|
||||
return array_keys($modulesToSort);
|
||||
}
|
||||
|
||||
private function getList(): array
|
||||
{
|
||||
if ($this->list === null) {
|
||||
$this->list = $this->fileManager->getDirList($this->pathToModules);
|
||||
}
|
||||
|
||||
return $this->list;
|
||||
}
|
||||
|
||||
private function loadData(): array
|
||||
{
|
||||
$data = [];
|
||||
|
||||
foreach ($this->getList() as $moduleName) {
|
||||
$path = $this->pathToModules . '/' . $moduleName . '/' . $this->moduleFilePath;
|
||||
|
||||
$itemContents = $this->fileManager->getContents($path);
|
||||
|
||||
$data[$moduleName] = Json::decode($itemContents, true);
|
||||
|
||||
$data[$moduleName]['order'] = $data[$moduleName]['order'] ?? self::DEFAULT_ORDER;
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -484,9 +484,9 @@ class Util
|
||||
/**
|
||||
* Return values of defined $key.
|
||||
*
|
||||
* @param mixed $data
|
||||
* @param mixed array|string $key Ex. of key is "entityDefs", "entityDefs.User"
|
||||
* @param mixed $default
|
||||
* @param mixed $data
|
||||
* @param mixed array|string $key Ex. of key is "entityDefs", "entityDefs.User"
|
||||
* @param mixed $default
|
||||
* @return mixed
|
||||
*/
|
||||
public static function getValueByKey($data, $key = null, $default = null)
|
||||
|
||||
Reference in New Issue
Block a user