Skip to content

[RAD][HttpKernel] Create a MicroBundle base class #19596

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 129 additions & 0 deletions src/Symfony/Component/HttpKernel/Bundle/SimpleBundle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\HttpKernel\Bundle;

use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Loader\DelegatingLoader;
use Symfony\Component\Config\Loader\LoaderResolver;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\DependencyInjection\Loader\IniFileLoader;
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
use Symfony\Component\DependencyInjection\Loader\DirectoryLoader;
use Symfony\Component\DependencyInjection\Loader\ClosureLoader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

/**
* An implementation of a simple all-in-one {@link BundleInterface} and
* configurable {@link Extension}.
*
* @author Guilhem N. <egetick@gmail.com>
*/
abstract class SimpleBundle extends Bundle implements ConfigurationInterface
{
/**
* {@inheritdoc}
*/
final public function getContainerExtension()
{
if (null === $this->extension) {
$this->extension = new SimpleExtension($this);
}

return $this->extension;
}

/**
* {@inheritdoc}
*/
final public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root($this->getContainerExtension()->getAlias());

$this->buildConfiguration($rootNode);

return $treeBuilder;
}

protected function buildConfiguration(ArrayNodeDefinition $rootNode)
{
}

protected function load(array $config, ContainerBuilder $container, LoaderInterface $loader)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love it :D

{
}
}

/**
* Simple {@link Extension} supporting {@link SimpleBundle}.
*
* @internal
*/
final class SimpleExtension extends Extension
{
private $bundle;

public function __construct(SimpleBundle $bundle)
{
$this->bundle = $bundle;
}

/**
* {@inheritdoc}
*/
public function getAlias()
{
// check naming convention
$basename = preg_replace('/Bundle$/', '', $this->bundle->getName());

return Container::underscore($basename);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Container is not imported :)

Copy link
Contributor Author

@GuilhemN GuilhemN Oct 25, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should definitely add tests... But i'd like the symfony members' point of view before spending too much time on them.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understand, i already planned taking it (if you're ok with it ;-)) and it's not accepted (hence the review :)). Perhaps create a composer gist then? Anyway.. hope this gets excepted 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem, that's made to be reused ;)
I would prefer seing this merged in symfony, i think it loses most of its interest if it's not simple and quick to use.

}

/**
* {@inheritdoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$config = $this->processConfiguration($this->bundle, $configs);

// Loader
$locator = new FileLocator($this->bundle->getPath().'/Resources/config');
$resolver = new LoaderResolver(array(
new XmlFileLoader($container, $locator),
new YamlFileLoader($container, $locator),
new IniFileLoader($container, $locator),
new PhpFileLoader($container, $locator),
new DirectoryLoader($container, $locator),
new ClosureLoader($container),
));
$loader = new DelegatingLoader($resolver);

call_user_func(\Closure::bind(function () use ($config, $container, $loader) {
$this->load($config, $container, $loader);
}, $this->bundle, SimpleBundle::class));
}

/**
* {@inheritdoc}
*/
public function getConfiguration(array $config, ContainerBuilder $container)
{
return $this->bundle;
}
}