orm metadata refactoring

This commit is contained in:
Yuri Kuznetsov
2020-12-21 13:01:07 +02:00
parent 19ccf09c20
commit df179212b1
12 changed files with 197 additions and 45 deletions

View File

@@ -48,6 +48,11 @@ class DefaultBinding
'entityManager'
);
$binder->bindService(
'Espo\\ORM\\Metadata',
'ormMetadata'
);
$binder->bindService(
'Espo\\Core\\ORM\\EntityManager',
'entityManager'

View File

@@ -36,7 +36,7 @@ use Espo\Core\{
Utils\Util,
Utils\Config,
Utils\File\Manager as FileManager,
Utils\Metadata\OrmMetadata,
Utils\Metadata\OrmMetadataData,
HookManager,
Utils\Database\Schema\SchemaProxy,
};
@@ -53,7 +53,7 @@ class DataManager
protected $entityManager;
protected $fileManager;
protected $metadata;
protected $ormMetadata;
protected $ormMetadataData;
protected $hookManager;
protected $schemaProxy;
@@ -64,7 +64,7 @@ class DataManager
Config $config,
FileManager $fileManager,
Metadata $metadata,
OrmMetadata $ormMetadata,
OrmMetadataData $ormMetadataData,
HookManager $hookManager,
SchemaProxy $schemaProxy
) {
@@ -72,7 +72,7 @@ class DataManager
$this->config = $config;
$this->fileManager = $fileManager;
$this->metadata = $metadata;
$this->ormMetadata = $ormMetadata;
$this->ormMetadataData = $ormMetadataData;
$this->hookManager = $hookManager;
$this->schemaProxy = $schemaProxy;
}
@@ -157,9 +157,9 @@ class DataManager
$metadata->init(true);
$ormData = $this->ormMetadata->getData(true);
$ormData = $this->ormMetadataData->getData(true);
$this->entityManager->setMetadata($ormData);
$this->entityManager->getMetadata()->updateData();
$this->updateCacheTimestamp();

View File

@@ -31,25 +31,32 @@ namespace Espo\Core\Loaders;
use Espo\Core\{
Utils\Config,
Utils\Metadata\OrmMetadata,
InjectableFactory,
ORM\EntityManager as EntityManagerService,
ORM\RepositoryFactory,
ORM\EntityFactory,
ORM\Helper,
ORM\MetadataDataProvider,
};
use Espo\{
ORM\Metadata as OrmMetadata,
};
class EntityManager implements Loader
{
protected $config;
protected $injectableFactory;
protected $ormMetadata;
protected $metadataDataProvider;
public function __construct(Config $config, InjectableFactory $injectableFactory, OrmMetadata $ormMetadata)
{
public function __construct(
Config $config,
InjectableFactory $injectableFactory,
MetadataDataProvider $metadataDataProvider
) {
$this->config = $config;
$this->injectableFactory = $injectableFactory;
$this->ormMetadata = $ormMetadata;
$this->metadataDataProvider = $metadataDataProvider;
}
public function load() : EntityManagerService
@@ -65,7 +72,6 @@ class EntityManager implements Loader
$config = $this->config;
$params = [
'metadata' => $this->ormMetadata->getData(),
'host' => $config->get('database.host'),
'port' => $config->get('database.port'),
'dbname' => $config->get('database.dbname'),
@@ -81,8 +87,11 @@ class EntityManager implements Loader
'sslCipher' => $config->get('database.sslCipher'),
];
$metadata = new OrmMetadata($this->metadataDataProvider);
$entityManager = $this->injectableFactory->createWith(EntityManagerService::class, [
'params' => $params,
'metadata' => $metadata,
'repositoryFactory' => $repositoryFactory,
'entityFactory' => $entityFactory,
'helper' => $helper,

View File

@@ -0,0 +1,50 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2020 Yuri Kuznetsov, Taras Machyshyn, Oleksiy 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\Loaders;
use Espo\{
ORM\Metadata,
ORM\EntityManager,
};
class OrmMetadata implements Loader
{
protected $entityManager;
public function __construct(EntityManager $entityManager)
{
$this->entityManager = $entityManager;
}
public function load() : Metadata
{
return $this->entityManager->getMetadata();
}
}

View File

@@ -32,12 +32,13 @@ namespace Espo\Core\ORM;
use Espo\Entities\User;
use Espo\Core\{
Utils\Metadata as EspoMetadata,
HookManager,
Utils\Util,
};
use Espo\ORM\EntityManager as BaseEntityManager;
use Espo\ORM\{
EntityManager as BaseEntityManager,
Metadata,
};
class EntityManager extends BaseEntityManager
{
@@ -45,11 +46,12 @@ class EntityManager extends BaseEntityManager
public function __construct(
array $params,
Metadata $metadata,
RepositoryFactory $repositoryFactory,
EntityFactory $entityFactory,
Helper $helper
) {
parent::__construct($params, $repositoryFactory, $entityFactory);
parent::__construct($params, $metadata, $repositoryFactory, $entityFactory);
$this->helper = $helper;
}

View File

@@ -0,0 +1,48 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2020 Yuri Kuznetsov, Taras Machyshyn, Oleksiy 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\ORM;
use Espo\{
Core\Utils\Metadata\OrmMetadataData,
ORM\MetadataDataProvider as MetadataDataProviderInterface,
};
class MetadataDataProvider implements MetadataDataProviderInterface
{
public function __construct(OrmMetadataData $ormMetadataData)
{
$this->ormMetadataData = $ormMetadataData;
}
public function get() : array
{
return $this->ormMetadataData->getData();
}
}

View File

@@ -41,7 +41,7 @@ use Espo\Core\{
Utils\File\Manager as FileManager,
ORM\EntityManager,
Utils\File\ClassParser,
Utils\Metadata\OrmMetadata,
Utils\Metadata\OrmMetadataData,
Utils\Util,
Utils\Database\Helper,
Utils\Database\DBAL\Schema\Comparator,
@@ -64,6 +64,8 @@ class Schema
private $databaseHelper;
protected $ormMetadataData;
protected $fieldTypePaths = [
'application/Espo/Core/Utils/Database/DBAL/FieldTypes',
'custom/Espo/Custom/Core/Utils/Database/DBAL/FieldTypes',
@@ -92,7 +94,7 @@ class Schema
FileManager $fileManager,
EntityManager $entityManager,
ClassParser $classParser,
OrmMetadata $ormMetadata
OrmMetadataData $ormMetadataData
) {
$this->config = $config;
$this->metadata = $metadata;
@@ -110,7 +112,7 @@ class Schema
$this->schemaConverter = new Converter($this->metadata, $this->fileManager, $this, $this->config);
$this->ormMetadata = $ormMetadata;
$this->ormMetadataData = $ormMetadataData;
}
protected function getConfig()
@@ -203,7 +205,7 @@ class Schema
$currentSchema = $this->getCurrentSchema();
$metadataSchema = $this->schemaConverter->process($this->ormMetadata->getData(), $entityList);
$metadataSchema = $this->schemaConverter->process($this->ormMetadataData->getData(), $entityList);
$this->initRebuildActions($currentSchema, $metadataSchema);

View File

@@ -39,7 +39,7 @@ use Espo\Core\{
Exceptions\Error,
};
class OrmMetadata
class OrmMetadataData
{
protected $data = null;

View File

@@ -82,11 +82,15 @@ class EntityManager
'mysqli' => 'Mysql',
];
public function __construct(array $params, RepositoryFactory $repositoryFactory, EntityFactory $entityFactory)
{
public function __construct(
array $params,
Metadata $metadata,
RepositoryFactory $repositoryFactory,
EntityFactory $entityFactory
) {
$this->params = $params;
$this->metadata = new Metadata();
$this->metadata = $metadata;
if (empty($this->params['platform'])) {
if (empty($this->params['driver'])) {
@@ -102,10 +106,6 @@ class EntityManager
$this->params['platform'] = $this->driverPlatformMap[$this->params['driver']];
}
if (!empty($params['metadata'])) {
$this->setMetadata($params['metadata']);
}
$this->entityFactory = $entityFactory;
$this->entityFactory->setEntityManager($this);
@@ -372,16 +372,7 @@ class EntityManager
return $this->queryBuilder;
}
/**
* @deprecated
* @todo Remove.
*/
public function setMetadata(array $data)
{
$this->metadata->setData($data);
}
public function getMetadata()
public function getMetadata() : Metadata
{
return $this->metadata;
}

View File

@@ -32,20 +32,27 @@ namespace Espo\ORM;
use InvalidArgumentException;
/**
* Metadata of all entities.
* Metadata.
*/
class Metadata
{
protected $data;
public function __construct(array $data = [])
protected $dataProvider;
public function __construct(MetadataDataProvider $dataProvider)
{
$this->data = $data;
$this->data = $dataProvider->get();
$this->dataProvider = $dataProvider;
}
public function setData(array $data)
/**
* Update data from the data provider.
*/
public function updateData()
{
$this->data = $data;
$this->data = $this->dataProvider->get();
}
/**

View File

@@ -0,0 +1,38 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2020 Yuri Kuznetsov, Taras Machyshyn, Oleksiy 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\ORM;
/**
* Provides data for metadata.
*/
interface MetadataDataProvider
{
public function get() : array;
}

View File

@@ -5,8 +5,8 @@
"schema": {
"className": "Espo\\Core\\Utils\\Database\\Schema\\Schema"
},
"ormMetadata": {
"className": "Espo\\Core\\Utils\\Metadata\\OrmMetadata"
"ormMetadataData": {
"className": "Espo\\Core\\Utils\\Metadata\\OrmMetadataData"
},
"classParser": {
"className": "Espo\\Core\\Utils\\File\\ClassParser"