feature #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 | #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
-------
b31542eb22
[FrameworkBundle] Allow micro kernel to subscribe events easily
This commit is contained in:
commit
8f862b2042
@ -13,6 +13,7 @@ namespace Symfony\Bundle\FrameworkBundle\Kernel;
|
||||
|
||||
use Symfony\Component\Config\Loader\LoaderInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
use Symfony\Component\Routing\RouteCollectionBuilder;
|
||||
|
||||
/**
|
||||
@ -68,6 +69,13 @@ trait MicroKernelTrait
|
||||
),
|
||||
));
|
||||
|
||||
if ($this instanceof EventSubscriberInterface) {
|
||||
$container->register('kernel', static::class)
|
||||
->setSynthetic(true)
|
||||
->addTag('kernel.event_subscriber')
|
||||
;
|
||||
}
|
||||
|
||||
$this->configureContainer($container, $loader);
|
||||
|
||||
$container->addObjectResource($this);
|
||||
|
@ -15,22 +15,37 @@ use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
|
||||
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(
|
||||
@ -57,6 +72,7 @@ class ConcreteMicroKernel extends Kernel
|
||||
protected function configureRoutes(RouteCollectionBuilder $routes)
|
||||
{
|
||||
$routes->add('/', 'kernel:halloweenAction');
|
||||
$routes->add('/danger', 'kernel:dangerousAction');
|
||||
}
|
||||
|
||||
protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader)
|
||||
@ -68,4 +84,18 @@ class ConcreteMicroKernel extends Kernel
|
||||
$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
|
||||
{
|
||||
}
|
||||
|
@ -28,4 +28,15 @@ class MicroKernelTraitTest extends TestCase
|
||||
$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());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user