Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / symfony / http-kernel / EventListener / RouterListener.php
index 3c46be860810f3ec2012491c73dbde97680ac464..caaf80f86b8920a5afe7313ef77d46321ec1fcc8 100644 (file)
 namespace Symfony\Component\HttpKernel\EventListener;
 
 use Psr\Log\LoggerInterface;
+use Symfony\Component\HttpFoundation\Response;
 use Symfony\Component\HttpKernel\Event\GetResponseEvent;
 use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
+use Symfony\Component\HttpKernel\Kernel;
 use Symfony\Component\HttpKernel\KernelEvents;
+use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
 use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
 use Symfony\Component\HttpFoundation\RequestStack;
 use Symfony\Component\Routing\Exception\MethodNotAllowedException;
+use Symfony\Component\Routing\Exception\NoConfigurationException;
 use Symfony\Component\Routing\Exception\ResourceNotFoundException;
 use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
 use Symfony\Component\Routing\Matcher\RequestMatcherInterface;
@@ -31,6 +35,7 @@ use Symfony\Component\HttpFoundation\Request;
  * Initializes the context from the request and sets request attributes based on a matching route.
  *
  * @author Fabien Potencier <fabien@symfony.com>
+ * @author Yonel Ceruto <yonelceruto@gmail.com>
  */
 class RouterListener implements EventSubscriberInterface
 {
@@ -38,18 +43,20 @@ class RouterListener implements EventSubscriberInterface
     private $context;
     private $logger;
     private $requestStack;
+    private $projectDir;
+    private $debug;
 
     /**
-     * Constructor.
-     *
      * @param UrlMatcherInterface|RequestMatcherInterface $matcher      The Url or Request matcher
      * @param RequestStack                                $requestStack A RequestStack instance
      * @param RequestContext|null                         $context      The RequestContext (can be null when $matcher implements RequestContextAwareInterface)
      * @param LoggerInterface|null                        $logger       The logger
+     * @param string                                      $projectDir
+     * @param bool                                        $debug
      *
      * @throws \InvalidArgumentException
      */
-    public function __construct($matcher, RequestStack $requestStack, RequestContext $context = null, LoggerInterface $logger = null)
+    public function __construct($matcher, RequestStack $requestStack, RequestContext $context = null, LoggerInterface $logger = null, $projectDir = null, $debug = true)
     {
         if (!$matcher instanceof UrlMatcherInterface && !$matcher instanceof RequestMatcherInterface) {
             throw new \InvalidArgumentException('Matcher must either implement UrlMatcherInterface or RequestMatcherInterface.');
@@ -63,12 +70,18 @@ class RouterListener implements EventSubscriberInterface
         $this->context = $context ?: $matcher->getContext();
         $this->requestStack = $requestStack;
         $this->logger = $logger;
+        $this->projectDir = $projectDir;
+        $this->debug = $debug;
     }
 
     private function setCurrentRequest(Request $request = null)
     {
         if (null !== $request) {
-            $this->context->fromRequest($request);
+            try {
+                $this->context->fromRequest($request);
+            } catch (\UnexpectedValueException $e) {
+                throw new BadRequestHttpException($e->getMessage(), $e, $e->getCode());
+            }
         }
     }
 
@@ -116,6 +129,12 @@ class RouterListener implements EventSubscriberInterface
             unset($parameters['_route'], $parameters['_controller']);
             $request->attributes->set('_route_params', $parameters);
         } catch (ResourceNotFoundException $e) {
+            if ($this->debug && $e instanceof NoConfigurationException) {
+                $event->setResponse($this->createWelcomeResponse());
+
+                return;
+            }
+
             $message = sprintf('No route found for "%s %s"', $request->getMethod(), $request->getPathInfo());
 
             if ($referer = $request->headers->get('referer')) {
@@ -137,4 +156,16 @@ class RouterListener implements EventSubscriberInterface
             KernelEvents::FINISH_REQUEST => array(array('onKernelFinishRequest', 0)),
         );
     }
+
+    private function createWelcomeResponse()
+    {
+        $version = Kernel::VERSION;
+        $baseDir = realpath($this->projectDir).DIRECTORY_SEPARATOR;
+        $docVersion = substr(Kernel::VERSION, 0, 3);
+
+        ob_start();
+        include __DIR__.'/../Resources/welcome.html.php';
+
+        return new Response(ob_get_clean(), Response::HTTP_NOT_FOUND);
+    }
 }