gpt4 book ai didi

php - JMS DI 额外 bundle : How do I inject a dependency only if it exists

转载 作者:可可西里 更新时间:2023-11-01 01:03:54 29 4
gpt4 key购买 nike

我正在使用 JMS DI 注入(inject)带有注解的服务:

use JMS\DiExtraBundle\Annotation as DI;
/**
* @DI\Service("foo.bar.service")
*/
class myClass
{
/**
* @DI\Inject("debug.stopwatch")
* @var $stopWatch \Symfony\Component\Stopwatch\Stopwatch
*/
public $stopWatch;

/**
* @DI\Inject("serializer")
* @var $serializer \JMS\Serializer\Serializer
*/
public $serializer;

public function toto()
{
if (isset($this->stopwatch)) {
$this->stopWatch->start("init");
}
}
}

但是 StopWatch 只能在 Dev Env 中使用,所以在 prod 中运行时:

The service "foo.bar.service" has a dependency on a non-existent service "debug.stopwatch".' in...

我的问题:如何在类里面正确地注入(inject)秒表服务?

最佳答案

建立依赖optional :

@DI\Inject("debug.stopwatch", required=false)

关于php - JMS DI 额外 bundle : How do I inject a dependency only if it exists,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15867778/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com