我正在努力通过类别名称从注入的已标记服务组中获取特定服务。
这里是一个示例: 我标记了所有实现DriverInterface'app.driver'的服务,并将其绑定到$ drivers变量。
在其他一些服务中,我需要获取所有标记为“ app.driver”的驱动程序,并实例化并仅使用其中一些。但是需要的驱动程序是动态的。
services.yml
_defaults:
autowire: true
autoconfigure: true
public: false
bind:
$drivers: [!tagged app.driver]
_instanceof:
DriverInterface:
tags: ['app.driver']
其他一些服务:
/**
* @var iterable
*/
private $drivers;
/**
* @param iterable $drivers
*/
public function __construct(iterable $drivers)
{
$this->drivers = $drivers;
}
public function getDriverByClassName(string $className): DriverInterface
{
????????
}
因此,将实现驱动程序接口的服务作为迭代结果注入到$ this-> drivers参数中。我只能遍历它们,但是所有服务都将被实例化。
是否还有其他方法可以将这些服务注入,以通过类名从它们那里获得特定服务而无需实例化其他人?
P.S。我知道有可能将这些驱动程序公开并改为使用容器,但是我想避免将硬币容器注入服务中,如果可能的话。
答案 0 :(得分:2)
ServiceLocator将允许按名称访问服务,而无需实例化其余服务。确实需要编译器通过,但设置起来并不难。
use Symfony\Component\DependencyInjection\ServiceLocator;
class DriverLocator extends ServiceLocator
{
// Leave empty
}
# Some Service
public function __construct(DriverLocator $driverLocator)
{
$this->driverLocator = $driverLocator;
}
public function getDriverByClassName(string $className): DriverInterface
{
return $this->driverLocator->get($fullyQualifiedClassName);
}
现在魔术了:
# src/Kernel.php
# Make your kernel a compiler pass
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
class Kernel extends BaseKernel implements CompilerPassInterface {
...
# Dynamically add all drivers to the locator using a compiler pass
public function process(ContainerBuilder $container)
{
$driverIds = [];
foreach ($container->findTaggedServiceIds('app.driver') as $id => $tags) {
$driverIds[$id] = new Reference($id);
}
$driverLocator = $container->getDefinition(DriverLocator::class);
$driverLocator->setArguments([$driverIds]);
}
然后保存。假设您修复了我可能引入的任何语法错误或错字,它就可以正常工作。
要获得额外的荣誉,您可以自动注册驱动程序类,并删除服务文件中的该instanceof实例。
# Kernel.php
protected function build(ContainerBuilder $container)
{
$container->registerForAutoconfiguration(DriverInterface::class)
->addTag('app.driver');
}
答案 1 :(得分:2)
您不再需要创建编译器通道来配置服务定位器。
可以通过配置完成所有操作,并让Symfony执行“魔术”。
您可以对配置进行以下补充:
services:
_instanceof:
DriverInterface:
tags: ['app.driver']
lazy: true
DriverConsumer:
arguments:
- !tagged_locator
tag: 'app.driver'
需要访问这些服务而不是接收iterable
的服务将接收ServiceLocatorInterface
:
class DriverConsumer
{
private $drivers;
public function __construct(ServiceLocatorInterface $locator)
{
$this->locator = $locator;
}
public function foo() {
$driver = $this->locator->get(Driver::class);
// where Driver is a concrete implementation of DriverInterface
}
}
然后就是这样。您不需要任何其他功能,它只适用于 tm 。
包含所有类的完整示例。
我们有:
FooInterface
:interface FooInterface
{
public function whoAmI(): string;
}
AbstractFoo
为了简化实施,我们将在具体服务中扩展一个抽象类:
abstract class AbstractFoo implements FooInterface
{
public function whoAmI(): string {
return get_class($this);
}
}
几个实现FooInterface
class FooOneService extends AbstractFoo { }
class FooTwoService extends AbstractFoo { }
另一个需要服务定位器才能使用我们刚刚定义的这两个服务的服务:
class Bar
{
/**
* @var \Symfony\Component\DependencyInjection\ServiceLocator
*/
private $service_locator;
public function __construct(ServiceLocator $service_locator) {
$this->service_locator = $service_locator;
}
public function handle(): string {
/** @var \App\Test\FooInterface $service */
$service = $this->service_locator->get(FooOneService::class);
return $service->whoAmI();
}
}
唯一需要的配置是这样:
services:
_instanceof:
App\Test\FooInterface:
tags: ['test_foo_tag']
lazy: true
App\Test\Bar:
arguments:
- !tagged_locator
tag: 'test_foo_tag'
如果您不想使用类名来定义自己的服务名,则可以使用静态方法来定义服务名。配置将更改为:
App\Test\Bar:
arguments:
- !tagged_locator
tag: 'test_foo_tag'
default_index_method: 'fooIndex'
其中fooIndex
是在每个返回字符串的服务上定义的公共静态方法。警告:如果使用此方法,将无法通过其类名获取服务。