Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[FrameworkBundle] Allow micro kernel to subscribe events easily #23812

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
{
}
Original file line number Diff line number Diff line change
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());
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.