diff --git a/application/Espo/Core/Binding/EspoBindingLoader.php b/application/Espo/Core/Binding/EspoBindingLoader.php index f90f950f54..4ce2176433 100644 --- a/application/Espo/Core/Binding/EspoBindingLoader.php +++ b/application/Espo/Core/Binding/EspoBindingLoader.php @@ -35,7 +35,7 @@ use Espo\Core\{ class EspoBindingLoader implements BindingLoader { - protected $moduleNameList; + private $moduleNameList; public function __construct(Module $module) { diff --git a/application/Espo/Core/Utils/File/FileUnifier.php b/application/Espo/Core/Utils/File/FileUnifier.php deleted file mode 100644 index 36151825c2..0000000000 --- a/application/Espo/Core/Utils/File/FileUnifier.php +++ /dev/null @@ -1,118 +0,0 @@ -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); - } -} diff --git a/application/Espo/Core/Utils/File/Manager.php b/application/Espo/Core/Utils/File/Manager.php index 128550ca72..d751a989a4 100644 --- a/application/Espo/Core/Utils/File/Manager.php +++ b/application/Espo/Core/Utils/File/Manager.php @@ -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. diff --git a/application/Espo/Core/Utils/Module.php b/application/Espo/Core/Utils/Module.php index ba2b1023ca..da94fca826 100644 --- a/application/Espo/Core/Utils/Module.php +++ b/application/Espo/Core/Utils/Module.php @@ -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; + } } diff --git a/application/Espo/Core/Utils/Util.php b/application/Espo/Core/Utils/Util.php index 75ad7cfebb..d6d7c24bc2 100644 --- a/application/Espo/Core/Utils/Util.php +++ b/application/Espo/Core/Utils/Util.php @@ -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)