Magnus Nordlander in Symfony 2 minute read

When you need bundle dependencies

It appears that my post on bundle dependencies has attracted attention, not only on Twitter, but Marc Morera has also responded in a post on his blog calles "You probably need bundle dependencies".

I have great respect for Marc, and I think that our positions on this matter are more similar than what it would appear at first glance. You see, I agree with almost everything Marc writes in his article. The example that he's using, one from FOSUserBundle, is a good example of when bundle dependencies are not only useful but also merited.

And it makes sense that this bundle has a dependency on SecurityBundle. After all, that is what it integrates into. (Almost?) all bundles have a dependency on FrameworkBundle, that sort of comes with the territory, and anything security related will tend to have a dependency on SecurityBundle, because, examining the purpose of a bundle (to integrate a library into Symfony), the salient part of Symfony that we're integrating into is the SecurityBundle.

Even in this case though, we could make this dependency optional, like so:

        encoder_factory_service: security.encoder_factory

We'd then handle this configation parameter in the bundle extension. We could also detect if SecurityBundle is installed, and in that case we could preconfigure this particular configuration node. Does it make sense, given the circumstances? No. Because SecurityBundle is a part of the standard edition, and (to my knowledge) the only bundle that integrates the Security component upon which FOSUserBundle has a very hard dependency, it is reasonable to declare it as a bundle dependency. (There is also the issue that FOSUserBundle could and should be split into a library and a service, but that, as one could say, is another show)

Consider however a bundle integrating a library with a dependency on Buzz (Let's call the library Widgetmotron and the bundle WidgetmotronBundle). There are several bundles integrating Buzz into the dependency injection container. Some add integration into the Web Profiler. If you're the author of WidgetmotronBundle, it does not make sense to depend on any one of the Buzz bundles, because you have no idea which one your users will be using, and it's just annoying for them if WidgetmotronBundle requires a particular Buzz bundle for no good reason.

So what do you do? You leave the Buzz browser service configurable, and let the user sort it out depending on which bundle he's using. If you're really nice to your users (and you probably should be), you could find the most popular Buzz bundles, detect them, and if the user hasn't configured that particular configuration node, you can configure it using the default for whatever Buzz bundle is installed.

What makes more sense depends on the situation. If hard bundle dependencies are what makes most sense, use mmoreram/symfony-bundle-dependencies, if not, keep it flexible.

To summarize:

Keep bundle dependencies optional when you can; use bundle dependencies when you must; don't confuse bundle dependencies with library dependencies.