orm refactoring

This commit is contained in:
Yuri Kuznetsov
2021-06-12 14:50:13 +03:00
parent f8ecbb7806
commit 3baf1f17bf
4 changed files with 379 additions and 127 deletions

View File

@@ -37,6 +37,7 @@ use Espo\Core\{
use Espo\{
ORM\Metadata,
ORM\EventDispatcher,
ORM\DatabaseParams,
};
class EntityManagerFactory
@@ -71,21 +72,20 @@ class EntityManagerFactory
$config = $this->config;
$params = [
'host' => $config->get('database.host'),
'port' => $config->get('database.port'),
'dbname' => $config->get('database.dbname'),
'user' => $config->get('database.user'),
'charset' => $config->get('database.charset', 'utf8'),
'password' => $config->get('database.password'),
'driver' => $config->get('database.driver'),
'platform' => $config->get('database.platform'),
'sslCA' => $config->get('database.sslCA'),
'sslCert' => $config->get('database.sslCert'),
'sslKey' => $config->get('database.sslKey'),
'sslCAPath' => $config->get('database.sslCAPath'),
'sslCipher' => $config->get('database.sslCipher'),
];
$databaseParams = DatabaseParams::create()
->withHost($config->get('database.host'))
->withPort($config->get('database.port') ? (int) $config->get('database.port') : null)
->withName($config->get('database.dbname'))
->withUsername($config->get('database.user'))
->withPassword($config->get('database.password'))
->withCharset($config->get('database.charset') ?? 'utf8')
->withDriver($config->get('database.driver'))
->withPlatform($config->get('database.platform'))
->withSslCa($config->get('database.sslCA'))
->withSslCert($config->get('database.sslCert'))
->withSslKey($config->get('database.sslKey'))
->withSslCaPath($config->get('database.sslCAPath'))
->withSslCipher($config->get('database.sslCipher'));
$metadata = new Metadata($this->metadataDataProvider, $this->eventDispatcher);
@@ -106,7 +106,7 @@ class EntityManagerFactory
$entityManager = $this->injectableFactory->createWith(
EntityManager::class,
[
'params' => $params,
'databaseParams' => $databaseParams,
'metadata' => $metadata,
'repositoryFactory' => $repositoryFactory,
'entityFactory' => $entityFactory,

View File

@@ -0,0 +1,233 @@
<?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\ORM;
class DatabaseParams
{
private $platform = null;
private $host = null;
private $port = null;
private $name = null;
private $username = null;
private $password = null;
private $charset = null;
private $driver = null;
private $sslCa = null;
private $sslCert = null;
private $sslKey = null;
private $sslCaPath = null;
private $sslCipher = null;
public static function create(): self
{
return new self();
}
public function getPlatform(): ?string
{
return $this->platform;
}
public function getHost(): ?string
{
return $this->host;
}
public function getPort(): ?int
{
return $this->port;
}
public function getName(): ?string
{
return $this->name;
}
public function getUsername(): ?string
{
return $this->username;
}
public function getPassword(): ?string
{
return $this->password;
}
public function getCharset(): ?string
{
return $this->charset;
}
public function getDriver(): ?string
{
return $this->driver;
}
public function getSslCa(): ?string
{
return $this->sslCa;
}
public function getSslCert(): ?string
{
return $this->sslCert;
}
public function getSslCaPath(): ?string
{
return $this->sslCaPath;
}
public function getSslCipher(): ?string
{
return $this->sslCipher;
}
public function getSslKey(): ?string
{
return $this->sslKey;
}
public function withPlatform(?string $platform): self
{
$obj = clone $this;
$obj->platform = $platform;
return $obj;
}
public function withHost(?string $host): self
{
$obj = clone $this;
$obj->host = $host;
return $obj;
}
public function withPort(?int $port): self
{
$obj = clone $this;
$obj->port = $port;
return $obj;
}
public function withName(?string $name): self
{
$obj = clone $this;
$obj->name = $name;
return $obj;
}
public function withUsername(?string $username): self
{
$obj = clone $this;
$obj->username = $username;
return $obj;
}
public function withPassword(?string $password): self
{
$obj = clone $this;
$obj->password = $password;
return $obj;
}
public function withCharset(?string $charset): self
{
$obj = clone $this;
$obj->charset = $charset;
return $obj;
}
public function withDriver(?string $driver): self
{
$obj = clone $this;
$obj->driver = $driver;
return $obj;
}
public function withSslCa(?string $sslCa): self
{
$obj = clone $this;
$obj->sslCa = $sslCa;
return $obj;
}
public function withSslCaPath(?string $sslCaPath): self
{
$obj = clone $this;
$obj->sslCaPath = $sslCaPath;
return $obj;
}
public function withSslCert(?string $sslCert): self
{
$obj = clone $this;
$obj->sslCert = $sslCert;
return $obj;
}
public function withSslCipher(?string $sslCipher): self
{
$obj = clone $this;
$obj->sslCipher = $sslCipher;
return $obj;
}
public function withSslKey(?string $sslKey): self
{
$obj = clone $this;
$obj->sslKey = $sslKey;
return $obj;
}
}

View File

@@ -32,7 +32,7 @@ namespace Espo\ORM;
use Espo\ORM\{
QueryComposer\QueryComposer,
Mapper\Mapper,
Mapper\BaseMapper,
Mapper\MapperFactory,
Repository\RepositoryFactory,
Repository\Repository,
Repository\RDBRepository,
@@ -63,13 +63,18 @@ class EntityManager
protected $eventDispatcher;
private $mapperFactory = null;
private $mappers = [];
private $metadata;
private $repositoryHash = [];
private $params = [];
/**
* @var DatabaseParams
*/
private $databaseParams;
private $queryComposer;
@@ -81,7 +86,7 @@ class EntityManager
private $locker;
protected $defaultMapperName = 'RDB';
private const RDB_MAPPER_NAME = 'RDB';
protected $driverPlatformMap = [
'pdo_mysql' => 'Mysql',
@@ -89,57 +94,53 @@ class EntityManager
];
public function __construct(
array $params,
DatabaseParams $databaseParams,
Metadata $metadata,
RepositoryFactory $repositoryFactory,
EntityFactory $entityFactory,
ValueFactoryFactory $valueFactoryFactory,
AttributeExtractorFactory $attributeExtractorFactory,
EventDispatcher $eventDispatcher
EventDispatcher $eventDispatcher,
?MapperFactory $mapperFactory = null
) {
$this->params = $params;
$this->databaseParams = $databaseParams;
$this->metadata = $metadata;
$this->eventDispatcher = $eventDispatcher;
$this->entityFactory = $entityFactory;
$this->repositoryFactory = $repositoryFactory;
$this->mapperFactory = $mapperFactory;
if (empty($this->params['platform'])) {
if (empty($this->params['driver'])) {
if (!$this->databaseParams->getPlatform()) {
$driver = $this->databaseParams->getDriver();
if (!$driver) {
throw new Exception('No database driver specified.');
}
$driver = $this->params['driver'];
$platform = $this->driverPlatformMap[$driver] ?? null;
if (empty($this->driverPlatformMap[$driver])) {
if (!$platform) {
throw new Exception("Database driver '{$driver}' is not supported.");
}
$this->params['platform'] = $this->driverPlatformMap[$this->params['driver']];
$this->databaseParams = $this->databaseParams->withPlatform($platform);
}
$this->eventDispatcher = $eventDispatcher;
$valueAccessorFactory = new ValueAccessorFactory(
$valueFactoryFactory,
$attributeExtractorFactory,
$eventDispatcher
);
$this->entityFactory = $entityFactory;
$this->entityFactory->setEntityManager($this);
$this->entityFactory->setValueAccessorFactory($valueAccessorFactory);
$this->repositoryFactory = $repositoryFactory;
$this->initQueryComposer();
$this->sqlExecutor = new SqlExecutor($this->getPDO());
$this->queryExecutor = new QueryExecutor($this->sqlExecutor, $this->queryComposer);
$this->queryBuilder = new QueryBuilder();
$this->collectionFactory = new CollectionFactory($this);
$this->transactionManager = new TransactionManager($this->getPDO(), $this->queryComposer);
$this->initLocker();
@@ -147,16 +148,12 @@ class EntityManager
private function initQueryComposer(): void
{
$className = $this->params['queryComposerClassName'] ?? null;
$platform = $this->databaseParams->getPlatform();
if (!$className) {
$platform = $this->params['platform'];
$className = 'Espo\\ORM\\QueryComposer\\' . ucfirst($platform) . 'QueryComposer';
$className = 'Espo\\ORM\\QueryComposer\\' . ucfirst($platform) . 'QueryComposer';
}
if (!$className || !class_exists($className)) {
throw new RuntimeException("Query composer could not be created.");
if (!class_exists($className)) {
throw new RuntimeException("Query composer for '{$platform}' platform does not exits.");
}
$this->queryComposer = new $className($this->getPDO(), $this->entityFactory, $this->metadata);
@@ -164,20 +161,12 @@ class EntityManager
private function initLocker(): void
{
$className = $this->params['lockerClassName'] ?? null;
$platform = $this->databaseParams->getPlatform();
if (!$className) {
$platform = $this->params['platform'];
$className = 'Espo\\ORM\\Locker\\' . ucfirst($platform) . 'Locker';
$className = 'Espo\\ORM\\Locker\\' . ucfirst($platform) . 'Locker';
if (!class_exists($className)) {
$className = BaseLocker::class;
}
}
if (!$className || !class_exists($className)) {
throw new RuntimeException("Locker could not be created.");
if (!class_exists($className)) {
$className = BaseLocker::class;
}
$this->locker = new $className($this->getPDO(), $this->queryComposer, $this->transactionManager);
@@ -207,46 +196,24 @@ class EntityManager
return $this->locker;
}
protected function getMapperClassName(string $name): string
{
$classNameMap = $this->params['mapperClassNameMap'] ?? [];
$className = $classNameMap[$name] ?? null;
if (!$className && $name === 'RDB') {
$className = $this->getRDBMapperClassName();
}
if (!$className || !class_exists($className)) {
throw new RuntimeException("Mapper '{$name}' does not exist.");
}
return $className;
}
private function getRDBMapperClassName(): string
{
$platform = $this->params['platform'];
$className = 'Espo\\ORM\\Mapper\\' . ucfirst($platform) . 'Mapper';
if (!class_exists($className)) {
$className = BaseMapper::class;
}
return $className;
}
/**
* Get a Mapper.
*/
public function getMapper(?string $name = null): Mapper
public function getMapper(string $name = self::RDB_MAPPER_NAME): Mapper
{
$className = $this->getMapperClassName($name ?? $this->defaultMapperName);
if (!array_key_exists($name, $this->mappers)) {
$this->loadMapper($name);
}
if (empty($this->mappers[$className])) {
$this->mappers[$className] = new $className(
return $this->mappers[$name];
}
private function loadMapper(string $name): void
{
if ($name === self::RDB_MAPPER_NAME) {
$className = $this->getRDBMapperClassName();
$this->mappers[$name] = new $className(
$this->getPDO(),
$this->entityFactory,
$this->collectionFactory,
@@ -254,45 +221,40 @@ class EntityManager
$this->metadata,
$this->sqlExecutor
);
return;
}
return $this->mappers[$className];
if (!$this->mapperFactory) {
throw new RuntimeException("Could not create mapper '{$name}'. No mapper factory.");
}
$this->mappers[$name] = $this->mapperFactory->create($name);
}
private function getRDBMapperClassName(): string
{
$platform = $this->databaseParams->getPlatform();
$className = 'Espo\\ORM\\Mapper\\' . ucfirst($platform) . 'Mapper';
if (!class_exists($className)) {
throw new RuntimeException("Mapper for '{$platform}' does not exist.");
}
return $className;
}
private function initPDO(): void
{
$params = $this->params;
$platform = strtolower($this->databaseParams->getPlatform() ?? '');
$options = [];
if (isset($params['sslCA'])) {
$options[PDO::MYSQL_ATTR_SSL_CA] = $params['sslCA'];
}
if (isset($params['sslCert'])) {
$options[PDO::MYSQL_ATTR_SSL_CERT] = $params['sslCert'];
}
if (isset($params['sslKey'])) {
$options[PDO::MYSQL_ATTR_SSL_KEY] = $params['sslKey'];
}
if (isset($params['sslCAPath'])) {
$options[PDO::MYSQL_ATTR_SSL_CAPATH] = $params['sslCAPath'];
}
if (isset($params['sslCipher'])) {
$options[PDO::MYSQL_ATTR_SSL_CIPHER] = $params['sslCipher'];
}
$platform = strtolower($params['platform'] ?? '');
$host = $params['host'] ?? null;
$port = $params['port'] ?? null;
$dbname = $params['dbname'] ?? null;
$charset = $params['charset'] ?? null;
$user = $params['user'] ?? null;
$password = $params['password'] ?? null;
$host = $this->databaseParams->getHost();
$port = $this->databaseParams->getPort();
$dbname = $this->databaseParams->getName();
$charset = $this->databaseParams->getCharset();
$username = $this->databaseParams->getUsername();
$password = $this->databaseParams->getPassword();
if (!$platform) {
throw new RuntimeException("No 'platform' parameter.");
@@ -311,7 +273,7 @@ class EntityManager
'host=' . $host;
if ($port) {
$dsn .= ';' . 'port=' . $port;
$dsn .= ';' . 'port=' . (string) $port;
}
if ($dbname) {
@@ -322,7 +284,29 @@ class EntityManager
$dsn .= ';' . 'charset=' . $charset;
}
$this->pdo = new PDO($dsn, $user, $password, $options);
$options = [];
if ($this->databaseParams->getSslCa()) {
$options[PDO::MYSQL_ATTR_SSL_CA] = $this->databaseParams->getSslCa();
}
if ($this->databaseParams->getSslCert()) {
$options[PDO::MYSQL_ATTR_SSL_CERT] = $this->databaseParams->getSslCert();
}
if ($this->databaseParams->getSslKey()) {
$options[PDO::MYSQL_ATTR_SSL_KEY] = $this->databaseParams->getSslKey();
}
if ($this->databaseParams->getSslCaPath()) {
$options[PDO::MYSQL_ATTR_SSL_CAPATH] = $this->databaseParams->getSslCaPath();
}
if ($this->databaseParams->getSslCipher()) {
$options[PDO::MYSQL_ATTR_SSL_CIPHER] = $this->databaseParams->getSslCipher();
}
$this->pdo = new PDO($dsn, $username, $password, $options);
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}

View File

@@ -0,0 +1,35 @@
<?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\ORM\Mapper;
interface MapperFactory
{
public function create(string $name): Mapper;
}