3

I'm developing a bundle who has a dependency on another one.

In order to handle the case that the base bundle has not been installed I'll like to perform a "bundle_exists()" function inside a controller.

The question is: How can I have a list of installed bundles or How can I check for the name (eventually also the version) of a bundle.

Thanks.

Carlos
  • 352
  • 1
  • 3
  • 13

4 Answers4

8

In addition to @Rooneyl's answer:

The best place to do such a check is inside your DI extension (e.g. AcmeDemoExtension). This is executed once the container is build and dumped to cache. There is no need to check such thing on each request (the container doesn't change while it's cached anyway), it'll only slow down your cache.

// ...
class AcmeDemoExtension extends Extension
{
    public function load(array $configs, ContainerBuilder $container)
    {
        $bundles = $container->getParameter('bundles');
        if (!isset($bundles['YourDependentBundle'])) {
            throw new \InvalidArgumentException(
                'The bundle ... needs to be registered in order to use AcmeDemoBundle.'
            );
        }
    }
}
Wouter J
  • 41,455
  • 15
  • 107
  • 112
6

Your class needs to have access to the container object (either by extending or DI).
Then you can do;

$this->container->getParameter('kernel.bundles');

This will give you a list of bundles installed.

Update;
If you are in a controller that extends the Symfony\Bundle\FrameworkBundle\Controller\Controller or in a command class that extends Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand, you can just get the parameter.

$this->getParameter('kernel.bundles').

Else @Wouter J's answer is your best answer.

Rooneyl
  • 7,802
  • 6
  • 52
  • 81
  • 2
    Extending sounds really scary and it's better to inject the parameter instead of injecting the complete container. – Wouter J Aug 11 '16 at 08:54
  • @WouterJ, fair point (I'll give you an upvote) - I'm just giving options. If this was a controller it would normally extend the main controller (unless set as a service), which in turn extends ContainerAware. – Rooneyl Aug 11 '16 at 09:23
1

You can get a list of all Bundles from the Kernel like this:

public function indexAction () 
{
    $arrBundles = $this->get("kernel")->getBundles();

    if (!array_key_exists("MyBundle", $arrBundles))
    {
        // bundle not found
    }

}
Veas
  • 160
  • 8
0

From Andrey at this question: How do I get a list of bundles in symfony2?

If you want to call a non static method of registered bundle object (not class) then you can do the following:

$kernel = $this->container->get('kernel');
$bundles = $kernel->getBundles();
$bundles['YourBundleName']->someMethod();

Where 'YourBundleName' is the name of your bundle, which you can get by calling from console:

php app/console config:dump-reference
Community
  • 1
  • 1
goto
  • 7,908
  • 10
  • 48
  • 58
  • Why do you copy/paste an answer from the original to a duplicated question? – gp_sflover Aug 11 '16 at 11:23
  • if the duplicated flag is not accepted the answer could be usefull. You think I shouldn't have? – goto Aug 11 '16 at 11:33
  • From the [Help center](http://stackoverflow.com/help/duplicates): "_The fundamental goal of closing duplicate questions is to help people find the right answer by getting all of those answers in one place_" and it's evidently that the question here is: _The question is: How can I have a list of installed bundles?_. I want to add that this question "_does not show any research effort_" because just search with "bundles list" in the symfony2 tag and it appear as first result. Maybe an expert user like @Wouter_J can give us a better explanation. – gp_sflover Aug 11 '16 at 11:51
  • 1
    I'm really sorry having duplicated a already answered topic. Surely my low knowledge about Symfony hasn't let me see that the answer was there. Sorry again. Once said this, I find some of the answers given more explanatory than the ones in the previous topic, as an example I can say that the Rooneyl's, Wouter J's and Veas gave me an overall idea. – Carlos Aug 11 '16 at 16:10
  • I will like to add that sometimes if you don't know how to ask something in a search leads you in a "does not show any research effort" position. Please, don't be so strict with the newbies. – Carlos Aug 11 '16 at 16:21