Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2cc23d7

Browse files
committed
Adding a MicroKernel that has the special power where services (and extensions) can be configured direclty inside.
1 parent 4d275b4 commit 2cc23d7

File tree

4 files changed

+306
-0
lines changed

4 files changed

+306
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\DependencyInjection\Loader;
13+
14+
use Symfony\Component\Config\Loader\LoaderInterface;
15+
use Symfony\Component\Config\Loader\LoaderResolverInterface;
16+
use Symfony\Component\DependencyInjection\ContainerBuilder;
17+
18+
/**
19+
* Configuration loader that holds the ContainerBuilder inside.
20+
*
21+
* It can be useful to pass this to KernelInterface::registerContainerConfiguration()
22+
* instead of a normal Loader if that method will need the ContainerBuilder.
23+
*
24+
* @author Ryan Weaver <[email protected]>
25+
*/
26+
class ContainerBuilderAwareLoader implements LoaderInterface
27+
{
28+
/**
29+
* @var ContainerBuilder
30+
*/
31+
private $containerBuilder;
32+
33+
/**
34+
* @var LoaderInterface
35+
*/
36+
private $resourceLoader;
37+
38+
public function __construct(ContainerBuilder $builder, LoaderInterface $resourceLoader)
39+
{
40+
$this->containerBuilder = $builder;
41+
$this->resourceLoader = $resourceLoader;
42+
}
43+
44+
/**
45+
* @return ContainerBuilder
46+
*/
47+
public function getContainerBuilder()
48+
{
49+
return $this->containerBuilder;
50+
}
51+
52+
/**
53+
* @return LoaderInterface
54+
*/
55+
public function getResourceLoader()
56+
{
57+
return $this->resourceLoader;
58+
}
59+
60+
/**
61+
* @see {@inheritdoc}
62+
*/
63+
public function load($resource, $type = null)
64+
{
65+
return $this->resourceLoader->load($resource, $type);
66+
}
67+
68+
/**
69+
* @see {@inheritdoc}
70+
*/
71+
public function supports($resource, $type = null)
72+
{
73+
return $this->resourceLoader->supports($resource, $type);
74+
}
75+
76+
/**
77+
* @see {@inheritdoc}
78+
*/
79+
public function getResolver()
80+
{
81+
return $this->resourceLoader->getResolver();
82+
}
83+
84+
/**
85+
* @see {@inheritdoc}
86+
*/
87+
public function setResolver(LoaderResolverInterface $resolver)
88+
{
89+
return $this->resourceLoader->setResolver($resolver);
90+
}
91+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpKernel;
13+
14+
use Symfony\Component\Config\Loader\Loader;
15+
use Symfony\Component\Config\Loader\LoaderInterface;
16+
use Symfony\Component\DependencyInjection\ContainerBuilder;
17+
use Symfony\Component\DependencyInjection\ContainerInterface;
18+
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
19+
use Symfony\Component\DependencyInjection\Loader\ContainerBuilderAwareLoader;
20+
21+
/**
22+
* A Kernel that allows you to configure services.
23+
*
24+
* @author Ryan Weaver <[email protected]>
25+
*/
26+
abstract class MicroKernel extends Kernel
27+
{
28+
/**
29+
* Configure dependency injection extensions that have been added to the container.
30+
*
31+
* $c->loadFromExtension('framework', array(
32+
* 'secret' => '%secret%'
33+
* ));
34+
*
35+
* @param ContainerBuilder $c
36+
* @param LoaderInterface $loader
37+
*/
38+
abstract protected function configureExtensions(ContainerBuilder $c, LoaderInterface $loader);
39+
40+
/**
41+
* Add any service definitions to your container.
42+
*
43+
* @param ContainerBuilder $c
44+
* @param LoaderInterface $loader
45+
*/
46+
abstract protected function configureServices(ContainerBuilder $c, LoaderInterface $loader);
47+
48+
/**
49+
* Applies the bundle configuration and calls configureServices() for continued building.
50+
*
51+
* @param LoaderInterface $loader
52+
*/
53+
public function registerContainerConfiguration(LoaderInterface $loader)
54+
{
55+
if (!$loader instanceof ContainerBuilderAwareLoader) {
56+
throw new \LogicException('registerContainerConfiguration requires the LoaderInterface to be a ContainerBuilderAwareLoader.');
57+
}
58+
59+
$this->configureExtensions($loader->getContainerBuilder(), $loader->getResourceLoader());
60+
$this->configureServices($loader->getContainerBuilder(), $loader->getResourceLoader());
61+
}
62+
63+
/**
64+
* Returns a loader with the ContainerBuilder embedded inside of it.
65+
*
66+
* @param ContainerInterface $container
67+
*
68+
* @return ContainerBuilderAwareLoader
69+
*/
70+
protected function getContainerLoader(ContainerInterface $container)
71+
{
72+
if (!$container instanceof ContainerBuilder) {
73+
throw new \LogicException('Only ContainerBuilder instances are supported.');
74+
}
75+
76+
$loader = parent::getContainerLoader($container);
77+
78+
return new ContainerBuilderAwareLoader($container, $loader);
79+
}
80+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpKernel\Tests\Fixtures;
13+
14+
use Symfony\Component\HttpKernel\MicroKernel;
15+
use Symfony\Component\Config\Loader\LoaderInterface;
16+
use Symfony\Component\DependencyInjection\ContainerBuilder;
17+
use Symfony\Component\DependencyInjection\ContainerInterface;
18+
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
19+
20+
class MicroKernelForTest extends MicroKernel
21+
{
22+
private $configureServicesCalled = false;
23+
24+
private $configureExtensionsCalled = false;
25+
26+
private $configureServicesArgs = array();
27+
28+
public function registerBundles()
29+
{
30+
return array();
31+
}
32+
33+
public function getContainerLoaderExternally(ContainerInterface $container)
34+
{
35+
return $this->getContainerLoader($container);
36+
}
37+
38+
protected function configureExtensions(ContainerBuilder $c, LoaderInterface $loader)
39+
{
40+
$this->configureExtensionsCalled = true;
41+
}
42+
43+
protected function configureServices(ContainerBuilder $c, LoaderInterface $loader)
44+
{
45+
$this->configureServicesArgs = array($c, $loader);
46+
47+
$this->configureServicesCalled = true;
48+
}
49+
50+
public function wasConfigureServicesCalled()
51+
{
52+
return $this->configureServicesCalled;
53+
}
54+
55+
public function wasConfigureExtensionsCalled()
56+
{
57+
return $this->configureExtensionsCalled;
58+
}
59+
60+
public function getConfigureServicesArguments()
61+
{
62+
return $this->configureServicesArgs;
63+
}
64+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpKernel\Tests;
13+
14+
use Symfony\Component\DependencyInjection\Loader\ContainerBuilderAwareLoader;
15+
use Symfony\Component\HttpKernel\Tests\Fixtures\MicroKernelForTest;
16+
use Symfony\Component\DependencyInjection\ContainerBuilder;
17+
use Symfony\Component\DependencyInjection\Container;
18+
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
19+
20+
class MicroKernelTest extends \PHPUnit_Framework_TestCase
21+
{
22+
public function testGetContainerLoader()
23+
{
24+
$containerBuilder = new ContainerBuilder();
25+
$kernel = new MicroKernelForTest('test', false);
26+
27+
$loader = $kernel->getContainerLoaderExternally($containerBuilder);
28+
$this->assertInstanceOf('Symfony\Component\DependencyInjection\Loader\ContainerBuilderAwareLoader', $loader);
29+
$this->assertSame($containerBuilder, $loader->getContainerBuilder());
30+
}
31+
32+
/**
33+
* @expectedException \LogicException
34+
*/
35+
public function testGetContainerLoaderFailsUnlessBuilder()
36+
{
37+
$containerBuilder = new Container();
38+
$kernel = new MicroKernelForTest('test', false);
39+
40+
$kernel->getContainerLoaderExternally($containerBuilder);
41+
}
42+
43+
/**
44+
* @expectedException \LogicException
45+
*/
46+
public function testRegisterContainerConfigurationOnlyAcceptsContainerAwareBuilderLoader()
47+
{
48+
$loader = $this->getMock('Symfony\Component\Config\Loader\LoaderInterface');
49+
$kernel = new MicroKernelForTest('test', false);
50+
$kernel->registerContainerConfiguration($loader);
51+
}
52+
53+
public function testRegisterContainerConfiguration()
54+
{
55+
$loader = $this->getContainerBuilderAwareLoader();
56+
$kernel = new MicroKernelForTest('test', false);
57+
$kernel->registerContainerConfiguration($loader);
58+
59+
$this->assertTrue($kernel->wasConfigureServicesCalled());
60+
$configureServicesArgs = $kernel->getConfigureServicesArguments();
61+
$this->assertSame($loader->getResourceLoader(), $configureServicesArgs[1], 'The original loader is sent to configureServices');
62+
}
63+
64+
private function getContainerBuilderAwareLoader()
65+
{
66+
$loader = $this->getMock('Symfony\Component\Config\Loader\LoaderInterface');
67+
$builder = $this->getMock('Symfony\Component\DependencyInjection\ContainerBuilder');
68+
69+
return new ContainerBuilderAwareLoader($builder, $loader);
70+
}
71+
}

0 commit comments

Comments
 (0)