This commit is contained in:
Yuri Kuznetsov
2020-09-06 11:12:52 +03:00
parent 551ed091e7
commit b1edce46ff
+31 -28
View File
@@ -29,6 +29,15 @@
namespace Espo\Core\Utils;
use Espo\Core\{
Exceptions\Error,
Utils\File\Manager as FileManager,
Utils\File\FileUnifier,
};
/**
* Gets module parameters.
*/
class Module
{
private $fileManager;
@@ -41,36 +50,24 @@ class Module
protected $cacheFile = 'data/cache/application/modules.php';
protected $paths = array(
protected $paths = [
'corePath' => 'application/Espo/Resources/module.json',
'modulePath' => 'application/Espo/Modules/{*}/Resources/module.json',
'customPath' => 'custom/Espo/Custom/Resources/module.json',
);
];
public function __construct(File\Manager $fileManager, $useCache = false)
public function __construct(FileManager $fileManager, bool $useCache = false)
{
$this->fileManager = $fileManager;
$this->unifier = new \Espo\Core\Utils\File\FileUnifier($this->fileManager);
$this->unifier = new FileUnifier($this->fileManager);
$this->useCache = $useCache;
}
protected function getConfig()
{
return $this->config;
}
protected function getFileManager()
{
return $this->fileManager;
}
protected function getUnifier()
{
return $this->unifier;
}
/**
* Get module parameters.
*/
public function get($key = '', $returns = null)
{
if (!isset($this->data)) {
@@ -84,6 +81,9 @@ class Module
return Util::getValueByKey($this->data, $key, $returns);
}
/**
* Get parameters of all modules.
*/
public function getAll()
{
return $this->get();
@@ -92,16 +92,19 @@ class Module
protected function init()
{
if (file_exists($this->cacheFile) && $this->useCache) {
$this->data = $this->getFileManager()->getPhpContents($this->cacheFile);
} else {
$this->data = $this->getUnifier()->unify($this->paths, true);
$this->data = $this->fileManager->getPhpContents($this->cacheFile);
if ($this->useCache) {
$result = $this->getFileManager()->putPhpContents($this->cacheFile, $this->data);
if ($result == false) {
throw new \Espo\Core\Exceptions\Error('Module - Cannot save unified modules.');
}
return;
}
$this->data = $this->unifier->unify($this->paths, true);
if ($this->useCache) {
$result = $this->fileManager->putPhpContents($this->cacheFile, $this->data);
if ($result === false) {
throw new Error('Module: Could not save unified module data.');
}
}
}
}
}