15
15
use Symfony \Bundle \FrameworkBundle \FrameworkBundle ;
16
16
use Symfony \Component \Config \Loader \LoaderInterface ;
17
17
use Symfony \Component \DependencyInjection \ContainerBuilder ;
18
+ use Symfony \Component \EventDispatcher \EventSubscriberInterface ;
18
19
use Symfony \Component \Filesystem \Filesystem ;
19
20
use Symfony \Component \HttpFoundation \Response ;
21
+ use Symfony \Component \HttpKernel \Event \GetResponseForExceptionEvent ;
20
22
use Symfony \Component \HttpKernel \Kernel ;
23
+ use Symfony \Component \HttpKernel \KernelEvents ;
21
24
use Symfony \Component \Routing \RouteCollectionBuilder ;
22
25
23
- class ConcreteMicroKernel extends Kernel
26
+ class ConcreteMicroKernel extends Kernel implements EventSubscriberInterface
24
27
{
25
28
use MicroKernelTrait;
26
29
27
30
private $ cacheDir ;
28
31
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
+
29
39
public function halloweenAction ()
30
40
{
31
41
return new Response ('halloween ' );
32
42
}
33
43
44
+ public function dangerousAction ()
45
+ {
46
+ throw new Danger ();
47
+ }
48
+
34
49
public function registerBundles ()
35
50
{
36
51
return array (
@@ -57,6 +72,7 @@ public function __destruct()
57
72
protected function configureRoutes (RouteCollectionBuilder $ routes )
58
73
{
59
74
$ routes ->add ('/ ' , 'kernel:halloweenAction ' );
75
+ $ routes ->add ('/danger ' , 'kernel:dangerousAction ' );
60
76
}
61
77
62
78
protected function configureContainer (ContainerBuilder $ c , LoaderInterface $ loader )
@@ -68,4 +84,18 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load
68
84
$ c ->setParameter ('halloween ' , 'Have a great day! ' );
69
85
$ c ->register ('halloween ' , 'stdClass ' );
70
86
}
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
+ {
71
101
}
0 commit comments