Skip to content

Commit 8f862b2

Browse files
committed
feature symfony#23812 [FrameworkBundle] Allow micro kernel to subscribe events easily (ogizanagi)
This PR was merged into the 3.4 branch. Discussion ---------- [FrameworkBundle] Allow micro kernel to subscribe events easily | Q | A | ------------- | --- | Branch? | 3.4 <!-- see comment below --> | Bug fix? | no | New feature? | yes <!-- don't forget updating src/**/CHANGELOG.md files --> | BC breaks? | no | Deprecations? | no <!-- don't forget updating UPGRADE-*.md files --> | Tests pass? | yes | Fixed tickets | symfony#16982 <!-- #-prefixed issue number(s), if any --> | License | MIT | Doc PR | N/A just by implementing `EventSubscriberInterface`. See related issue for other implementation suggestions. Commits ------- b31542e [FrameworkBundle] Allow micro kernel to subscribe events easily
2 parents 84fb318 + b31542e commit 8f862b2

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

src/Symfony/Bundle/FrameworkBundle/Kernel/MicroKernelTrait.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Config\Loader\LoaderInterface;
1515
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1617
use Symfony\Component\Routing\RouteCollectionBuilder;
1718

1819
/**
@@ -68,6 +69,13 @@ public function registerContainerConfiguration(LoaderInterface $loader)
6869
),
6970
));
7071

72+
if ($this instanceof EventSubscriberInterface) {
73+
$container->register('kernel', static::class)
74+
->setSynthetic(true)
75+
->addTag('kernel.event_subscriber')
76+
;
77+
}
78+
7179
$this->configureContainer($container, $loader);
7280

7381
$container->addObjectResource($this);

src/Symfony/Bundle/FrameworkBundle/Tests/Kernel/ConcreteMicroKernel.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,37 @@
1515
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
1616
use Symfony\Component\Config\Loader\LoaderInterface;
1717
use Symfony\Component\DependencyInjection\ContainerBuilder;
18+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1819
use Symfony\Component\Filesystem\Filesystem;
1920
use Symfony\Component\HttpFoundation\Response;
21+
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
2022
use Symfony\Component\HttpKernel\Kernel;
23+
use Symfony\Component\HttpKernel\KernelEvents;
2124
use Symfony\Component\Routing\RouteCollectionBuilder;
2225

23-
class ConcreteMicroKernel extends Kernel
26+
class ConcreteMicroKernel extends Kernel implements EventSubscriberInterface
2427
{
2528
use MicroKernelTrait;
2629

2730
private $cacheDir;
2831

32+
public function onKernelException(GetResponseForExceptionEvent $event)
33+
{
34+
if ($event->getException() instanceof Danger) {
35+
$event->setResponse(Response::create('It\'s dangerous to go alone. Take this ⚔'));
36+
}
37+
}
38+
2939
public function halloweenAction()
3040
{
3141
return new Response('halloween');
3242
}
3343

44+
public function dangerousAction()
45+
{
46+
throw new Danger();
47+
}
48+
3449
public function registerBundles()
3550
{
3651
return array(
@@ -57,6 +72,7 @@ public function __destruct()
5772
protected function configureRoutes(RouteCollectionBuilder $routes)
5873
{
5974
$routes->add('/', 'kernel:halloweenAction');
75+
$routes->add('/danger', 'kernel:dangerousAction');
6076
}
6177

6278
protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader)
@@ -68,4 +84,18 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load
6884
$c->setParameter('halloween', 'Have a great day!');
6985
$c->register('halloween', 'stdClass');
7086
}
87+
88+
/**
89+
* {@inheritdoc}
90+
*/
91+
public static function getSubscribedEvents()
92+
{
93+
return array(
94+
KernelEvents::EXCEPTION => 'onKernelException',
95+
);
96+
}
97+
}
98+
99+
class Danger extends \RuntimeException
100+
{
71101
}

src/Symfony/Bundle/FrameworkBundle/Tests/Kernel/MicroKernelTraitTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,15 @@ public function test()
2828
$this->assertEquals('Have a great day!', $kernel->getContainer()->getParameter('halloween'));
2929
$this->assertInstanceOf('stdClass', $kernel->getContainer()->get('halloween'));
3030
}
31+
32+
public function testAsEventSubscriber()
33+
{
34+
$kernel = new ConcreteMicroKernel('test', true);
35+
$kernel->boot();
36+
37+
$request = Request::create('/danger');
38+
$response = $kernel->handle($request);
39+
40+
$this->assertSame('It\'s dangerous to go alone. Take this ⚔', $response->getContent());
41+
}
3142
}

0 commit comments

Comments
 (0)