Skip to content

Commit

Permalink
feature #23812 [FrameworkBundle] Allow micro kernel to subscribe even…
Browse files Browse the repository at this point in the history
…ts 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 | #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
  • Loading branch information
fabpot committed Aug 9, 2017
2 parents 84fb318 + b31542e commit 8f862b2
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Routing\RouteCollectionBuilder;

/**
Expand Down Expand Up @@ -68,6 +69,13 @@ public function registerContainerConfiguration(LoaderInterface $loader)
),
));

if ($this instanceof EventSubscriberInterface) {
$container->register('kernel', static::class)
->setSynthetic(true)
->addTag('kernel.event_subscriber')
;
}

$this->configureContainer($container, $loader);

$container->addObjectResource($this);
Expand Down
Expand Up @@ -15,22 +15,37 @@
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Routing\RouteCollectionBuilder;

class ConcreteMicroKernel extends Kernel
class ConcreteMicroKernel extends Kernel implements EventSubscriberInterface
{
use MicroKernelTrait;

private $cacheDir;

public function onKernelException(GetResponseForExceptionEvent $event)
{
if ($event->getException() instanceof Danger) {
$event->setResponse(Response::create('It\'s dangerous to go alone. Take this ⚔'));
}
}

public function halloweenAction()
{
return new Response('halloween');
}

public function dangerousAction()
{
throw new Danger();
}

public function registerBundles()
{
return array(
Expand All @@ -57,6 +72,7 @@ public function __destruct()
protected function configureRoutes(RouteCollectionBuilder $routes)
{
$routes->add('/', 'kernel:halloweenAction');
$routes->add('/danger', 'kernel:dangerousAction');
}

protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader)
Expand All @@ -68,4 +84,18 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load
$c->setParameter('halloween', 'Have a great day!');
$c->register('halloween', 'stdClass');
}

/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return array(
KernelEvents::EXCEPTION => 'onKernelException',
);
}
}

class Danger extends \RuntimeException
{
}
Expand Up @@ -28,4 +28,15 @@ public function test()
$this->assertEquals('Have a great day!', $kernel->getContainer()->getParameter('halloween'));
$this->assertInstanceOf('stdClass', $kernel->getContainer()->get('halloween'));
}

public function testAsEventSubscriber()
{
$kernel = new ConcreteMicroKernel('test', true);
$kernel->boot();

$request = Request::create('/danger');
$response = $kernel->handle($request);

$this->assertSame('It\'s dangerous to go alone. Take this ⚔', $response->getContent());
}
}

0 comments on commit 8f862b2

Please sign in to comment.