From a21671f1b7b4c4087238c38512cdb4be059ce156 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Wed, 9 Sep 2020 12:09:09 +0300 Subject: [PATCH] entityManagerProxy --- application/Espo/Core/ApplicationUser.php | 10 +-- .../Core/Authentication/Authentication.php | 6 +- .../Espo/Core/ORM/EntityManagerProxy.php | 75 +++++++++++++++++++ application/Espo/ORM/EntityManager.php | 2 +- 4 files changed, 84 insertions(+), 9 deletions(-) create mode 100644 application/Espo/Core/ORM/EntityManagerProxy.php diff --git a/application/Espo/Core/ApplicationUser.php b/application/Espo/Core/ApplicationUser.php index 5df5af4265..053b5bf19d 100644 --- a/application/Espo/Core/ApplicationUser.php +++ b/application/Espo/Core/ApplicationUser.php @@ -38,7 +38,7 @@ use Espo\Entities\{ }; use Espo\Core\{ - ORM\EntityManager, + ORM\EntityManagerProxy, }; /** @@ -47,12 +47,12 @@ use Espo\Core\{ class ApplicationUser { protected $container; - protected $entityManager; + protected $entityManagerProxy; - public function __construct(Container $container, EntityManager $entityManager) + public function __construct(Container $container, EntityManagerProxy $entityManagerProxy) { $this->container = $container; - $this->entityManager = $entityManager; + $this->entityManagerProxy = $entityManagerProxy; } /** @@ -60,7 +60,7 @@ class ApplicationUser */ public function setupSystemUser() { - $user = $this->entityManager->getEntity('User', 'system'); + $user = $this->entityManagerProxy->getEntity('User', 'system'); if (!$user) { throw new Error("System user is not found"); } diff --git a/application/Espo/Core/Authentication/Authentication.php b/application/Espo/Core/Authentication/Authentication.php index d67433beb2..b01c15fc1f 100644 --- a/application/Espo/Core/Authentication/Authentication.php +++ b/application/Espo/Core/Authentication/Authentication.php @@ -53,7 +53,7 @@ use Espo\Core\{ ApplicationState, Utils\Config, Utils\Metadata, - ORM\EntityManager, + ORM\EntityManagerProxy, Api\Request, }; @@ -86,7 +86,7 @@ class Authentication ApplicationState $applicationState, Config $config, Metadata $metadata, - EntityManager $entityManager, + EntityManagerProxy $entityManagerProxy, LoginFactory $authLoginFactory, TwoFAFactory $auth2FAFactory ) { @@ -96,7 +96,7 @@ class Authentication $this->applicationState = $applicationState; $this->config = $config; $this->metadata = $metadata; - $this->entityManager = $entityManager; + $this->entityManager = $entityManagerProxy; $this->authLoginFactory = $authLoginFactory; $this->auth2FAFactory = $auth2FAFactory; } diff --git a/application/Espo/Core/ORM/EntityManagerProxy.php b/application/Espo/Core/ORM/EntityManagerProxy.php new file mode 100644 index 0000000000..97393c663b --- /dev/null +++ b/application/Espo/Core/ORM/EntityManagerProxy.php @@ -0,0 +1,75 @@ +container = $container; + } + + private function getEntityManager() : EntityManager + { + if (!$this->entityManager) { + $this->entityManager = $this->container->get('entityManager'); + } + + return $this->entityManager; + } + + public function getEntity(string $entityType, ?string $id = null) : ?Entity + { + return $this->getEntityManager()->getEntity($entityType, $id); + } + + public function saveEntity(Entity $entity, array $options = []) + { + return $this->getEntityManager()->saveEntity($entity, $options); + } + + public function getRepository(string $entityType) : Repository + { + return $this->getEntityManager()->getRepository($entityType); + } +} diff --git a/application/Espo/ORM/EntityManager.php b/application/Espo/ORM/EntityManager.php index d6b16401c2..6b7abdb3a9 100644 --- a/application/Espo/ORM/EntityManager.php +++ b/application/Espo/ORM/EntityManager.php @@ -351,7 +351,7 @@ class EntityManager /** * Get a repository for a specific entity type. */ - public function getRepository(string $entityType) : ?Repository + public function getRepository(string $entityType) : Repository { if (!$this->hasRepository($entityType)) { throw new RuntimeException("Repository '{$entityType}' does not exist.");