From 30bb9f4fcca654dbac2d917dd879988c3360352d Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Sat, 20 Jun 2020 15:53:32 +0300 Subject: [PATCH] di setter --- application/Espo/Core/Acl/Base.php | 2 +- application/Espo/Core/Di/AclAware.php | 37 +++++++++++++ application/Espo/Core/Di/AclSetter.php | 42 +++++++++++++++ application/Espo/Core/Di/ConfigAware.php | 37 +++++++++++++ application/Espo/Core/Di/ConfigSetter.php | 42 +++++++++++++++ .../Espo/Core/Di/EntityManagerAware.php | 37 +++++++++++++ .../Espo/Core/Di/EntityManagerSetter.php | 42 +++++++++++++++ application/Espo/Core/Di/MetadataAware.php | 37 +++++++++++++ application/Espo/Core/Di/MetadataSetter.php | 42 +++++++++++++++ .../Core/Di/SelectManagerFactoryAware.php | 37 +++++++++++++ .../Core/Di/SelectManagerFactorySetter.php | 42 +++++++++++++++ .../Espo/Core/FileStorage/Storages/Base.php | 2 +- .../Espo/Core/Formula/Functions/Base.php | 2 +- application/Espo/Core/Hooks/Base.php | 2 +- application/Espo/Core/Injectable.php | 3 +- application/Espo/Core/InjectableFactory.php | 53 ++++++++++++++----- .../Espo/Core/Interfaces/Injectable.php | 2 +- application/Espo/Core/Notificators/Base.php | 2 +- .../Espo/Core/ORM/Repositories/RDB.php | 2 +- application/Espo/Core/Services/Base.php | 3 +- application/Espo/Core/Traits/Injectable.php | 3 +- .../Core/Utils/EntityManager/Hooks/Base.php | 2 +- .../Core/Utils/FieldManager/Hooks/Base.php | 2 +- application/Espo/Services/GlobalSearch.php | 35 +++++------- 24 files changed, 463 insertions(+), 47 deletions(-) create mode 100644 application/Espo/Core/Di/AclAware.php create mode 100644 application/Espo/Core/Di/AclSetter.php create mode 100644 application/Espo/Core/Di/ConfigAware.php create mode 100644 application/Espo/Core/Di/ConfigSetter.php create mode 100644 application/Espo/Core/Di/EntityManagerAware.php create mode 100644 application/Espo/Core/Di/EntityManagerSetter.php create mode 100644 application/Espo/Core/Di/MetadataAware.php create mode 100644 application/Espo/Core/Di/MetadataSetter.php create mode 100644 application/Espo/Core/Di/SelectManagerFactoryAware.php create mode 100644 application/Espo/Core/Di/SelectManagerFactorySetter.php diff --git a/application/Espo/Core/Acl/Base.php b/application/Espo/Core/Acl/Base.php index e927f100f8..d2fee4f969 100644 --- a/application/Espo/Core/Acl/Base.php +++ b/application/Espo/Core/Acl/Base.php @@ -69,7 +69,7 @@ class Base implements Injectable protected function getInjection($name) { - return $this->injections[$name]; + return $this->injections[$name] ?? $this->$name ?? null; } protected function addDependencyList(array $list) diff --git a/application/Espo/Core/Di/AclAware.php b/application/Espo/Core/Di/AclAware.php new file mode 100644 index 0000000000..1ee25c3876 --- /dev/null +++ b/application/Espo/Core/Di/AclAware.php @@ -0,0 +1,37 @@ +acl = $acl; + } +} diff --git a/application/Espo/Core/Di/ConfigAware.php b/application/Espo/Core/Di/ConfigAware.php new file mode 100644 index 0000000000..f7a6c42e56 --- /dev/null +++ b/application/Espo/Core/Di/ConfigAware.php @@ -0,0 +1,37 @@ +config = $config; + } +} diff --git a/application/Espo/Core/Di/EntityManagerAware.php b/application/Espo/Core/Di/EntityManagerAware.php new file mode 100644 index 0000000000..76cb737081 --- /dev/null +++ b/application/Espo/Core/Di/EntityManagerAware.php @@ -0,0 +1,37 @@ +entityManager = $entityManager; + } +} diff --git a/application/Espo/Core/Di/MetadataAware.php b/application/Espo/Core/Di/MetadataAware.php new file mode 100644 index 0000000000..bbfeafae20 --- /dev/null +++ b/application/Espo/Core/Di/MetadataAware.php @@ -0,0 +1,37 @@ +metadata = $metadata; + } +} diff --git a/application/Espo/Core/Di/SelectManagerFactoryAware.php b/application/Espo/Core/Di/SelectManagerFactoryAware.php new file mode 100644 index 0000000000..a45eedf78e --- /dev/null +++ b/application/Espo/Core/Di/SelectManagerFactoryAware.php @@ -0,0 +1,37 @@ +selectManagerFactory = $selectManagerFactory; + } +} diff --git a/application/Espo/Core/FileStorage/Storages/Base.php b/application/Espo/Core/FileStorage/Storages/Base.php index c19ddf9cbb..75e7f840fb 100644 --- a/application/Espo/Core/FileStorage/Storages/Base.php +++ b/application/Espo/Core/FileStorage/Storages/Base.php @@ -54,7 +54,7 @@ abstract class Base implements Injectable protected function getInjection($name) { - return $this->injections[$name]; + return $this->injections[$name] ?? $this->$name ?? null; } protected function addDependency($name) diff --git a/application/Espo/Core/Formula/Functions/Base.php b/application/Espo/Core/Formula/Functions/Base.php index d2ead554de..6bf12f5a6d 100644 --- a/application/Espo/Core/Formula/Functions/Base.php +++ b/application/Espo/Core/Formula/Functions/Base.php @@ -57,7 +57,7 @@ abstract class Base implements Injectable protected function getInjection($name) { - return $this->injections[$name]; + return $this->injections[$name] ?? $this->$name ?? null; } protected function addDependency($name) diff --git a/application/Espo/Core/Hooks/Base.php b/application/Espo/Core/Hooks/Base.php index 3c7cde3078..51626767bd 100644 --- a/application/Espo/Core/Hooks/Base.php +++ b/application/Espo/Core/Hooks/Base.php @@ -77,7 +77,7 @@ abstract class Base implements Injectable protected function getInjection($name) { - return $this->injections[$name]; + return $this->injections[$name] ?? $this->$name ?? null; } public function inject($name, $object) diff --git a/application/Espo/Core/Injectable.php b/application/Espo/Core/Injectable.php index 26018509fc..7f58c6a6e7 100644 --- a/application/Espo/Core/Injectable.php +++ b/application/Espo/Core/Injectable.php @@ -29,6 +29,7 @@ namespace Espo\Core; +/** Deprecated */ abstract class Injectable implements \Espo\Core\Interfaces\Injectable { protected $dependencyList = []; @@ -62,7 +63,7 @@ abstract class Injectable implements \Espo\Core\Interfaces\Injectable protected function getInjection($name) { - return $this->injections[$name]; + return $this->injections[$name] ?? $this->$name ?? null; } protected function addDependency($name) diff --git a/application/Espo/Core/InjectableFactory.php b/application/Espo/Core/InjectableFactory.php index c43238a214..2233e86be1 100644 --- a/application/Espo/Core/InjectableFactory.php +++ b/application/Espo/Core/InjectableFactory.php @@ -31,13 +31,15 @@ namespace Espo\Core; use Espo\Core\Exceptions\Error; +use ReflectionClass; + /** * Creates instance by class name. Uses either Injectable interface or constructor param names to detect * which dependencies are needed. Only container services supported as dependencies. */ class InjectableFactory { - private $container; + protected $container; public function __construct(Container $container) { @@ -55,7 +57,7 @@ class InjectableFactory throw new Error("Class '{$className}' does not exist."); } - $class = new \ReflectionClass($className); + $class = new ReflectionClass($className); if ($class->implementsInterface('\\Espo\\Core\\Interfaces\\Injectable')) { return $this->createByClassNameInjectable($className); } @@ -65,7 +67,7 @@ class InjectableFactory protected function createByClassNameByConstructorParams(string $className) { - $class = new \ReflectionClass($className); + $class = new ReflectionClass($className); $injectionList = []; @@ -83,7 +85,7 @@ class InjectableFactory } $name = $param->getName(); - $injection = $this->getContainer()->get($name); + $injection = $this->container->get($name); if (!$injection) { throw new Error("InjectableFactory: Could not create {$className}, dependency {$name} not found."); @@ -93,13 +95,20 @@ class InjectableFactory } } - return $class->newInstanceArgs($injectionList); + $obj = $class->newInstanceArgs($injectionList); + + $this->processSetterInjections($class, $obj); + + return $obj; } + /** Deprecated */ protected function createByClassNameInjectable(string $className) { $obj = new $className(); - $class = new \ReflectionClass($className); + $class = new ReflectionClass($className); + + $setList = []; $dependencyList = $obj->getDependencyList(); foreach ($dependencyList as $name) { @@ -107,14 +116,37 @@ class InjectableFactory if ($this->classHasDependencySetter($class, $name)) { $methodName = 'set' . ucfirst($name); $obj->$methodName($injection); + $setList[] = $name; } $obj->inject($name, $injection); } + $this->processSetterInjections($class, $obj, $setList); + return $obj; } - protected function classHasDependencySetter(\ReflectionClass $class, string $name) : bool + protected function processSetterInjections(ReflectionClass $class, object $obj, array $ignoreList = []) + { + foreach ($class->getInterfaces() as $interface) { + $interfaceName = $interface->getShortName(); + + if (substr($interfaceName, -5) !== 'Aware' || strlen($interfaceName) <= 5) continue; + + $name = lcfirst(substr($interfaceName, 0, -5)); + + if (in_array($name, $ignoreList)) continue; + + if (!$this->classHasDependencySetter($class, $name, true)) continue; + + $injection = $this->container->get($name); + + $methodName = 'set' . ucfirst($name); + $obj->$methodName($injection); + } + } + + protected function classHasDependencySetter(ReflectionClass $class, string $name, bool $skipInstanceCheck = false) : bool { $methodName = 'set' . ucfirst($name); @@ -131,15 +163,10 @@ class InjectableFactory $paramClass = $params[0]->getClass(); - if ($paramClass && $paramClass->isInstance($injection)) { + if ($skipInstanceCheck || $paramClass && $paramClass->isInstance($injection)) { return true; } return false; } - - protected function getContainer() - { - return $this->container; - } } diff --git a/application/Espo/Core/Interfaces/Injectable.php b/application/Espo/Core/Interfaces/Injectable.php index 5c049ce3de..c70e6c936c 100644 --- a/application/Espo/Core/Interfaces/Injectable.php +++ b/application/Espo/Core/Interfaces/Injectable.php @@ -29,10 +29,10 @@ namespace Espo\Core\Interfaces; +/** Deprecated */ interface Injectable { public function getDependencyList(); public function inject($name, $object); } - diff --git a/application/Espo/Core/Notificators/Base.php b/application/Espo/Core/Notificators/Base.php index 14cffff93c..1875b4f068 100644 --- a/application/Espo/Core/Notificators/Base.php +++ b/application/Espo/Core/Notificators/Base.php @@ -81,7 +81,7 @@ class Base implements Injectable protected function getInjection($name) { - return $this->injections[$name]; + return $this->injections[$name] ?? $this->$name ?? null; } public function inject($name, $object) diff --git a/application/Espo/Core/ORM/Repositories/RDB.php b/application/Espo/Core/ORM/Repositories/RDB.php index b66918f19b..bec721222b 100644 --- a/application/Espo/Core/ORM/Repositories/RDB.php +++ b/application/Espo/Core/ORM/Repositories/RDB.php @@ -78,7 +78,7 @@ class RDB extends \Espo\ORM\Repositories\RDB implements Injectable protected function getInjection($name) { - return $this->injections[$name]; + return $this->injections[$name] ?? $this->$name ?? null; } public function getDependencyList() diff --git a/application/Espo/Core/Services/Base.php b/application/Espo/Core/Services/Base.php index 2ecac961b8..e56fe0b764 100644 --- a/application/Espo/Core/Services/Base.php +++ b/application/Espo/Core/Services/Base.php @@ -61,9 +61,10 @@ abstract class Base implements Injectable { } + /** Deprecated */ protected function getInjection($name) { - return $this->injections[$name]; + return $this->injections[$name] ?? $this->$name ?? null; } protected function addDependency($name) diff --git a/application/Espo/Core/Traits/Injectable.php b/application/Espo/Core/Traits/Injectable.php index ea9d2054bf..df5197ff94 100644 --- a/application/Espo/Core/Traits/Injectable.php +++ b/application/Espo/Core/Traits/Injectable.php @@ -29,6 +29,7 @@ namespace Espo\Core\Traits; +/** Deprecated */ trait Injectable { private $injections = []; @@ -45,7 +46,7 @@ trait Injectable protected function getInjection(string $name) { - return $this->injections[$name]; + return $this->injections[$name] ?? $this->$name ?? null; } protected function addDependency(string $name) diff --git a/application/Espo/Core/Utils/EntityManager/Hooks/Base.php b/application/Espo/Core/Utils/EntityManager/Hooks/Base.php index 4e6c6f0cb7..0e69d8ba8f 100644 --- a/application/Espo/Core/Utils/EntityManager/Hooks/Base.php +++ b/application/Espo/Core/Utils/EntityManager/Hooks/Base.php @@ -67,7 +67,7 @@ abstract class Base protected function getInjection($name) { - return $this->injections[$name]; + return $this->injections[$name] ?? $this->$name ?? null; } public function inject($name, $object) diff --git a/application/Espo/Core/Utils/FieldManager/Hooks/Base.php b/application/Espo/Core/Utils/FieldManager/Hooks/Base.php index 5c487b69e5..0063aecf2f 100644 --- a/application/Espo/Core/Utils/FieldManager/Hooks/Base.php +++ b/application/Espo/Core/Utils/FieldManager/Hooks/Base.php @@ -67,7 +67,7 @@ abstract class Base protected function getInjection($name) { - return $this->injections[$name]; + return $this->injections[$name] ?? $this->$name ?? null; } public function inject($name, $object) diff --git a/application/Espo/Services/GlobalSearch.php b/application/Espo/Services/GlobalSearch.php index 8879fda519..5ecf8ffa6e 100644 --- a/application/Espo/Services/GlobalSearch.php +++ b/application/Espo/Services/GlobalSearch.php @@ -31,36 +31,27 @@ namespace Espo\Services; use Espo\Core\Exceptions\{ Forbidden, - NotFound + NotFound, }; use Espo\ORM\{ Entity, - EntityManager, }; -use Espo\Core\{ - Acl, - SelectManagerFactory, - Utils\Metadata, - Utils\Config, -}; +use Espo\Core\Di; -class GlobalSearch +class GlobalSearch implements + Di\EntityManagerAware, + Di\MetadataAware, + Di\AclAware, + Di\ConfigAware, + Di\SelectManagerFactoryAware { - public function __construct( - EntityManager $entityManager, - Metadata $metadata, - Acl $acl, - SelectManagerFactory $selectManagerFactory, - Config $config - ) { - $this->entityManager = $entityManager; - $this->metadata = $metadata; - $this->acl = $acl; - $this->selectManagerFactory = $selectManagerFactory; - $this->config = $config; - } + use Di\EntityManagerSetter; + use Di\MetadataSetter; + use Di\AclSetter; + use Di\ConfigSetter; + use Di\SelectManagerFactorySetter; public function find(string $query, int $offset, int $maxSize) {