我正在使用 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/
我已经像这样安装了一个新的Rails项目:$railsnewsite它执行并到达:bundleinstall但是当它似乎尝试安装依赖项时我得到了这个错误Gem::Ext::BuildError:ERROR:Failedtobuildgemnativeextension./System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/rubyextconf.rbcheckingforlibkern/OSAtomic.h...yescreatingMakefilemake"DESTDIR="cleanmake"DESTDIR="
为了在我的mac上为一个rails项目安装mysql,我遵循了安装Homebrew软件和删除mac端口的在线建议。这是问题开始的地方。rails项目不会构建,我得到这个:[rake--prereqs]rakeaborted!dlopen(/Users/Parker/.rvm/gems/ruby-1.9.3-p448/gems/nokogiri-1.6.0/lib/nokogiri/nokogiri.bundle,9):Librarynotloaded:/opt/local/lib/libiconv.2.dylibReferencedfrom:/Users/Parker/.rvm/gem
我定义了一个方法:defmethod(one:1,two:2)[one,two]end当我这样调用它时:methodone:'one',three:'three'我得到:ArgumentError:unknownkeyword:three我不想从散列中一个一个地提取所需的键或排除额外的键。除了像这样定义方法之外,有没有办法规避这种行为:defmethod(one:1,two:2,**other)[one,two,other]end 最佳答案 如果不想写**other中的other,可以省略。defmethod(one:1,two:2
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visitthehelpcenter指导。关闭9年前。我来自C、php和bash背景,很容易学习,因为它们都有相同的C结构,我可以将其与我已经知道的联系起来。然后2年前我学了Python并且学得很好,Python对我来说比Ruby更容易学。然后从去年开始,我一直在尝试学习Ruby,然后是Rails,我承认,直到现在我还是学不会,讽刺的是那些打着简单易学的烙印,但是对于我这样一个老练的程序员来说,我只是无法将它
当我尝试进行bundle安装时,我的gem_path和gem_home指向/usr/local/rvm/gems/我没有写入权限,并且由于权限无效而失败。因此,我已将两个路径都更改为我具有写入权限的本地目录。这样做时,我进行了bundle安装,我得到:bruno@test6:~$bundleinstallFetchinggemmetadatafromhttps://rubygems.org/.........Fetchinggemmetadatafromhttps://rubygems.org/..Bundler::GemspecError:Couldnotreadgemat/afs/
运行bundle安装时,我收到以下消息:Rubygems2.0.14isnotthreadsafe,soyourgemswillbeinstalledoneatatime.UpgradetoRubygems2.1.0orhighertoenableparallelgeminstallation.这很奇怪,因为在我的RubyGems环境中它说我的RubyGems版本是:2.4.5.1(见下文)~/w/Rafftopia❯❯❯gemenvRubyGemsEnvironment:-RUBYGEMSVERSION:2.4.5.1-RUBYVERSION:2.2.5(2016-04-26patc
我在目录“/home/enterprise/pkg”中有一个本地gem(enterprise-0.0.1.gem)。它依赖于active_directorygem(v1.5.5),这是在它的enterprise.gemspec文件中指定的,如下所示:-gem.add_dependency("active_directory")在我的应用程序的Gemfile中,我添加了以下行:-gem'enterprise','0.0.1',path=>'/home/enterprise/pkg'当我做的时候bundleinstall在我的应用程序的源目录中,只安装了企业gem。因此,我遇到了引用act
我有以下模型用户has_many:users_contactshas_many:contacts,through::users_contactsaccepts_nested_attributes_for:contacts,allow_destroy:true联系方式has_many:users_contactshas_many:users,through::users_contactsaccepts_nested_attributes_for:users_contacts,allow_destroy:true用户联系belongs_to:usersbelongs_to:contacts
刚拿到一台新机器,我借此机会尝试了一些新东西。RVM很棒,我一直在使用gemset,但在阅读了一些博客文章后,我决定使用switch到rbenv并使用bundler专门管理我的gem,如RubyRoguesepisode45中所述。.我并没有那么多合作,如果我这样做,通常是与另外一两个人合作。bundler文档详细说明了通过运行将gem打包到vendor/cache目录中的能力:$bundlepackage$bundleinstall--local伟大的私有(private)gem,可以checkin源代码控制;我想部署到一个干净的服务器或更容易协作?但是,如果您将Gemfile和Ge
我有一个自定义表单生成器,使用此自定义生成器的原因之一是对于每个表单,我都需要包含一些额外的参数,我不想在每个表单中使用隐藏字段标签显式放入这些参数写。for_for(@foo,:builder=>MyBuilder)do|f|#stuffIshouldn'thavetoworryabout#thisshouldbeputinallthetimewithoutmehavingtodoithidden_field_tag('extra','myextrainfo')#normalthingsIwouldputinf.text_field(:bar)end我必须在我的自定义表单构建器中做什