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

Skip to content

Commit 5467231

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

File tree

4 files changed

+304
-0
lines changed

4 files changed

+304
-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: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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\DependencyInjection\Loader\ContainerBuilderAwareLoader;
19+
20+
/**
21+
* A Kernel that allows you to configure services.
22+
*
23+
* @author Ryan Weaver <[email protected]>
24+
*/
25+
abstract class MicroKernel extends Kernel
26+
{
27+
/**
28+
* Configure dependency injection extensions that have been added to the container.
29+
*
30+
* $c->loadFromExtension('framework', array(
31+
* 'secret' => '%secret%'
32+
* ));
33+
*
34+
* @param ContainerBuilder $c
35+
* @param LoaderInterface $loader
36+
*/
37+
abstract protected function configureExtensions(ContainerBuilder $c, LoaderInterface $loader);
38+
39+
/**
40+
* Add any service definitions to your container.
41+
*
42+
* @param ContainerBuilder $c
43+
* @param LoaderInterface $loader
44+
*/
45+
abstract protected function configureServices(ContainerBuilder $c, LoaderInterface $loader);
46+
47+
/**
48+
* Applies the bundle configuration and calls configureServices() for continued building.
49+
*
50+
* @param LoaderInterface $loader
51+
*/
52+
public function registerContainerConfiguration(LoaderInterface $loader)
53+
{
54+
if (!$loader instanceof ContainerBuilderAwareLoader) {
55+
throw new \LogicException('registerContainerConfiguration requires the LoaderInterface to be a ContainerBuilderAwareLoader.');
56+
}
57+
58+
$this->configureExtensions($loader->getContainerBuilder(), $loader->getResourceLoader());
59+
$this->configureServices($loader->getContainerBuilder(), $loader->getResourceLoader());
60+
}
61+
62+
/**
63+
* Returns a loader with the ContainerBuilder embedded inside of it.
64+
*
65+
* @param ContainerInterface $container
66+
*
67+
* @return ContainerBuilderAwareLoader
68+
*/
69+
protected function getContainerLoader(ContainerInterface $container)
70+
{
71+
if (!$container instanceof ContainerBuilder) {
72+
throw new \LogicException('Only ContainerBuilder instances are supported.');
73+
}
74+
75+
$loader = parent::getContainerLoader($container);
76+
77+
return new ContainerBuilderAwareLoader($container, $loader);
78+
}
79+
}
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: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
19+
class MicroKernelTest extends \PHPUnit_Framework_TestCase
20+
{
21+
public function testGetContainerLoader()
22+
{
23+
$containerBuilder = new ContainerBuilder();
24+
$kernel = new MicroKernelForTest('test', false);
25+
26+
$loader = $kernel->getContainerLoaderExternally($containerBuilder);
27+
$this->assertInstanceOf('Symfony\Component\DependencyInjection\Loader\ContainerBuilderAwareLoader', $loader);
28+
$this->assertSame($containerBuilder, $loader->getContainerBuilder());
29+
}
30+
31+
/**
32+
* @expectedException \LogicException
33+
*/
34+
public function testGetContainerLoaderFailsUnlessBuilder()
35+
{
36+
$containerBuilder = new Container();
37+
$kernel = new MicroKernelForTest('test', false);
38+
39+
$kernel->getContainerLoaderExternally($containerBuilder);
40+
}
41+
42+
/**
43+
* @expectedException \LogicException
44+
*/
45+
public function testRegisterContainerConfigurationOnlyAcceptsContainerAwareBuilderLoader()
46+
{
47+
$loader = $this->getMock('Symfony\Component\Config\Loader\LoaderInterface');
48+
$kernel = new MicroKernelForTest('test', false);
49+
$kernel->registerContainerConfiguration($loader);
50+
}
51+
52+
public function testRegisterContainerConfiguration()
53+
{
54+
$loader = $this->getContainerBuilderAwareLoader();
55+
$kernel = new MicroKernelForTest('test', false);
56+
$kernel->registerContainerConfiguration($loader);
57+
58+
$this->assertTrue($kernel->wasConfigureServicesCalled());
59+
$configureServicesArgs = $kernel->getConfigureServicesArguments();
60+
$this->assertSame($loader->getResourceLoader(), $configureServicesArgs[1], 'The original loader is sent to configureServices');
61+
}
62+
63+
private function getContainerBuilderAwareLoader()
64+
{
65+
$loader = $this->getMock('Symfony\Component\Config\Loader\LoaderInterface');
66+
$builder = $this->getMock('Symfony\Component\DependencyInjection\ContainerBuilder');
67+
68+
return new ContainerBuilderAwareLoader($builder, $loader);
69+
}
70+
}

0 commit comments

Comments
 (0)