mirror of
https://github.com/espocrm/espocrm.git
synced 2026-06-28 06:56:05 +00:00
container get by class
This commit is contained in:
@@ -29,6 +29,7 @@
|
||||
|
||||
namespace Espo\Core;
|
||||
|
||||
use Espo\Core\Binding\Binding;
|
||||
use Espo\Core\Container\Exceptions\NotFoundException;
|
||||
use Espo\Core\Container\Exceptions\NotSettableException;
|
||||
use Espo\Core\Container\Loader;
|
||||
@@ -45,7 +46,7 @@ use RuntimeException;
|
||||
|
||||
/**
|
||||
* DI container for services. Lazy initialization is used. Services are instantiated only once.
|
||||
* See https://docs.espocrm.com/development/di/.
|
||||
* @see https://docs.espocrm.com/development/di/.
|
||||
*/
|
||||
class Container implements ContainerInterface
|
||||
{
|
||||
@@ -60,7 +61,6 @@ class Container implements ContainerInterface
|
||||
private array $loaderClassNames;
|
||||
|
||||
private ?Configuration $configuration = null;
|
||||
private ?BindingContainer $bindingContainer = null;
|
||||
private InjectableFactory $injectableFactory;
|
||||
|
||||
/**
|
||||
@@ -71,9 +71,9 @@ class Container implements ContainerInterface
|
||||
*/
|
||||
public function __construct(
|
||||
string $configurationClassName,
|
||||
private BindingContainer $bindingContainer,
|
||||
array $loaderClassNames = [],
|
||||
array $services = [],
|
||||
?BindingContainer $bindingContainer = null
|
||||
array $services = []
|
||||
) {
|
||||
$this->loaderClassNames = $loaderClassNames;
|
||||
|
||||
@@ -85,8 +85,6 @@ class Container implements ContainerInterface
|
||||
$this->setForced($name, $service);
|
||||
}
|
||||
|
||||
$this->bindingContainer = $bindingContainer;
|
||||
|
||||
/** @var InjectableFactory $injectableFactory */
|
||||
$injectableFactory = $this->get(self::ID_INJECTABLE_FACTORY);
|
||||
$this->injectableFactory = $injectableFactory;
|
||||
@@ -95,9 +93,7 @@ class Container implements ContainerInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtain a service object.
|
||||
*
|
||||
* @throws NotFoundExceptionInterface If not gettable.
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function get(string $id): object
|
||||
{
|
||||
@@ -113,7 +109,7 @@ class Container implements ContainerInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether a service can be obtained.
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function has(string $id): bool
|
||||
{
|
||||
@@ -146,6 +142,31 @@ class Container implements ContainerInterface
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
* @template T of object
|
||||
* @param class-string<T> $className A class name or interface name.
|
||||
* @return T A service instance.
|
||||
* @throws NotFoundExceptionInterface If not gettable.
|
||||
*/
|
||||
public function getByClass(string $className): object
|
||||
{
|
||||
$binding = $this->bindingContainer->getByInterface($className);
|
||||
|
||||
if ($binding->getType() !== Binding::CONTAINER_SERVICE) {
|
||||
throw new NotFoundException("No service bound to `{$className}`.");
|
||||
}
|
||||
|
||||
$id = $binding->getValue();
|
||||
|
||||
if (!is_string($id)) {
|
||||
throw new LogicException();
|
||||
}
|
||||
|
||||
/** @var T */
|
||||
return $this->get($id);
|
||||
}
|
||||
|
||||
private function isSet(string $id): bool
|
||||
{
|
||||
return isset($this->data[$id]);
|
||||
@@ -243,9 +264,7 @@ class Container implements ContainerInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a service object. Must be configured as settable.
|
||||
*
|
||||
* @throws NotSettableException Is not settable or already set.
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function set(string $id, object $object): void
|
||||
{
|
||||
|
||||
@@ -36,8 +36,7 @@ use ReflectionClass;
|
||||
|
||||
/**
|
||||
* DI container for services. Lazy initialization is used. Services are instantiated only once.
|
||||
*
|
||||
* See https://docs.espocrm.com/development/di/.
|
||||
* @see https://docs.espocrm.com/development/di/.
|
||||
*/
|
||||
interface Container extends ContainerInterface
|
||||
{
|
||||
@@ -67,4 +66,14 @@ interface Container extends ContainerInterface
|
||||
* @throws NotFoundExceptionInterface If not gettable.
|
||||
*/
|
||||
public function getClass(string $id): ReflectionClass;
|
||||
|
||||
/**
|
||||
* Get a service by a class name. A service should be bound to a class or interface.
|
||||
*
|
||||
* @template T of object
|
||||
* @param class-string<T> $className A class name or interface name.
|
||||
* @return T A service instance.
|
||||
* @throws NotFoundExceptionInterface If not gettable.
|
||||
*/
|
||||
public function getByClass(string $className): object;
|
||||
}
|
||||
|
||||
@@ -30,7 +30,6 @@
|
||||
namespace Espo\Core\Container;
|
||||
|
||||
use Espo\Core\Container;
|
||||
use Espo\Core\Container\ContainerConfiguration;
|
||||
use Espo\Core\Container\Container as ContainerInterface;
|
||||
|
||||
use Espo\Core\Binding\BindingContainer;
|
||||
@@ -194,9 +193,9 @@ class ContainerBuilder
|
||||
|
||||
return new $this->containerClassName(
|
||||
$this->containerConfigurationClassName,
|
||||
$bindingContainer,
|
||||
$this->loaderClassNames,
|
||||
$this->services,
|
||||
$bindingContainer
|
||||
$this->services
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
42
tests/integration/Espo/Core/ContainerTest.php
Normal file
42
tests/integration/Espo/Core/ContainerTest.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM - Open Source CRM application.
|
||||
* Copyright (C) 2014-2023 Yurii Kuznietsov, Taras Machyshyn, Oleksii 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 tests\integration\Espo\Core;
|
||||
|
||||
use Espo\ORM\EntityManager;
|
||||
|
||||
class ContainerTest extends \tests\integration\Core\BaseTestCase
|
||||
{
|
||||
public function testGetByClass1(): void
|
||||
{
|
||||
$em = $this->getContainer()->getByClass(EntityManager::class);
|
||||
|
||||
$this->assertInstanceOf(EntityManager::class, $em);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user