mirror of
https://github.com/espocrm/espocrm.git
synced 2026-06-28 06:56:05 +00:00
injectable setter
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -68,6 +68,7 @@ class ServiceFactory
|
||||
|
||||
$obj = $this->injectableFactory->create($className);
|
||||
|
||||
// deprecated
|
||||
if (method_exists($obj, 'prepare')) {
|
||||
$obj->prepare();
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
{
|
||||
}
|
||||
|
||||
@@ -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');
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user