Skip to content

Commit 9b3224b

Browse files
committed
feature #15990 added a micro kernel (fabpot)
This PR was merged into the 2.8 branch. Discussion ---------- added a micro kernel | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | n/a Related to #15948 and #15820 Commits ------- eab0f0a added a micro kernel
2 parents b587fa3 + eab0f0a commit 9b3224b

File tree

3 files changed

+191
-0
lines changed

3 files changed

+191
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\Bundle\FrameworkBundle\Kernel;
13+
14+
use Symfony\Component\Config\Loader\LoaderInterface;
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\Routing\RouteCollectionBuilder;
17+
18+
/**
19+
* A Kernel that provides configuration hooks.
20+
*
21+
* @author Ryan Weaver <ryan@knpuniversity.com>
22+
* @author Fabien Potencier <fabien@symfony.com>
23+
*/
24+
trait MicroKernelTrait
25+
{
26+
/**
27+
* Add or import routes into your application.
28+
*
29+
* $routes->import('config/routing.yml');
30+
* $routes->add('/admin', 'AppBundle:Admin:dashboard', 'admin_dashboard');
31+
*
32+
* @param RouteCollectionBuilder $routes
33+
*/
34+
abstract protected function configureRoutes(RouteCollectionBuilder $routes);
35+
36+
/**
37+
* Configures the container.
38+
*
39+
* You can register extensions:
40+
*
41+
* $c->loadFromExtension('framework', array(
42+
* 'secret' => '%secret%'
43+
* ));
44+
*
45+
* Or services:
46+
*
47+
* $c->register('halloween', 'FooBundle\HalloweenProvider');
48+
*
49+
* Or parameters:
50+
*
51+
* $c->setParameter('halloween', 'lot of fun');
52+
*
53+
* @param ContainerBuilder $c
54+
* @param LoaderInterface $loader
55+
*/
56+
abstract protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader);
57+
58+
/**
59+
* {@inheritdoc}
60+
*/
61+
public function registerContainerConfiguration(LoaderInterface $loader)
62+
{
63+
$loader->load(function (ContainerBuilder $container) use ($loader) {
64+
$container->loadFromExtension('framework', array(
65+
'router' => array(
66+
'resource' => 'kernel:loadRoutes',
67+
'type' => 'service',
68+
),
69+
));
70+
71+
$this->configureContainer($container, $loader);
72+
73+
$container->addObjectResource($this);
74+
});
75+
}
76+
77+
/**
78+
* @internal
79+
*/
80+
public function loadRoutes(LoaderInterface $loader)
81+
{
82+
$routes = new RouteCollectionBuilder($loader);
83+
$this->configureRoutes($routes);
84+
85+
return $routes->build();
86+
}
87+
}
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 <fabien@symfony.com>
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\Bundle\FrameworkBundle\Tests\Kernel;
13+
14+
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
15+
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
16+
use Symfony\Component\Config\Loader\LoaderInterface;
17+
use Symfony\Component\DependencyInjection\ContainerBuilder;
18+
use Symfony\Component\Filesystem\Filesystem;
19+
use Symfony\Component\HttpFoundation\Response;
20+
use Symfony\Component\HttpKernel\Kernel;
21+
use Symfony\Component\Routing\RouteCollectionBuilder;
22+
23+
class ConcreteMicroKernel extends Kernel
24+
{
25+
use MicroKernelTrait;
26+
27+
private $cacheDir;
28+
29+
public function halloweenAction()
30+
{
31+
return new Response('halloween');
32+
}
33+
34+
public function registerBundles()
35+
{
36+
return array(
37+
new FrameworkBundle(),
38+
);
39+
}
40+
41+
public function getCacheDir()
42+
{
43+
return $this->cacheDir = sys_get_temp_dir().'/sf_micro_kernel';
44+
}
45+
46+
public function getLogDir()
47+
{
48+
return $this->cacheDir;
49+
}
50+
51+
public function __destruct()
52+
{
53+
$fs = new Filesystem();
54+
$fs->remove($this->cacheDir);
55+
}
56+
57+
protected function configureRoutes(RouteCollectionBuilder $routes)
58+
{
59+
$routes->add('/', 'kernel:halloweenAction');
60+
}
61+
62+
protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader)
63+
{
64+
$c->loadFromExtension('framework', array(
65+
'secret' => '$ecret',
66+
));
67+
68+
$c->setParameter('halloween', 'Have a great day!');
69+
$c->register('halloween', 'stdClass');
70+
}
71+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\Bundle\FrameworkBundle\Tests\Kernel;
13+
14+
use Symfony\Component\HttpFoundation\Request;
15+
16+
class MicroKernelTraitTest extends \PHPUnit_Framework_TestCase
17+
{
18+
/**
19+
* @requires PHP 5.4
20+
*/
21+
public function test()
22+
{
23+
$kernel = new ConcreteMicroKernel('test', true);
24+
$kernel->boot();
25+
26+
$request = Request::create('/');
27+
$response = $kernel->handle($request);
28+
29+
$this->assertEquals('halloween', $response->getContent());
30+
$this->assertEquals('Have a great day!', $kernel->getContainer()->getParameter('halloween'));
31+
$this->assertInstanceOf('stdClass', $kernel->getContainer()->get('halloween'));
32+
}
33+
}

0 commit comments

Comments
 (0)