From b947d667d6b024978906b1aebf660f691700368b Mon Sep 17 00:00:00 2001 From: Taras Machyshyn Date: Tue, 13 Jan 2015 13:48:01 +0200 Subject: [PATCH] added 'module.json' - options for modules --- .../Espo/Core/Utils/File/FileUnifier.php | 102 ++++++++++++++++++ application/Espo/Core/Utils/Metadata.php | 42 +++++--- application/Espo/Core/Utils/Module.php | 99 +++++++++++++++++ .../Espo/Modules/Crm/Resources/module.json | 3 + 4 files changed, 233 insertions(+), 13 deletions(-) create mode 100644 application/Espo/Core/Utils/File/FileUnifier.php create mode 100644 application/Espo/Core/Utils/Module.php create mode 100644 application/Espo/Modules/Crm/Resources/module.json diff --git a/application/Espo/Core/Utils/File/FileUnifier.php b/application/Espo/Core/Utils/File/FileUnifier.php new file mode 100644 index 0000000000..eb63131f3e --- /dev/null +++ b/application/Espo/Core/Utils/File/FileUnifier.php @@ -0,0 +1,102 @@ +fileManager = $fileManager; + } + + protected function getFileManager() + { + return $this->fileManager; + } + + /** + * Unite files content + * + * @param array $paths + * @param bool $isReturnModuleNames - If need to return data with module names + * + * @return array + */ + public function unify(array $paths, $isReturnModuleNames = false) + { + $data = $this->loadData($paths['corePath']); + + if (!empty($paths['modulePath'])) { + $moduleDir = strstr($paths['modulePath'], '{*}', true); + $moduleList = $this->getFileManager()->getFileList($moduleDir, false, '', false); + + foreach ($moduleList as $moduleName) { + $moduleFilePath = str_replace('{*}', $moduleName, $paths['modulePath']); + + if ($isReturnModuleNames) { + if (!isset($data[$moduleName])) { + $data[$moduleName] = array(); + } + $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($filePath, $returns = array()) + { + if (file_exists($filePath)) { + $content = $this->getFileManager()->getContents($filePath); + $data = Json::getArrayData($content); + if (empty($data)) { + $GLOBALS['log']->warning('FileUnifier::unify() - Empty file or syntax error - ['.$filePath.']'); + return $returns; + } + + return $data; + } + + return $returns; + } +} diff --git a/application/Espo/Core/Utils/Metadata.php b/application/Espo/Core/Utils/Metadata.php index 7a0702ebb9..10981c65a2 100644 --- a/application/Espo/Core/Utils/Metadata.php +++ b/application/Espo/Core/Utils/Metadata.php @@ -34,6 +34,7 @@ class Metadata private $unifier; private $fileManager; private $converter; + private $moduleConfig; /** * @var string - uses for loading default values @@ -48,13 +49,18 @@ class Metadata 'customPath' => 'custom/Espo/Custom/Resources/metadata', ); - protected $ormMeta = null; private $ormCacheFile = 'data/cache/application/ormMetadata.php'; private $moduleList = null; + /** + * Default module order + * @var integer + */ + protected $defaultModuleOrder = 10; + public function __construct(\Espo\Core\Utils\Config $config, \Espo\Core\Utils\File\Manager $fileManager) { $this->config = $config; @@ -63,6 +69,8 @@ class Metadata $this->unifier = new \Espo\Core\Utils\File\Unifier($this->fileManager); $this->converter = new \Espo\Core\Utils\Database\Converter($this, $this->fileManager); + + $this->moduleConfig = new \Espo\Core\Utils\Module($this->config, $this->fileManager); } protected function getConfig() @@ -85,6 +93,11 @@ class Metadata return $this->converter; } + protected function getModuleConfig() + { + return $this->moduleConfig; + } + public function isCached() { if (!$this->getConfig()->get('useCache')) { @@ -357,23 +370,27 @@ class Metadata */ public function getModuleList() { - if (is_null($this->moduleList)) { - $this->moduleList = array(); - $scopes = $this->getScopes(); + if (!empty($this->moduleList)) { + return $this->moduleList; + } - // TODO order - foreach ($scopes as $moduleName) { - if (!empty($moduleName)) { - if (!in_array($moduleName, $this->moduleList)) { - $this->moduleList[] = $moduleName; - } - } + $scopes = $this->getScopes(); + + $modulesToSort = array(); + foreach ($scopes as $moduleName) { + if (!empty($moduleName) && !isset($modulesToSort[$moduleName])) { + $modulesToSort[$moduleName] = $this->getModuleConfig()->get($moduleName . '.order', $this->defaultModuleOrder); } } + + krsort($modulesToSort); + asort($modulesToSort); + + $this->moduleList = array_keys($modulesToSort); + return $this->moduleList; } - /** * Get module name if it's a custom module or empty string for core entity * @@ -386,7 +403,6 @@ class Metadata return $this->get('scopes.' . $scopeName . '.module', false); } - /** * Get Scope path, ex. "Modules/Crm" for Account * diff --git a/application/Espo/Core/Utils/Module.php b/application/Espo/Core/Utils/Module.php new file mode 100644 index 0000000000..a20bb89408 --- /dev/null +++ b/application/Espo/Core/Utils/Module.php @@ -0,0 +1,99 @@ + 'application/Espo/Resources/module.json', + 'modulePath' => 'application/Espo/Modules/{*}/Resources/module.json', + 'customPath' => 'custom/Espo/Custom/Resources/module.json', + ); + + public function __construct(Config $config, File\Manager $fileManager) + { + $this->config = $config; + $this->fileManager = $fileManager; + + $this->unifier = new \Espo\Core\Utils\File\FileUnifier($this->fileManager); + } + + protected function getConfig() + { + return $this->config; + } + + protected function getFileManager() + { + return $this->fileManager; + } + + protected function getUnifier() + { + return $this->unifier; + } + + public function get($key = '', $returns = null) + { + if (!isset($this->data)) { + $this->init(); + } + + if (empty($key)) { + return $this->data; + } + + return Util::getValueByKey($this->data, $key, $returns); + } + + public function getAll() + { + return $this->get(); + } + + protected function init() + { + if (file_exists($this->cacheFile) && $this->getConfig()->get('useCache')) { + $this->data = $this->getFileManager()->getContents($this->cacheFile); + } else { + $this->data = $this->getUnifier()->unify($this->paths, true); + + if ($this->getConfig()->get('useCache')) { + $result = $this->getFileManager()->putContentsPHP($this->cacheFile, $this->data); + if ($result == false) { + throw new \Espo\Core\Exceptions\Error('Module - Cannot save unified modules.'); + } + } + } + } +} \ No newline at end of file diff --git a/application/Espo/Modules/Crm/Resources/module.json b/application/Espo/Modules/Crm/Resources/module.json new file mode 100644 index 0000000000..8ff9c2dd46 --- /dev/null +++ b/application/Espo/Modules/Crm/Resources/module.json @@ -0,0 +1,3 @@ +{ + "order": 10 +} \ No newline at end of file