layout custom module location

This commit is contained in:
Yuri Kuznetsov
2023-11-29 10:26:12 +02:00
parent 7c6b3d20ce
commit 26fbdabd94
6 changed files with 82 additions and 21 deletions

19
.idea/jsonSchemas.xml generated
View File

@@ -797,6 +797,25 @@
</SchemaInfo>
</value>
</entry>
<entry key="metadata/app/layouts">
<value>
<SchemaInfo>
<option name="generatedName" value="New Schema" />
<option name="name" value="metadata/app/layouts" />
<option name="relativePathToSchema" value="schema/metadata/app/layouts.json" />
<option name="schemaVersion" value="JSON Schema version 7" />
<option name="patterns">
<list>
<Item>
<option name="pattern" value="true" />
<option name="path" value="*/Resources/metadata/app/layouts.json" />
<option name="mappingKind" value="Pattern" />
</Item>
</list>
</option>
</SchemaInfo>
</value>
</entry>
<entry key="metadata/app/linkManager">
<value>
<SchemaInfo>

View File

@@ -334,6 +334,12 @@
],
"url": "./schema/metadata/app/language.json"
},
{
"fileMatch": [
"*/Resources/metadata/app/layouts.json"
],
"url": "./schema/metadata/app/layouts.json"
},
{
"fileMatch": [
"*/Resources/metadata/app/linkManager.json"

View File

@@ -0,0 +1 @@
{}

View File

@@ -32,6 +32,7 @@ namespace Espo\Tools\Layout;
use Espo\Core\InjectableFactory;
use Espo\Core\Utils\File\Manager as FileManager;
use Espo\Core\Utils\Json;
use Espo\Core\Utils\Metadata;
use Espo\Core\Utils\Resource\FileReader;
use Espo\Core\Utils\Resource\FileReader\Params as FileReaderParams;
use RuntimeException;
@@ -46,6 +47,7 @@ class LayoutProvider
public function __construct(
private FileManager $fileManager,
private InjectableFactory $injectableFactory,
private Metadata $metadata,
FileReader $fileReader
) {
$this->fileReader = $fileReader;
@@ -62,8 +64,15 @@ class LayoutProvider
$path = 'layouts/' . $scope . '/' . $name . '.json';
$params = FileReaderParams::create()
->withScope($scope);
$params = FileReaderParams::create()->withScope($scope);
$module = $this->getLayoutLocationModule($scope, $name);
if ($module) {
$params = $params
->withScope(null)
->withModuleName($module);
}
if ($this->fileReader->exists($path, $params)) {
return $this->fileReader->read($path, $params);
@@ -72,6 +81,11 @@ class LayoutProvider
return $this->getDefault($scope, $name);
}
private function getLayoutLocationModule(string $scope, string $name): ?string
{
return $this->metadata->get("app.layouts.{$scope}.{$name}.module");
}
private function getDefault(string $scope, string $name): ?string
{
$defaultImplClassName = 'Espo\\Custom\\Classes\\DefaultLayouts\\' . ucfirst($name) . 'Type';

View File

@@ -0,0 +1,26 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://www.espocrm.com/schema/metadata/app/layouts.json",
"title": "app/layouts",
"description": "Layouts.",
"type": "object",
"propertyNames": {
"anyOf": [
{"type": "string"}
]
},
"additionalProperties": {
"type": "object",
"description": "A scope name.",
"additionalProperties": {
"type": "object",
"description": "A layout name.",
"properties": {
"module": {
"type": "string",
"description": "A module in which the layout is located."
}
}
}
}
}

View File

@@ -29,43 +29,38 @@
namespace tests\unit\Espo\Core\Utils;
use Espo\Core\Utils\Metadata;
use Espo\Tools\Layout\LayoutProvider;
use Espo\Core\Utils\File\Manager as FileManager;
use Espo\Core\InjectableFactory;
use Espo\Core\{
Utils\Resource\FileReader,
Utils\Resource\FileReader\Params as FileReaderParams,
};
use Espo\Core\Utils\Resource\FileReader;
use Espo\Core\Utils\Resource\FileReader\Params as FileReaderParams;
class LayoutTest extends \PHPUnit\Framework\TestCase
{
/**
* @var LayoutProvider
*/
/** @var LayoutProvider */
private $layout;
/**
* @var InjectableFactory
*/
/** @var InjectableFactory */
private $injectableFactory;
/**
* @var FileManager
*/
/** @var FileManager */
private $fileManager;
private $fileReader;
protected function setUp(): void
{
$this->fileManager = $this->createMock(FileManager::class);
$this->injectableFactory = $this->createMock(InjectableFactory::class);
$this->fileReader = $this->createMock(FileReader::class);
$this->layout = new \Espo\Tools\Layout\LayoutProvider($this->fileManager, $this->injectableFactory, $this->fileReader);
$metadata = $this->createMock(Metadata::class);
$this->layout = new LayoutProvider(
$this->fileManager,
$this->injectableFactory,
$metadata,
$this->fileReader
);
}
public function testGet1(): void