依赖Symfony2

时间:2011-05-31 08:25:49

标签: business-logic symfony

我想知道是否有正确的方法来检查依赖项。

例如,我有一个NewsBundle。现在我必须检查是否有CommentBundle。如果有的话,它应该再执行一些代码。

有什么建议吗?

3 个答案:

答案 0 :(得分:28)

除了markymark的答案,您还可以使用以下代码段检查控制器(或任何其他容器感知代码)中是否存在特定的服务

if ($this->container->has('foo_service.alias'))
{
    // service is loaded and usable
}

如果您不确定给定服务的确切别名,或仅仅是为了踢和傻笑,您可以运行控制台命令php app/console container:debug以查看向容器注册的所有服务。

答案 1 :(得分:2)

你可以在每个bundle应该拥有的主Bundle类上使用class_exists。

例如:

if (class_exists('Acme\CommentBundle\AcmeCommentBundle'))
{
    // Bundle exists and is loaded by AppKernel...
}

答案 2 :(得分:0)

Kernel类包含一个辅助方法列表,用于检查某个类是否是活动包的一部分,或者是否已注册包。

public BundleInterface[] getBundles()
    Gets the registered bundle instances.

public bool isClassInActiveBundle(string $class)
    Checks if a given class name belongs to an active bundle.
相关问题