Files
espocrm/application/Espo/Core/Utils/File/FileUnifier.php
Yuri Kuznetsov e50839b7cc refactoring
2021-04-24 18:47:03 +03:00

119 lines
3.6 KiB
PHP

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