metadata ref

This commit is contained in:
Yuri Kuznetsov
2023-11-05 10:08:23 +02:00
parent e2630bbf51
commit c635b23b56
5 changed files with 268 additions and 209 deletions
+10 -140
View File
@@ -30,9 +30,8 @@
namespace Espo\Core\Utils;
use Espo\Core\Utils\File\Manager as FileManager;
use Espo\Core\Utils\Metadata\Helper;
use Espo\Core\Utils\Resource\Reader as ResourceReader;
use Espo\Core\Utils\Resource\Reader\Params as ResourceReaderParams;
use Espo\Core\Utils\Metadata\Builder;
use Espo\Core\Utils\Metadata\BuilderHelper;
use stdClass;
use LogicException;
@@ -56,54 +55,14 @@ class Metadata
/** @var array<string, array<string, mixed>> */
private $changedData = [];
/** @var array<int, string[]> */
private $forceAppendPathList = [
['app', 'rebuild', 'actionClassNameList'],
['app', 'fieldProcessing', 'readLoaderClassNameList'],
['app', 'fieldProcessing', 'listLoaderClassNameList'],
['app', 'fieldProcessing', 'saverClassNameList'],
['app', 'hook', 'suppressClassNameList'],
['app', 'api', 'globalMiddlewareClassNameList'],
['app', 'api', 'routeMiddlewareClassNameListMap', self::ANY_KEY],
['app', 'api', 'controllerMiddlewareClassNameListMap', self::ANY_KEY],
['app', 'api', 'controllerActionMiddlewareClassNameListMap', self::ANY_KEY],
['app', 'entityManager', 'createHookClassNameList'],
['app', 'entityManager', 'deleteHookClassNameList'],
['app', 'entityManager', 'updateHookClassNameList'],
['app', 'linkManager', 'createHookClassNameList'],
['app', 'linkManager', 'deleteHookClassNameList'],
['recordDefs', self::ANY_KEY, 'readLoaderClassNameList'],
['recordDefs', self::ANY_KEY, 'listLoaderClassNameList'],
['recordDefs', self::ANY_KEY, 'saverClassNameList'],
['recordDefs', self::ANY_KEY, 'selectApplierClassNameList'],
['recordDefs', self::ANY_KEY, 'beforeReadHookClassNameList'],
['recordDefs', self::ANY_KEY, 'beforeCreateHookClassNameList'],
['recordDefs', self::ANY_KEY, 'beforeUpdateHookClassNameList'],
['recordDefs', self::ANY_KEY, 'beforeDeleteHookClassNameList'],
['recordDefs', self::ANY_KEY, 'beforeLinkHookClassNameList'],
['recordDefs', self::ANY_KEY, 'beforeUnlinkHookClassNameList'],
];
private const ANY_KEY = '__ANY__';
private Helper $metadataHelper;
public function __construct(
private FileManager $fileManager,
private DataCache $dataCache,
private ResourceReader $resourceReader,
private Module $module,
private Builder $builder,
private BuilderHelper $builderHelper,
private bool $useCache = false
){}
private function getMetadataHelper(): Helper
{
if (!isset($this->metadataHelper)) {
$this->metadataHelper = new Helper($this);
}
return $this->metadataHelper;
}
) {}
/**
* Init metadata.
@@ -162,28 +121,6 @@ class Metadata
return Util::getValueByKey($this->getData(), $key, $default);
}
/**
* Get all metadata.
*
* @/param bool $isJSON
* @/param bool $reload
* @/return array<string, mixed>|string
*/
/*public function getAll(bool $isJSON = false, bool $reload = false)
{
if ($reload) {
$this->init($reload);
}
assert($this->data !== null);
if ($isJSON) {
return Json::encode($this->data);
}
return $this->data;
}*/
private function objInit(bool $reload = false): void
{
if (!$this->useCache) {
@@ -199,12 +136,7 @@ class Metadata
return;
}
$readerParams = ResourceReaderParams::create()
->withForceAppendPathList($this->forceAppendPathList);
$this->objData = $this->resourceReader->read('metadata', $readerParams);
$this->objData = $this->addAdditionalFieldsObj($this->objData);
$this->objData = $this->builder->build();
if ($this->useCache) {
$this->dataCache->store($this->objCacheKey, $this->objData);
@@ -241,71 +173,7 @@ class Metadata
return $this->getObjData();
}
/**
* @param stdClass $data
* @return stdClass
*/
private function addAdditionalFieldsObj($data)
{
if (!isset($data->entityDefs)) {
return $data;
}
$fieldDefinitionList = Util::objectToArray($data->fields);
foreach (get_object_vars($data->entityDefs) as $entityType => $entityDefsItem) {
if (isset($data->entityDefs->$entityType->collection)) {
/** @var stdClass $collectionItem */
$collectionItem = $data->entityDefs->$entityType->collection;
if (isset($collectionItem->orderBy)) {
$collectionItem->sortBy = $collectionItem->orderBy;
}
else if (isset($collectionItem->sortBy)) {
$collectionItem->orderBy = $collectionItem->sortBy;
}
if (isset($collectionItem->order)) {
$collectionItem->asc = $collectionItem->order === 'asc';
}
else if (isset($collectionItem->asc)) {
$collectionItem->order = $collectionItem->asc === true ? 'asc' : 'desc';
}
}
if (!isset($entityDefsItem->fields)) {
continue;
}
foreach (get_object_vars($entityDefsItem->fields) as $field => $fieldDefsItem) {
$additionalFields = $this->getMetadataHelper()->getAdditionalFieldList(
$field,
Util::objectToArray($fieldDefsItem), $fieldDefinitionList
);
if (!$additionalFields) {
continue;
}
foreach ($additionalFields as $subFieldName => $subFieldParams) {
$item = Util::arrayToObject($subFieldParams);
if (isset($entityDefsItem->fields->$subFieldName)) {
$data->entityDefs->$entityType->fields->$subFieldName = DataUtil::merge(
$item,
$entityDefsItem->fields->$subFieldName
);
continue;
}
$data->entityDefs->$entityType->fields->$subFieldName = $item;
}
}
}
return $data;
}
/**
* Get metadata definition in custom directory.
@@ -401,7 +269,7 @@ class Metadata
switch ($key1) {
case 'entityDefs':
// unset related additional fields, e.g. a field with "address" type
// Unset related additional fields, e.g. a field with an 'address' type.
$fieldDefinitionList = $this->get('fields');
$unsetList = $unsets;
@@ -411,7 +279,8 @@ class Metadata
$fieldName = $matches[1];
$fieldPath = [$key1, $key2, 'fields', $fieldName];
$additionalFields = $this->getMetadataHelper()->getAdditionalFieldList(
// @todo Revise the need. Additional fields are supposed to exist only in the build?
$additionalFields = $this->builderHelper->getAdditionalFieldList(
$fieldName,
$this->get($fieldPath, []),
$fieldDefinitionList
@@ -449,6 +318,7 @@ class Metadata
$this->deletedData = $mergedDeletedData;
/** @var array<string, array<string, mixed>> $unsetDeletedData */
/** @noinspection PhpRedundantOptionalArgumentInspection */
$unsetDeletedData = Util::unsetInArrayByValue('__APPEND__', $this->deletedData, true);
$this->deletedData = $unsetDeletedData;
@@ -0,0 +1,148 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2023 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\Metadata;
use Espo\Core\Utils\DataUtil;
use Espo\Core\Utils\Resource\Reader as ResourceReader;
use Espo\Core\Utils\Resource\Reader\Params as ResourceReaderParams;
use Espo\Core\Utils\Util;
use stdClass;
class Builder
{
/** @var array<int, string[]> */
private $forceAppendPathList = [
['app', 'rebuild', 'actionClassNameList'],
['app', 'fieldProcessing', 'readLoaderClassNameList'],
['app', 'fieldProcessing', 'listLoaderClassNameList'],
['app', 'fieldProcessing', 'saverClassNameList'],
['app', 'hook', 'suppressClassNameList'],
['app', 'api', 'globalMiddlewareClassNameList'],
['app', 'api', 'routeMiddlewareClassNameListMap', self::ANY_KEY],
['app', 'api', 'controllerMiddlewareClassNameListMap', self::ANY_KEY],
['app', 'api', 'controllerActionMiddlewareClassNameListMap', self::ANY_KEY],
['app', 'entityManager', 'createHookClassNameList'],
['app', 'entityManager', 'deleteHookClassNameList'],
['app', 'entityManager', 'updateHookClassNameList'],
['app', 'linkManager', 'createHookClassNameList'],
['app', 'linkManager', 'deleteHookClassNameList'],
['recordDefs', self::ANY_KEY, 'readLoaderClassNameList'],
['recordDefs', self::ANY_KEY, 'listLoaderClassNameList'],
['recordDefs', self::ANY_KEY, 'saverClassNameList'],
['recordDefs', self::ANY_KEY, 'selectApplierClassNameList'],
['recordDefs', self::ANY_KEY, 'beforeReadHookClassNameList'],
['recordDefs', self::ANY_KEY, 'beforeCreateHookClassNameList'],
['recordDefs', self::ANY_KEY, 'beforeUpdateHookClassNameList'],
['recordDefs', self::ANY_KEY, 'beforeDeleteHookClassNameList'],
['recordDefs', self::ANY_KEY, 'beforeLinkHookClassNameList'],
['recordDefs', self::ANY_KEY, 'beforeUnlinkHookClassNameList'],
];
private const ANY_KEY = '__ANY__';
public function __construct(
private ResourceReader $resourceReader,
private BuilderHelper $builderHelper
) {}
public function build(): stdClass
{
$readerParams = ResourceReaderParams::create()
->withForceAppendPathList($this->forceAppendPathList);
$data = $this->resourceReader->read('metadata', $readerParams);
$this->addAdditionalField($data);
return $data;
}
private function addAdditionalField(stdClass $data): void
{
if (!isset($data->entityDefs)) {
return;
}
$fieldDefinitionList = Util::objectToArray($data->fields);
foreach (get_object_vars($data->entityDefs) as $entityType => $entityDefsItem) {
if (isset($data->entityDefs->$entityType->collection)) {
/** @var stdClass $collectionItem */
$collectionItem = $data->entityDefs->$entityType->collection;
if (isset($collectionItem->orderBy)) {
$collectionItem->sortBy = $collectionItem->orderBy;
}
else if (isset($collectionItem->sortBy)) {
$collectionItem->orderBy = $collectionItem->sortBy;
}
if (isset($collectionItem->order)) {
$collectionItem->asc = $collectionItem->order === 'asc';
}
else if (isset($collectionItem->asc)) {
$collectionItem->order = $collectionItem->asc === true ? 'asc' : 'desc';
}
}
if (!isset($entityDefsItem->fields)) {
continue;
}
foreach (get_object_vars($entityDefsItem->fields) as $field => $fieldDefsItem) {
$additionalFields = $this->builderHelper->getAdditionalFieldList(
$field,
Util::objectToArray($fieldDefsItem),
$fieldDefinitionList
);
if (!$additionalFields) {
continue;
}
foreach ($additionalFields as $subFieldName => $subFieldParams) {
$item = Util::arrayToObject($subFieldParams);
if (isset($entityDefsItem->fields->$subFieldName)) {
$data->entityDefs->$entityType->fields->$subFieldName =
DataUtil::merge(
$item,
$entityDefsItem->fields->$subFieldName
);
continue;
}
$data->entityDefs->$entityType->fields->$subFieldName = $item;
}
}
}
}
}
@@ -0,0 +1,97 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2023 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\Metadata;
use Espo\Core\Utils\Util;
class BuilderHelper
{
/**
* A list of copy-from-parent params for metadata -> fields.
*
* @var string[]
*/
private array $copiedDefParams = [
'readOnly',
'disabled',
'notStorable',
'layoutListDisabled',
'layoutDetailDisabled',
'layoutMassUpdateDisabled',
'layoutFiltersDisabled',
'directAccessDisabled',
'directUpdateDisabled',
'customizationDisabled',
'importDisabled',
'exportDisabled',
];
private string $defaultFieldNaming = 'postfix';
/**
* Get additional field list based on field definition in metadata 'fields'.
*
* @param string $fieldName
* @param array<string, mixed> $fieldParams
* @param array<string, mixed> $definitionList
* @return ?array<string, mixed>
*/
public function getAdditionalFieldList(string $fieldName, array $fieldParams, array $definitionList): ?array
{
if (empty($fieldParams['type']) || empty($definitionList)) {
return null;
}
$fieldType = $fieldParams['type'];
$fieldDefinition = $definitionList[$fieldType] ?? null;
if (
isset($fieldDefinition) &&
!empty($fieldDefinition['fields']) &&
is_array($fieldDefinition['fields'])
) {
$copiedParams = array_intersect_key($fieldParams, array_flip($this->copiedDefParams));
$additionalFields = [];
foreach ($fieldDefinition['fields'] as $subFieldName => $subFieldParams) {
$namingType = $fieldDefinition['naming'] ?? $this->defaultFieldNaming;
$subFieldNaming = Util::getNaming($fieldName, $subFieldName, $namingType);
$additionalFields[$subFieldNaming] = array_merge($copiedParams, $subFieldParams);
}
return $additionalFields;
}
return null;
}
}
@@ -29,36 +29,10 @@
namespace Espo\Core\Utils\Metadata;
use Espo\Core\Utils\Util;
use Espo\Core\Utils\Metadata;
/**
* Warning: Instantiated explicitly.
*/
class Helper
{
protected string $defaultNaming = 'postfix';
/**
* List of copied params for metadata -> 'fields' from parent items.
*
* @var string[]
*/
private array $copiedDefParams = [
'readOnly',
'disabled',
'notStorable',
'layoutListDisabled',
'layoutDetailDisabled',
'layoutMassUpdateDisabled',
'layoutFiltersDisabled',
'directAccessDisabled',
'directUpdateDisabled',
'customizationDisabled',
'importDisabled',
'exportDisabled',
];
public function __construct(private Metadata $metadata)
{}
@@ -94,7 +68,7 @@ class Helper
/**
* Get link definition defined in 'fields' metadata.
* In linkDefs can be used as value (e.g. "type": "hasChildren") and/or variables (e.g. "entityName":"{entity}").
* In linkDefs can be used as value (e.g. "type": "hasChildren") and/or variables (e.g. "entityName": "{entity}").
* Variables should be defined into fieldDefs (in 'entityDefs' metadata).
*
* @param string $entityType
@@ -128,45 +102,4 @@ class Helper
return $linkFieldDefsByType;
}
/**
* Get additional field list based on field definition in metadata 'fields'.
*
* @param string $fieldName
* @param array<string, mixed> $fieldParams
* @param array<string, mixed> $definitionList
* @return ?array<string, mixed>
*/
public function getAdditionalFieldList($fieldName, array $fieldParams, array $definitionList)
{
if (empty($fieldParams['type']) || empty($definitionList)) {
return null;
}
$fieldType = $fieldParams['type'];
$fieldDefinition = $definitionList[$fieldType] ?? null;
if (
isset($fieldDefinition) &&
!empty($fieldDefinition['fields']) &&
is_array($fieldDefinition['fields'])
) {
$copiedParams = array_intersect_key($fieldParams, array_flip($this->copiedDefParams));
$additionalFields = [];
// add additional fields
foreach ($fieldDefinition['fields'] as $subFieldName => $subFieldParams) {
$namingType = $fieldDefinition['naming'] ?? $this->defaultNaming;
$subFieldNaming = Util::getNaming($fieldName, $subFieldName, $namingType);
$additionalFields[$subFieldNaming] = array_merge($copiedParams, $subFieldParams);
}
return $additionalFields;
}
return null;
}
}
+12 -1
View File
@@ -67,7 +67,18 @@ class MetadataTest extends \PHPUnit\Framework\TestCase
$reader = new Reader($unifier, $unifierObj);
$this->object = new Metadata($this->fileManager, $this->dataCache, $reader, $module, true);
$builderHelper = new Metadata\BuilderHelper();
$builder = new Metadata\Builder($reader, $builderHelper);
$this->object = new Metadata(
$this->fileManager,
$this->dataCache,
$module,
$builder,
$builderHelper,
true
);
$this->reflection = new ReflectionHelper($this->object);