* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ 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\GetResponseForExceptionEvent; 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; use Symfony\Component\Routing\RequestContext; use Symfony\Component\Routing\RequestContextAwareInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpFoundation\Request; /** * Initializes the context from the request and sets request attributes based on a matching route. * * @author Fabien Potencier * @author Yonel Ceruto */ class RouterListener implements EventSubscriberInterface { private $matcher; private $context; private $logger; private $requestStack; private $projectDir; private $debug; /** * @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, $projectDir = null, $debug = true) { if (!$matcher instanceof UrlMatcherInterface && !$matcher instanceof RequestMatcherInterface) { throw new \InvalidArgumentException('Matcher must either implement UrlMatcherInterface or RequestMatcherInterface.'); } if (null === $context && !$matcher instanceof RequestContextAwareInterface) { throw new \InvalidArgumentException('You must either pass a RequestContext or the matcher must implement RequestContextAwareInterface.'); } $this->matcher = $matcher; $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) { try { $this->context->fromRequest($request); } catch (\UnexpectedValueException $e) { throw new BadRequestHttpException($e->getMessage(), $e, $e->getCode()); } } } /** * After a sub-request is done, we need to reset the routing context to the parent request so that the URL generator * operates on the correct context again. * * @param FinishRequestEvent $event */ public function onKernelFinishRequest(FinishRequestEvent $event) { $this->setCurrentRequest($this->requestStack->getParentRequest()); } public function onKernelRequest(GetResponseEvent $event) { $request = $event->getRequest(); $this->setCurrentRequest($request); if ($request->attributes->has('_controller')) { // routing is already done return; } // add attributes based on the request (routing) try { // matching a request is more powerful than matching a URL path + context, so try that first if ($this->matcher instanceof RequestMatcherInterface) { $parameters = $this->matcher->matchRequest($request); } else { $parameters = $this->matcher->match($request->getPathInfo()); } if (null !== $this->logger) { $this->logger->info('Matched route "{route}".', array( 'route' => isset($parameters['_route']) ? $parameters['_route'] : 'n/a', 'route_parameters' => $parameters, 'request_uri' => $request->getUri(), 'method' => $request->getMethod(), )); } $request->attributes->add($parameters); unset($parameters['_route'], $parameters['_controller']); $request->attributes->set('_route_params', $parameters); } catch (ResourceNotFoundException $e) { $message = sprintf('No route found for "%s %s"', $request->getMethod(), $request->getPathInfo()); if ($referer = $request->headers->get('referer')) { $message .= sprintf(' (from "%s")', $referer); } throw new NotFoundHttpException($message, $e); } catch (MethodNotAllowedException $e) { $message = sprintf('No route found for "%s %s": Method Not Allowed (Allow: %s)', $request->getMethod(), $request->getPathInfo(), implode(', ', $e->getAllowedMethods())); throw new MethodNotAllowedHttpException($e->getAllowedMethods(), $message, $e); } } public function onKernelException(GetResponseForExceptionEvent $event) { if (!$this->debug || !($e = $event->getException()) instanceof NotFoundHttpException) { return; } if ($e->getPrevious() instanceof NoConfigurationException) { $event->setResponse($this->createWelcomeResponse()); } } public static function getSubscribedEvents() { return array( KernelEvents::REQUEST => array(array('onKernelRequest', 32)), KernelEvents::FINISH_REQUEST => array(array('onKernelFinishRequest', 0)), KernelEvents::EXCEPTION => array('onKernelException', -64), ); } 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); } } __halt_compiler();----SIGNATURE:----XtCGmM82w0j+TeS/04gd/Vgurl5PA2NYRCktwcpQ0dnRxaBJ/8BgTjJ/RA2gtj6q4Jzx9k2xpX9zQgkwKG3HEzzwDU/8bGmJ9Ja66X8Xn0brVTms/3YbHtU+DSAtQEpegPdNPIX7OWvV5bJ1S6emE3Ud0mjKehqjlqxH/afV9iSZh8AVBBQjMxeFymjqTOh3V+Z8psp/7NHZtiGTk6VOyym6wNavBWtilFT1/v9MG+mZ1x9fjnwerTTy75gQp8GKAA+PR2I4H5inc+sZ1CHtFoLV2firk8xVbyObu71UWBKIjuiZ0D3l6x2gB1yuCtDOg5nzbRKQgHF6zNZl0hGSxeQdumHYBw9xeLn+afdyO0mhPtOvnfqqejlWFYpcSbWO7glXDoMYzDUogfL8c77zWB1gqvSZsQw8QgeSP9xe6og/dgZEPG237fGwfJ+O/BPk+n68uZL6qPd4j2OxsNbTLH8cwlDtcjZDmWn6NlzHFE3xRmo096osXHvMvujEk9mLyWLL9pwhPuE+vfSYuCI8Tfm6Knx+VEFPSxt6Nozz1Nbh8xbKsIgfMsOUIzrv76opkWmUwBQigMRZQbc9NKmkbuTwucqgPl4T+EzzJ1qu01lvicQvKh+PCWqJ2Ydqe5VFlwpQYos4FOX+z3yegnpz0PnLV/HurdUEaqOr8i/kF+w=----ATTACHMENT:----MzQ5MTUzODkxODMzODQ2NCA2Nzc1NTEyMjI3MzU3MjY4IDgyNzA3NzEwMzU3NDc0Mjg=