12
12
namespace Symfony \Component \HttpKernel \EventListener ;
13
13
14
14
use Psr \Log \LoggerInterface ;
15
+ use Symfony \Component \HttpFoundation \Response ;
15
16
use Symfony \Component \HttpKernel \Event \GetResponseEvent ;
16
17
use Symfony \Component \HttpKernel \Event \FinishRequestEvent ;
18
+ use Symfony \Component \HttpKernel \Kernel ;
17
19
use Symfony \Component \HttpKernel \KernelEvents ;
18
20
use Symfony \Component \HttpKernel \Exception \MethodNotAllowedHttpException ;
19
21
use Symfony \Component \HttpKernel \Exception \NotFoundHttpException ;
20
22
use Symfony \Component \HttpFoundation \RequestStack ;
21
23
use Symfony \Component \Routing \Exception \MethodNotAllowedException ;
24
+ use Symfony \Component \Routing \Exception \NoConfigurationException ;
22
25
use Symfony \Component \Routing \Exception \ResourceNotFoundException ;
23
26
use Symfony \Component \Routing \Matcher \UrlMatcherInterface ;
24
27
use Symfony \Component \Routing \Matcher \RequestMatcherInterface ;
31
34
* Initializes the context from the request and sets request attributes based on a matching route.
32
35
*
33
36
* @author Fabien Potencier <[email protected] >
37
+ * @author Yonel Ceruto <[email protected] >
34
38
*/
35
39
class RouterListener implements EventSubscriberInterface
36
40
{
37
41
private $ matcher ;
38
42
private $ context ;
39
43
private $ logger ;
40
44
private $ requestStack ;
45
+ private $ projectDir ;
46
+ private $ debug ;
41
47
42
48
/**
43
49
* @param UrlMatcherInterface|RequestMatcherInterface $matcher The Url or Request matcher
44
50
* @param RequestStack $requestStack A RequestStack instance
45
51
* @param RequestContext|null $context The RequestContext (can be null when $matcher implements RequestContextAwareInterface)
46
52
* @param LoggerInterface|null $logger The logger
53
+ * @param string $projectDir
54
+ * @param bool $debug
47
55
*
48
56
* @throws \InvalidArgumentException
49
57
*/
50
- public function __construct ($ matcher , RequestStack $ requestStack , RequestContext $ context = null , LoggerInterface $ logger = null )
58
+ public function __construct ($ matcher , RequestStack $ requestStack , RequestContext $ context = null , LoggerInterface $ logger = null , $ projectDir = null , $ debug = true )
51
59
{
52
60
if (!$ matcher instanceof UrlMatcherInterface && !$ matcher instanceof RequestMatcherInterface) {
53
61
throw new \InvalidArgumentException ('Matcher must either implement UrlMatcherInterface or RequestMatcherInterface. ' );
@@ -61,6 +69,8 @@ public function __construct($matcher, RequestStack $requestStack, RequestContext
61
69
$ this ->context = $ context ?: $ matcher ->getContext ();
62
70
$ this ->requestStack = $ requestStack ;
63
71
$ this ->logger = $ logger ;
72
+ $ this ->projectDir = $ projectDir ;
73
+ $ this ->debug = $ debug ;
64
74
}
65
75
66
76
private function setCurrentRequest (Request $ request = null )
@@ -114,6 +124,12 @@ public function onKernelRequest(GetResponseEvent $event)
114
124
unset($ parameters ['_route ' ], $ parameters ['_controller ' ]);
115
125
$ request ->attributes ->set ('_route_params ' , $ parameters );
116
126
} catch (ResourceNotFoundException $ e ) {
127
+ if ($ this ->debug && $ e instanceof NoConfigurationException) {
128
+ $ event ->setResponse ($ this ->createWelcomeResponse ());
129
+
130
+ return ;
131
+ }
132
+
117
133
$ message = sprintf ('No route found for "%s %s" ' , $ request ->getMethod (), $ request ->getPathInfo ());
118
134
119
135
if ($ referer = $ request ->headers ->get ('referer ' )) {
@@ -135,4 +151,16 @@ public static function getSubscribedEvents()
135
151
KernelEvents::FINISH_REQUEST => array (array ('onKernelFinishRequest ' , 0 )),
136
152
);
137
153
}
154
+
155
+ private function createWelcomeResponse ()
156
+ {
157
+ $ version = Kernel::VERSION ;
158
+ $ baseDir = realpath ($ this ->projectDir ).DIRECTORY_SEPARATOR ;
159
+ $ docVersion = substr (Kernel::VERSION , 0 , 3 );
160
+
161
+ ob_start ();
162
+ include __DIR__ .'/../Resources/welcome.html.php ' ;
163
+
164
+ return new Response (ob_get_clean (), Response::HTTP_NOT_FOUND );
165
+ }
138
166
}
0 commit comments