Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fd4b63a

Browse files
committed
Move logic to RouterListener
1 parent 7d6e41e commit fd4b63a

File tree

11 files changed

+144
-219
lines changed

11 files changed

+144
-219
lines changed

src/Symfony/Bundle/FrameworkBundle/EventListener/NoRoutingConfigurationSubscriber.php

Lines changed: 0 additions & 163 deletions
This file was deleted.

src/Symfony/Bundle/FrameworkBundle/Resources/config/debug.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,5 @@
2424
<argument type="service" id="debug.argument_resolver.inner" />
2525
<argument type="service" id="debug.stopwatch" />
2626
</service>
27-
28-
<service id="debug.no_routing_configuration_subscriber" class="Symfony\Bundle\FrameworkBundle\EventListener\NoRoutingConfigurationSubscriber">
29-
<argument>%kernel.project_dir%</argument>
30-
<tag name="kernel.event_subscriber" />
31-
</service>
3227
</services>
3328
</container>

src/Symfony/Bundle/FrameworkBundle/Resources/config/routing.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@
109109
<argument type="service" id="request_stack" />
110110
<argument type="service" id="router.request_context" on-invalid="ignore" />
111111
<argument type="service" id="logger" on-invalid="ignore" />
112+
<argument>%kernel.project_dir%</argument>
113+
<argument>%kernel.debug%</argument>
112114
</service>
113115
</services>
114116
</container>

src/Symfony/Bundle/FrameworkBundle/Tests/EventListener/NoRoutingConfigurationSubscriberTest.php

Lines changed: 0 additions & 49 deletions
This file was deleted.

src/Symfony/Component/HttpKernel/EventListener/RouterListener.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@
1212
namespace Symfony\Component\HttpKernel\EventListener;
1313

1414
use Psr\Log\LoggerInterface;
15+
use Symfony\Component\HttpFoundation\Response;
1516
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
1617
use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
18+
use Symfony\Component\HttpKernel\Kernel;
1719
use Symfony\Component\HttpKernel\KernelEvents;
1820
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
1921
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
2022
use Symfony\Component\HttpFoundation\RequestStack;
2123
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
24+
use Symfony\Component\Routing\Exception\NoConfigurationException;
2225
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
2326
use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
2427
use Symfony\Component\Routing\Matcher\RequestMatcherInterface;
@@ -31,23 +34,28 @@
3134
* Initializes the context from the request and sets request attributes based on a matching route.
3235
*
3336
* @author Fabien Potencier <[email protected]>
37+
* @author Yonel Ceruto <[email protected]>
3438
*/
3539
class RouterListener implements EventSubscriberInterface
3640
{
3741
private $matcher;
3842
private $context;
3943
private $logger;
4044
private $requestStack;
45+
private $projectDir;
46+
private $debug;
4147

4248
/**
4349
* @param UrlMatcherInterface|RequestMatcherInterface $matcher The Url or Request matcher
4450
* @param RequestStack $requestStack A RequestStack instance
4551
* @param RequestContext|null $context The RequestContext (can be null when $matcher implements RequestContextAwareInterface)
4652
* @param LoggerInterface|null $logger The logger
53+
* @param string $projectDir
54+
* @param bool $debug
4755
*
4856
* @throws \InvalidArgumentException
4957
*/
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)
5159
{
5260
if (!$matcher instanceof UrlMatcherInterface && !$matcher instanceof RequestMatcherInterface) {
5361
throw new \InvalidArgumentException('Matcher must either implement UrlMatcherInterface or RequestMatcherInterface.');
@@ -61,6 +69,8 @@ public function __construct($matcher, RequestStack $requestStack, RequestContext
6169
$this->context = $context ?: $matcher->getContext();
6270
$this->requestStack = $requestStack;
6371
$this->logger = $logger;
72+
$this->projectDir = $projectDir;
73+
$this->debug = $debug;
6474
}
6575

6676
private function setCurrentRequest(Request $request = null)
@@ -114,6 +124,12 @@ public function onKernelRequest(GetResponseEvent $event)
114124
unset($parameters['_route'], $parameters['_controller']);
115125
$request->attributes->set('_route_params', $parameters);
116126
} catch (ResourceNotFoundException $e) {
127+
if ($this->debug && $e instanceof NoConfigurationException) {
128+
$event->setResponse($this->createWelcomeResponse());
129+
130+
return;
131+
}
132+
117133
$message = sprintf('No route found for "%s %s"', $request->getMethod(), $request->getPathInfo());
118134

119135
if ($referer = $request->headers->get('referer')) {
@@ -135,4 +151,16 @@ public static function getSubscribedEvents()
135151
KernelEvents::FINISH_REQUEST => array(array('onKernelFinishRequest', 0)),
136152
);
137153
}
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+
}
138166
}

0 commit comments

Comments
 (0)