Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
added the concept of a main DIC extension for bundles
This allows for better conventions and better error messages if you
use the wrong configuration alias in a config file.

This is also the first step for a bigger refactoring of how the configuration
works (see next commits).

 * Bundle::registerExtensions() method has been renamed to Bundle::build()

 * The "main" DIC extension must be renamed to the new convention to be
   automatically registered:

      SensioBlogBundle -> DependencyInjection\SensioBlogExtension

 * The main DIC extension alias must follow the convention:

      sensio_blog for SensioBlogBundle

 * If you have more than one extension for a bundle (which should really
   never be the case), they must be registered manually by overriding the
   build() method

 * If you use YAML or PHP for your configuration, renamed the following
   configuration entry points in your configs:

      app -> framework
      webprofiler -> web_profiler
      doctrine_odm -> doctrine_mongo_db
  • Loading branch information
fabpot committed Feb 15, 2011
1 parent 391e00c commit 14aa95b
Show file tree
Hide file tree
Showing 15 changed files with 78 additions and 60 deletions.
4 changes: 2 additions & 2 deletions src/Symfony/Bundle/AsseticBundle/AsseticBundle.php
Expand Up @@ -24,9 +24,9 @@
*/
class AsseticBundle extends Bundle
{
public function registerExtensions(ContainerBuilder $container)
public function build(ContainerBuilder $container)
{
parent::registerExtensions($container);
parent::build($container);

$container->addCompilerPass(new AssetManagerPass());
$container->addCompilerPass(new FilterManagerPass());
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Bundle/DoctrineBundle/DoctrineBundle.php
Expand Up @@ -25,9 +25,9 @@
*/
class DoctrineBundle extends Bundle
{
public function registerExtensions(ContainerBuilder $container)
public function build(ContainerBuilder $container)
{
parent::registerExtensions($container);
parent::build($container);

$container->addCompilerPass(new RegisterEventListenersAndSubscribersPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION);
}
Expand Down
Expand Up @@ -402,6 +402,6 @@ public function getXsdValidationBasePath()
*/
public function getAlias()
{
return 'doctrine_odm';
return 'doctrine_mongo_db';
}
}
Expand Up @@ -27,9 +27,9 @@
*/
class DoctrineMongoDBBundle extends Bundle
{
public function registerExtensions(ContainerBuilder $container)
public function build(ContainerBuilder $container)
{
parent::registerExtensions($container);
parent::build($container);

$container->addCompilerPass(new RegisterEventListenersAndSubscribersPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION);
$container->addCompilerPass(new CreateProxyDirectoryPass(), PassConfig::TYPE_BEFORE_REMOVING);
Expand Down
Expand Up @@ -506,6 +506,6 @@ public function getNamespace()

public function getAlias()
{
return 'app';
return 'framework';
}
}
4 changes: 2 additions & 2 deletions src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php
Expand Up @@ -64,9 +64,9 @@ public function boot()
}
}

public function registerExtensions(ContainerBuilder $container)
public function build(ContainerBuilder $container)
{
parent::registerExtensions($container);
parent::build($container);

$container->addScope(new Scope('request'));

Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Bundle/SecurityBundle/SecurityBundle.php
Expand Up @@ -22,9 +22,9 @@
*/
class SecurityBundle extends Bundle
{
public function registerExtensions(ContainerBuilder $container)
public function build(ContainerBuilder $container)
{
parent::registerExtensions($container);
parent::build($container);

$container->addCompilerPass(new AddSecurityVotersPass());
}
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Bundle/TwigBundle/TwigBundle.php
Expand Up @@ -22,9 +22,9 @@
*/
class TwigBundle extends Bundle
{
public function registerExtensions(ContainerBuilder $container)
public function build(ContainerBuilder $container)
{
parent::registerExtensions($container);
parent::build($container);

$container->addCompilerPass(new TwigEnvironmentPass());
}
Expand Down
Expand Up @@ -85,6 +85,6 @@ public function getNamespace()

public function getAlias()
{
return 'webprofiler';
return 'web_profiler';
}
}
4 changes: 2 additions & 2 deletions src/Symfony/Bundle/ZendBundle/ZendBundle.php
Expand Up @@ -22,9 +22,9 @@
*/
class ZendBundle extends Bundle
{
public function registerExtensions(ContainerBuilder $container)
public function build(ContainerBuilder $container)
{
parent::registerExtensions($container);
parent::build($container);

$container->addCompilerPass(new ZendLoggerWriterPass());
}
Expand Down
59 changes: 32 additions & 27 deletions src/Symfony/Component/HttpKernel/Bundle/Bundle.php
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\DependencyInjection\ContainerAware;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\Console\Application;
use Symfony\Component\Finder\Finder;

Expand Down Expand Up @@ -41,6 +42,37 @@ public function shutdown()
{
}

/**
* Builds the bundle.
*
* It is only ever called once when the cache is empty.
*
* The default implementation automatically registers a DIC extension
* if its name is the same as the bundle name after replacing the
* Bundle suffix by Extension (DependencyInjection\SensioBlogExtension
* for a SensioBlogBundle for instance). In such a case, the alias
* is forced to be the underscore version of the bundle name
* (sensio_blog for a SensioBlogBundle for instance).
*
* This method can be overriden to register compilation passes,
* other extensions, ...
*
* @param ContainerBuilder $container A ContainerBuilder instance
*/
public function build(ContainerBuilder $container)
{
$class = $this->getNamespace().'\\DependencyInjection\\'.str_replace('Bundle', 'Extension', $this->getName());
if (class_exists($class)) {
$extension = new $class();
$alias = Container::underscore(str_replace('Bundle', '', $this->getName()));
if ($alias !== $extension->getAlias()) {
throw new \LogicException(sprintf('The extension alias for the default extension of a bundle must be the underscored version of the bundle name ("%s" vs "%s")', $alias, $extension->getAlias()));
}

$container->registerExtension($extension);
}
}

/**
* Gets the Bundle namespace.
*
Expand Down Expand Up @@ -96,33 +128,6 @@ final public function getName()
return $this->name = false === $pos ? $name : substr($name, $pos + 1);
}

/**
* Finds and registers Dependency Injection Container extensions.
*
* Override this method if your DIC extensions do not follow the conventions:
*
* * Extensions are in the 'DependencyInjection/' sub-directory
* * Extension class names ends with 'Extension'
*
* @param ContainerBuilder $container A ContainerBuilder instance
*/
public function registerExtensions(ContainerBuilder $container)
{
if (!$dir = realpath($this->getPath().'/DependencyInjection')) {
return;
}

$finder = new Finder();
$finder->files()->name('*Extension.php')->in($dir);

$prefix = $this->getNamespace().'\\DependencyInjection';
foreach ($finder as $file) {
$class = $prefix.strtr($file->getPath(), array($dir => '', '/' => '\\')).'\\'.$file->getBasename('.php');

$container->registerExtension(new $class());
}
}

/**
* Finds and registers Commands.
*
Expand Down
11 changes: 11 additions & 0 deletions src/Symfony/Component/HttpKernel/Bundle/BundleInterface.php
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\HttpKernel\Bundle;

use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* BundleInterface.
*
Expand All @@ -28,6 +30,15 @@ function boot();
*/
function shutdown();

/**
* Builds the bundle.
*
* It is only ever called once when the cache is empty.
*
* @param ContainerBuilder $container A ContainerBuilder instance
*/
public function build(ContainerBuilder $container);

/**
* Returns the bundle parent name.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/HttpKernel/Kernel.php
Expand Up @@ -458,7 +458,7 @@ protected function buildContainer()

$container = new ContainerBuilder($parameterBag);
foreach ($this->bundles as $bundle) {
$bundle->registerExtensions($container);
$bundle->build($container);

if ($this->debug) {
$container->addObjectResource($bundle);
Expand Down
30 changes: 16 additions & 14 deletions src/Symfony/Component/HttpKernel/bootstrap.php
Expand Up @@ -223,10 +223,12 @@ public function setContainer(ContainerInterface $container = null)
}
namespace Symfony\Component\HttpKernel\Bundle
{
use Symfony\Component\DependencyInjection\ContainerBuilder;
interface BundleInterface
{
function boot();
function shutdown();
public function build(ContainerBuilder $container);
function getParent();
function getName();
function getNamespace();
Expand All @@ -237,6 +239,7 @@ function getPath();
{
use Symfony\Component\DependencyInjection\ContainerAware;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\Console\Application;
use Symfony\Component\Finder\Finder;
abstract class Bundle extends ContainerAware implements BundleInterface
Expand All @@ -249,6 +252,18 @@ public function boot()
public function shutdown()
{
}
public function build(ContainerBuilder $container)
{
$class = $this->getNamespace().'\\DependencyInjection\\'.str_replace('Bundle', 'Extension', $this->getName());
if (class_exists($class)) {
$extension = new $class();
$alias = Container::underscore(str_replace('Bundle', '', $this->getName()));
if ($alias !== $extension->getAlias()) {
throw new \LogicException(sprintf('The extension alias for the default extension of a bundle must be the underscored version of the bundle name ("%s" vs "%s")', $alias, $extension->getAlias()));
}
$container->registerExtension($extension);
}
}
public function getNamespace()
{
if (null === $this->reflected) {
Expand Down Expand Up @@ -276,19 +291,6 @@ final public function getName()
$pos = strrpos($name, '\\');
return $this->name = false === $pos ? $name : substr($name, $pos + 1);
}
public function registerExtensions(ContainerBuilder $container)
{
if (!$dir = realpath($this->getPath().'/DependencyInjection')) {
return;
}
$finder = new Finder();
$finder->files()->name('*Extension.php')->in($dir);
$prefix = $this->getNamespace().'\\DependencyInjection';
foreach ($finder as $file) {
$class = $prefix.strtr($file->getPath(), array($dir => '', '/' => '\\')).'\\'.$file->getBasename('.php');
$container->registerExtension(new $class());
}
}
public function registerCommands(Application $application)
{
if (!$dir = realpath($this->getPath().'/Command')) {
Expand Down Expand Up @@ -727,7 +729,7 @@ protected function buildContainer()
$parameterBag = new ParameterBag($this->getKernelParameters());
$container = new ContainerBuilder($parameterBag);
foreach ($this->bundles as $bundle) {
$bundle->registerExtensions($container);
$bundle->build($container);
if ($this->debug) {
$container->addObjectResource($bundle);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/HttpKernel/bootstrap_cache.php
Expand Up @@ -289,7 +289,7 @@ protected function buildContainer()
$parameterBag = new ParameterBag($this->getKernelParameters());
$container = new ContainerBuilder($parameterBag);
foreach ($this->bundles as $bundle) {
$bundle->registerExtensions($container);
$bundle->build($container);
if ($this->debug) {
$container->addObjectResource($bundle);
}
Expand Down

0 comments on commit 14aa95b

Please sign in to comment.