1515use Symfony \Bundle \FrameworkBundle \FrameworkBundle ;
1616use Symfony \Component \Config \Loader \LoaderInterface ;
1717use Symfony \Component \DependencyInjection \ContainerBuilder ;
18+ use Symfony \Component \EventDispatcher \EventSubscriberInterface ;
1819use Symfony \Component \Filesystem \Filesystem ;
1920use Symfony \Component \HttpFoundation \Response ;
21+ use Symfony \Component \HttpKernel \Event \GetResponseForExceptionEvent ;
2022use Symfony \Component \HttpKernel \Kernel ;
23+ use Symfony \Component \HttpKernel \KernelEvents ;
2124use 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}
0 commit comments