8Oct/100
Checking symfony plugin dependencies
When developing symfony projects, I try to modularize as many components as possible in plugins. This is a very flexible and reusable approach that definitely pays off in the long term.
Occasionally however, a plugin might depend on another, and if you do not check these dependencies properly, debugging the errors that follow from a missing dependency is not always straight-forward.
That's why I try to build in checks that warn me when I forgot to enable a dependency as so:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <?php class BarPluginConfiguration extends sfPluginConfiguration { public function getDependencies() { return array('FooPlugin'); } public function initialize() { // Check after factories are loaded, so the exception gets caught by symfony $this->dispatcher->connect('context.load_factories', array($this, 'checkDependencies')); } public function checkDependencies() { $plugins = $this->configuration->getPlugins(); foreach ($this->getDependencies() as $dependency) { if (!in_array($dependency, $plugins)) { throw new sfException(sprintf('The plugin "BarPlugin" requires "%s" to be enabled.', $dependency)); } } } } |