di setter

This commit is contained in:
Yuri Kuznetsov
2020-06-20 15:53:32 +03:00
parent 626da4c8bb
commit 30bb9f4fcc
24 changed files with 463 additions and 47 deletions
+1 -1
View File
@@ -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)
+37
View File
@@ -0,0 +1,37 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2020 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
* Website: https://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Di;
use Espo\Core\Acl;
interface AclAware
{
public function setAcl(Acl $acl);
}
+42
View File
@@ -0,0 +1,42 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2020 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
* Website: https://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Di;
use Espo\Core\Acl;
trait AclSetter
{
protected $acl;
public function setAcl(Acl $acl)
{
$this->acl = $acl;
}
}
+37
View File
@@ -0,0 +1,37 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2020 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
* Website: https://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Di;
use Espo\Core\Utils\Config;
interface ConfigAware
{
public function setConfig(Config $config);
}
+42
View File
@@ -0,0 +1,42 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2020 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
* Website: https://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Di;
use Espo\Core\Utils\Config;
trait ConfigSetter
{
protected $config;
public function setConfig(Config $config)
{
$this->config = $config;
}
}
@@ -0,0 +1,37 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2020 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
* Website: https://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Di;
use Espo\Core\ORM\EntityManager;
interface EntityManagerAware
{
public function setEntityManager(EntityManager $entityManager);
}
@@ -0,0 +1,42 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2020 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
* Website: https://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Di;
use Espo\Core\ORM\EntityManager;
trait EntityManagerSetter
{
protected $entityManager;
public function setEntityManager(EntityManager $entityManager)
{
$this->entityManager = $entityManager;
}
}
@@ -0,0 +1,37 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2020 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
* Website: https://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Di;
use Espo\Core\Utils\Metadata;
interface MetadataAware
{
public function setMetadata(Metadata $metadata);
}
@@ -0,0 +1,42 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2020 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
* Website: https://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Di;
use Espo\Core\Utils\Metadata;
trait MetadataSetter
{
protected $metadata;
public function setMetadata(Metadata $metadata)
{
$this->metadata = $metadata;
}
}
@@ -0,0 +1,37 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2020 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
* Website: https://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Di;
use Espo\Core\SelectManagerFactory;
interface SelectManagerFactoryAware
{
public function setSelectManagerFactory(SelectManagerFactory $selectManagerFactory);
}
@@ -0,0 +1,42 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2020 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
* Website: https://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Di;
use Espo\Core\SelectManagerFactory;
trait SelectManagerFactorySetter
{
protected $selectManagerFactory;
public function setSelectManagerFactory(SelectManagerFactory $selectManagerFactory)
{
$this->selectManagerFactory = $selectManagerFactory;
}
}
@@ -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)
@@ -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)
+1 -1
View File
@@ -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)
+2 -1
View File
@@ -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)
+40 -13
View File
@@ -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;
}
}
@@ -29,10 +29,10 @@
namespace Espo\Core\Interfaces;
/** Deprecated */
interface Injectable
{
public function getDependencyList();
public function inject($name, $object);
}
+1 -1
View File
@@ -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)
@@ -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()
+2 -1
View File
@@ -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)
+2 -1
View File
@@ -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)
@@ -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)
@@ -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)
+13 -22
View File
@@ -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)
{