【问题标题】:How to inherit the service dependencies (from a bundle)如何继承服务依赖性(从捆绑包中)
【发布时间】:2022-11-25 01:04:50
【问题描述】:

我想覆盖放置在供应商包中的服务“foo”方法:

class SamlProvider implements AuthenticationProviderInterface
{
    protected $userProvider;
    protected $userFactory;
    protected $tokenFactory;
    protected $eventDispatcher;

    public function __construct(UserProviderInterface $userProvider, ?EventDispatcherInterface $eventDispatcher)
    {
        $this->userProvider = $userProvider;
        $this->eventDispatcher = $eventDispatcher;
    }

    protected function foo()
    {
        ....
    }

我创建自己的服务并扩展供应商服务:

class SamlUserProvider extends SamlProvider
{
    protected function foo()
    {
        echo 'bar';
    }
}

现在我需要在 service.yml 中定义依赖项,我对此一无所知,因为它是一个供应商包。

如何从子类继承服务定义?

【问题讨论】:

    标签: symfony dependency-injection symfony6


    【解决方案1】:

    如果我理解这个问题,那么您真正需要做的就是更改原始服务的类别。可以通过编译器传递完成的事情:

    class Kernel implements CompilerPassInterface
    {
        public function process(ContainerBuilder $container)
        {
            // Or use the actual service id if it's not the class name
            $definition = $container->getDefinition(SamlProvider::class);
            $definition->setClass(SamlUserProvider::class);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-03
      • 2015-10-04
      • 2013-04-01
      • 2021-02-12
      • 2017-09-10
      • 2017-09-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多