3

我正在使用 JMS DI 注入带有注释的服务:

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...

我的问题:如何在课堂上正确注入秒表服务?

4

1 回答 1

4

使依赖项可选

@DI\Inject("debug.stopwatch", required=false)
于 2013-04-07T21:07:33.987 回答