diff --git a/application/Espo/Core/InjectableFactory.php b/application/Espo/Core/InjectableFactory.php index 5303a8124c..c43238a214 100644 --- a/application/Espo/Core/InjectableFactory.php +++ b/application/Espo/Core/InjectableFactory.php @@ -67,7 +67,7 @@ class InjectableFactory { $class = new \ReflectionClass($className); - $dependencyList = []; + $injectionList = []; $constructor = $class->getConstructor(); if (!is_null($constructor)) { @@ -77,40 +77,67 @@ class InjectableFactory $dependencyClassName = $param->getClass(); if (is_null($dependencyClassName)) { if ($param->isDefaultValueAvailable()) { - $dependencyList[] = $param->getDefaultValue(); + $injectionList[] = $param->getDefaultValue(); continue; } } $name = $param->getName(); - $dependency = $this->getContainer()->get($name); + $injection = $this->getContainer()->get($name); - if (!$dependency) { + if (!$injection) { throw new Error("InjectableFactory: Could not create {$className}, dependency {$name} not found."); } - $dependencyList[] = $dependency; + $injectionList[] = $injection; } } - return $class->newInstanceArgs($dependencyList); + return $class->newInstanceArgs($injectionList); } protected function createByClassNameInjectable(string $className) { $obj = new $className(); + $class = new \ReflectionClass($className); $dependencyList = $obj->getDependencyList(); foreach ($dependencyList as $name) { - $obj->inject($name, $this->container->get($name)); - } - if (method_exists($obj, 'prepare')) { - $obj->prepare(); + $injection = $this->container->get($name); + if ($this->classHasDependencySetter($class, $name)) { + $methodName = 'set' . ucfirst($name); + $obj->$methodName($injection); + } + $obj->inject($name, $injection); } return $obj; } + protected function classHasDependencySetter(\ReflectionClass $class, string $name) : bool + { + $methodName = 'set' . ucfirst($name); + + if (!$class->hasMethod($methodName) || !$class->getMethod($methodName)->isPublic()) { + return false; + } + + $params = $class->getMethod($methodName)->getParameters(); + if (!$params || !count($params)) { + return false; + } + + $injection = $this->container->get($name); + + $paramClass = $params[0]->getClass(); + + if ($paramClass && $paramClass->isInstance($injection)) { + return true; + } + + return false; + } + protected function getContainer() { return $this->container; diff --git a/application/Espo/Core/ServiceFactory.php b/application/Espo/Core/ServiceFactory.php index caec0fce08..a866fa14b5 100644 --- a/application/Espo/Core/ServiceFactory.php +++ b/application/Espo/Core/ServiceFactory.php @@ -68,6 +68,7 @@ class ServiceFactory $obj = $this->injectableFactory->create($className); + // deprecated if (method_exists($obj, 'prepare')) { $obj->prepare(); } diff --git a/application/Espo/Core/Services/Base.php b/application/Espo/Core/Services/Base.php index 0e8781558d..2ecac961b8 100644 --- a/application/Espo/Core/Services/Base.php +++ b/application/Espo/Core/Services/Base.php @@ -29,7 +29,7 @@ namespace Espo\Core\Services; -use \Espo\Core\Interfaces\Injectable; +use Espo\Core\Interfaces\Injectable; abstract class Base implements Injectable { @@ -56,6 +56,7 @@ abstract class Base implements Injectable { } + /** Deprecated */ public function prepare() { } diff --git a/application/Espo/Services/Record.php b/application/Espo/Services/Record.php index cb1bed34c0..1110bdda81 100644 --- a/application/Espo/Services/Record.php +++ b/application/Espo/Services/Record.php @@ -41,6 +41,9 @@ use Espo\Core\Exceptions\ForbiddenSilent; use Espo\Core\Exceptions\ConflictSilent; use Espo\Core\Utils\Util; +use Espo\Core\AclManager; +use Espo\Core\Acl; +use Espo\Entities\User; class Record extends \Espo\Core\Services\Base { @@ -136,6 +139,8 @@ class Record extends \Espo\Core\Services\Base private $user = null; + private $aclManager = null; + const MAX_SELECT_TEXT_ATTRIBUTE_LENGTH = 5000; const FOLLOWERS_LIMIT = 4; @@ -157,11 +162,9 @@ class Record extends \Espo\Core\Services\Base $this->entityName = $this->entityType; } - public function prepare() + public function setAclManager(AclManager $aclManager) { - parent::prepare(); - - $aclManager = $this->getInjection('aclManager'); + $this->aclManager = $aclManager; foreach ($aclManager->getScopeRestrictedAttributeList($this->entityType, 'forbidden') as $item) { if (!in_array($item, $this->forbiddenAttributeList)) $this->forbiddenAttributeList[] = $item; @@ -231,18 +234,26 @@ class Record extends \Espo\Core\Services\Base return $this->getInjection('user'); } - public function setAcl(\Espo\Core\Acl $acl) + public function setAcl(Acl $acl) { $this->acl = $acl; + + // for backward compatibility + $this->inject('acl', $acl); } - public function setUser(\Espo\Entities\User $user) + public function setUser(User $user) { $this->user = $user; + + // for backward compatibility + $this->inject('user', $user); } protected function getAclManager() { + if ($this->aclManager) return $this->aclManager; + return $this->getInjection('aclManager'); }