Initial commit
This commit is contained in:
115
application/Espo/Core/Utils/Resource/FileReader.php
Normal file
115
application/Espo/Core/Utils/Resource/FileReader.php
Normal file
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://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 Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Core\Utils\Resource;
|
||||
|
||||
use Espo\Core\Utils\File\Manager as FileManager;
|
||||
use Espo\Core\Utils\Metadata;
|
||||
use Espo\Core\Utils\Resource\FileReader\Params;
|
||||
use RuntimeException;
|
||||
|
||||
/**
|
||||
* Reads resource files.
|
||||
*/
|
||||
class FileReader
|
||||
{
|
||||
public function __construct(
|
||||
private FileManager $fileManager,
|
||||
private Metadata $metadata,
|
||||
private PathProvider $pathProvider
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Read a resource file. Returns NULL if the file does not exist.
|
||||
*
|
||||
* @throws RuntimeException If the resource does not exist.
|
||||
*/
|
||||
public function read(string $path, Params $params): string
|
||||
{
|
||||
$exactPath = $this->findExactPath($path, $params);
|
||||
|
||||
if (!$exactPath) {
|
||||
throw new RuntimeException("Resource file '$path' does not exist.");
|
||||
}
|
||||
|
||||
return $this->fileManager->getContents($exactPath);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a resource file exists.
|
||||
*/
|
||||
public function exists(string $path, Params $params): bool
|
||||
{
|
||||
return $this->findExactPath($path, $params) !== null;
|
||||
}
|
||||
|
||||
private function findExactPath(string $path, Params $params): ?string
|
||||
{
|
||||
$customPath = $this->pathProvider->getCustom() . $path;
|
||||
|
||||
if ($this->fileManager->isFile($customPath)) {
|
||||
return $customPath;
|
||||
}
|
||||
|
||||
$moduleName = null;
|
||||
|
||||
if ($params->getScope()) {
|
||||
$moduleName = $this->metadata->getScopeModuleName($params->getScope());
|
||||
}
|
||||
|
||||
if ($moduleName) {
|
||||
$modulePath = $this->buildModulePath($path, $moduleName);
|
||||
|
||||
if ($this->fileManager->isFile($modulePath)) {
|
||||
return $modulePath;
|
||||
}
|
||||
}
|
||||
|
||||
if ($params->getModuleName()) {
|
||||
$modulePath = $this->buildModulePath($path, $params->getModuleName());
|
||||
|
||||
if ($this->fileManager->isFile($modulePath)) {
|
||||
return $modulePath;
|
||||
}
|
||||
}
|
||||
|
||||
$corePath = $this->pathProvider->getCore() . $path;
|
||||
|
||||
if ($this->fileManager->isFile($corePath)) {
|
||||
return $corePath;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private function buildModulePath(string $path, string $moduleName): string
|
||||
{
|
||||
return $this->pathProvider->getModule($moduleName) . $path;
|
||||
}
|
||||
}
|
||||
70
application/Espo/Core/Utils/Resource/FileReader/Params.php
Normal file
70
application/Espo/Core/Utils/Resource/FileReader/Params.php
Normal file
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://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 Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Core\Utils\Resource\FileReader;
|
||||
|
||||
/**
|
||||
* Immutable.
|
||||
*/
|
||||
class Params
|
||||
{
|
||||
private ?string $scope = null;
|
||||
private ?string $moduleName = null;
|
||||
|
||||
public static function create(): self
|
||||
{
|
||||
return new self();
|
||||
}
|
||||
|
||||
public function withScope(?string $scope): self
|
||||
{
|
||||
$obj = clone $this;
|
||||
$obj->scope = $scope;
|
||||
|
||||
return $obj;
|
||||
}
|
||||
|
||||
public function withModuleName(?string $moduleName): self
|
||||
{
|
||||
$obj = clone $this;
|
||||
$obj->moduleName = $moduleName;
|
||||
|
||||
return $obj;
|
||||
}
|
||||
|
||||
public function getScope(): ?string
|
||||
{
|
||||
return $this->scope;
|
||||
}
|
||||
|
||||
public function getModuleName(): ?string
|
||||
{
|
||||
return $this->moduleName;
|
||||
}
|
||||
}
|
||||
53
application/Espo/Core/Utils/Resource/PathProvider.php
Normal file
53
application/Espo/Core/Utils/Resource/PathProvider.php
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://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 Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Core\Utils\Resource;
|
||||
|
||||
use Espo\Core\Utils\Module\PathProvider as ModulePathProvider;
|
||||
|
||||
class PathProvider
|
||||
{
|
||||
public function __construct(private ModulePathProvider $provider)
|
||||
{}
|
||||
|
||||
public function getCore(): string
|
||||
{
|
||||
return $this->provider->getCore() . 'Resources/';
|
||||
}
|
||||
|
||||
public function getCustom(): string
|
||||
{
|
||||
return $this->provider->getCustom() . 'Resources/';
|
||||
}
|
||||
|
||||
public function getModule(string $moduleName): string
|
||||
{
|
||||
return $this->provider->getModule($moduleName) . 'Resources/';
|
||||
}
|
||||
}
|
||||
67
application/Espo/Core/Utils/Resource/Reader.php
Normal file
67
application/Espo/Core/Utils/Resource/Reader.php
Normal file
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://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 Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Core\Utils\Resource;
|
||||
|
||||
use Espo\Core\Utils\File\Unifier;
|
||||
use Espo\Core\Utils\File\UnifierObj;
|
||||
use Espo\Core\Utils\Resource\Reader\Params;
|
||||
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* Reads resource data. Reading is expensive. Read data is supposed to be cached after.
|
||||
*/
|
||||
class Reader
|
||||
{
|
||||
public function __construct(
|
||||
private Unifier $unifier,
|
||||
private UnifierObj $unifierObj
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Read resource data.
|
||||
*/
|
||||
public function read(string $path, Params $params): stdClass
|
||||
{
|
||||
/** @var stdClass */
|
||||
return $this->unifierObj->unify($path, $params->noCustom(), $params->getForceAppendPathList());
|
||||
}
|
||||
|
||||
/**
|
||||
* Read resource data as an associative array.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function readAsArray(string $path, Params $params): array
|
||||
{
|
||||
/** @var array<string, mixed> */
|
||||
return $this->unifier->unify($path, $params->noCustom(), $params->getForceAppendPathList());
|
||||
}
|
||||
}
|
||||
74
application/Espo/Core/Utils/Resource/Reader/Params.php
Normal file
74
application/Espo/Core/Utils/Resource/Reader/Params.php
Normal file
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://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 Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Core\Utils\Resource\Reader;
|
||||
|
||||
class Params
|
||||
{
|
||||
private bool $noCustom = false;
|
||||
/** @var array<int, string[]> */
|
||||
private array $forceAppendPathList = [];
|
||||
|
||||
public static function create(): self
|
||||
{
|
||||
return new self();
|
||||
}
|
||||
|
||||
public function withNoCustom(bool $noCustom = true): self
|
||||
{
|
||||
$obj = clone $this;
|
||||
$obj->noCustom = $noCustom;
|
||||
|
||||
return $obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, string[]> $forceAppendPathList
|
||||
*/
|
||||
public function withForceAppendPathList(array $forceAppendPathList): self
|
||||
{
|
||||
$obj = clone $this;
|
||||
$obj->forceAppendPathList = $forceAppendPathList;
|
||||
|
||||
return $obj;
|
||||
}
|
||||
|
||||
public function noCustom(): bool
|
||||
{
|
||||
return $this->noCustom;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, string[]>
|
||||
*/
|
||||
public function getForceAppendPathList(): array
|
||||
{
|
||||
return $this->forceAppendPathList;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user