orm refactoring using injectable factory for entities

This commit is contained in:
Yuri Kuznetsov
2021-04-22 12:33:45 +03:00
parent b2b968f915
commit 7420f2ff98
8 changed files with 68 additions and 54 deletions
+23 -3
View File
@@ -31,18 +31,38 @@ namespace Espo\Core\Entities;
use Espo\Core\{
ORM\Entity,
ORM\Helper,
Fields\EmailAddressGroup,
Fields\PhoneNumberGroup,
Fields\Address,
};
use Espo\ORM\{
EntityManager,
Value\ValueAccessorFactory,
};
class Person extends Entity
{
private $helper;
public function __construct(
string $entityType,
array $defs,
EntityManager $entityManager,
ValueAccessorFactory $valueAccessorFactory,
Helper $helper
) {
parent::__construct($entityType, $defs, $entityManager, $valueAccessorFactory);
$this->helper = $helper;
}
protected function _setLastName($value)
{
$this->setInContainer('lastName', $value);
$name = $this->getEntityManager()->getHelper()->formatPersonName($this, 'name');
$name = $this->helper->formatPersonName($this, 'name');
$this->setInContainer('name', $name);
}
@@ -51,7 +71,7 @@ class Person extends Entity
{
$this->setInContainer('firstName', $value);
$name = $this->getEntityManager()->getHelper()->formatPersonName($this, 'name');
$name = $this->helper->formatPersonName($this, 'name');
$this->setInContainer('name', $name);
}
@@ -60,7 +80,7 @@ class Person extends Entity
{
$this->setInContainer('middleName', $value);
$name = $this->getEntityManager()->getHelper()->formatPersonName($this, 'name');
$name = $this->helper->formatPersonName($this, 'name');
$this->setInContainer('name', $name);
}
+37 -8
View File
@@ -33,7 +33,11 @@ use Espo\Core\Exceptions\Error;
use Espo\Core\{
Utils\ClassFinder,
InjectableFactory,
ORM\Entity as BaseEntity,
Binding\BindingContainer,
Binding\BindingData,
Binding\Binder,
};
use Espo\ORM\{
@@ -47,18 +51,24 @@ class EntityFactory implements EntityFactoryInterface
{
private $classFinder;
private $helper;
private $injectableFactory;
private $entityManager = null;
private $valueAccessorFactory = null;
public function __construct(ClassFinder $classFinder)
public function __construct(ClassFinder $classFinder, Helper $helper, InjectableFactory $injectableFactory)
{
$this->classFinder = $classFinder;
$this->helper = $helper;
$this->injectableFactory = $injectableFactory;
}
private function getClassName(string $name): ?string
private function getClassName(string $entityType): ?string
{
return $this->classFinder->find('Entities', $name);
return $this->classFinder->find('Entities', $entityType);
}
public function setEntityManager(EntityManager $entityManager): void
@@ -79,20 +89,39 @@ class EntityFactory implements EntityFactoryInterface
$this->valueAccessorFactory = $valueAccessorFactory;
}
public function create(string $name): Entity
public function create(string $entityType): Entity
{
$className = $this->getClassName($name);
$className = $this->getClassName($entityType);
if (!class_exists($className)) {
$className = BaseEntity::class;
}
$defs = $this->entityManager->getMetadata()->get($name);
$defs = $this->entityManager->getMetadata()->get($entityType);
if (is_null($defs)) {
throw new Error("Entity '{$name}' is not defined in metadata.");
throw new Error("Entity '{$entityType}' is not defined in metadata.");
}
return new $className($name, $defs, $this->entityManager, $this->valueAccessorFactory);
$bindingContainer = $this->getBindingContainer($className, $entityType, $defs);
return $this->injectableFactory->createWithBinding($className, $bindingContainer);
}
private function getBindingContainer(string $className, string $entityType, array $defs): BindingContainer
{
$data = new BindingData();
$binder = new Binder($data);
$binder
->for($className)
->bindValue('$entityType', $entityType)
->bindValue('$defs', $defs)
->bindInstance(EntityManager::class, $this->entityManager)
->bindInstance(ValueAccessorFactory::class, $this->valueAccessorFactory)
->bindInstance(Helper::class, $this->helper);
return new BindingContainer($data);
}
}
@@ -31,41 +31,9 @@ namespace Espo\Core\ORM;
use Espo\ORM\{
EntityManager as BaseEntityManager,
Metadata,
Value\ValueFactoryFactory,
Value\AttributeExtractorFactory,
EventDispatcher,
};
class EntityManager extends BaseEntityManager
{
private $helper;
public function __construct(
array $params,
Metadata $metadata,
RepositoryFactory $repositoryFactory,
EntityFactory $entityFactory,
ValueFactoryFactory $valueFactoryFactory,
AttributeExtractorFactory $attributeExtractorFactory,
EventDispatcher $eventDispatcher,
Helper $helper
) {
parent::__construct(
$params,
$metadata,
$repositoryFactory,
$entityFactory,
$valueFactoryFactory,
$attributeExtractorFactory,
$eventDispatcher
);
$this->helper = $helper;
}
public function getHelper(): Helper
{
return $this->helper;
}
}
@@ -69,8 +69,6 @@ class EntityManagerFactory
'entityFactory' => $entityFactory,
]);
$helper = $this->injectableFactory->create(Helper::class);
$config = $this->config;
$params = [
@@ -115,7 +113,6 @@ class EntityManagerFactory
'valueFactoryFactory' => $valueFactoryFactory,
'attributeExtractorFactory' => $attributeExtractorFactory,
'eventDispatcher' => $this->eventDispatcher,
'helper' => $helper,
]
);
+1 -1
View File
@@ -35,7 +35,7 @@ use Espo\ORM\Entity;
class Helper
{
protected $config;
private $config;
public function __construct(Config $config)
{
@@ -61,14 +61,14 @@ class RepositoryFactory implements RepositoryFactoryInterface
$this->classFinder = $classFinder;
}
protected function getClassName(string $name): ?string
protected function getClassName(string $entityType): ?string
{
return $this->classFinder->find('Repositories', $name);
return $this->classFinder->find('Repositories', $entityType);
}
public function create(string $name): Repository
public function create(string $entityType): Repository
{
$className = $this->getClassName($name);
$className = $this->getClassName($entityType);
if (!$className || !class_exists($className)) {
$className = $this->defaultClassName;
@@ -76,7 +76,7 @@ class RepositoryFactory implements RepositoryFactoryInterface
return $this->injectableFactory->createWith($className, [
'entityFactory' => $this->entityFactory,
'entityType' => $name,
'entityType' => $entityType,
]);
}
}
+1 -1
View File
@@ -36,7 +36,7 @@ interface EntityFactory
/**
* Create an entity.
*/
public function create(string $name): Entity;
public function create(string $entityType): Entity;
/**
* For internal use.
@@ -31,5 +31,5 @@ namespace Espo\ORM\Repository;
interface RepositoryFactory
{
public function create(string $name): Repository;
public function create(string $entityType): Repository;
}