/**
  * @inheritDoc
  */
 public function match(PsrRequest $request)
 {
     // Must inject routes prior to matching.
     $this->injectRoutes();
     $match = $this->zendRouter->match(Psr7ServerRequest::toZend($request, true));
     if (null === $match) {
         return RouteResult::fromRouteFailure();
     }
     return $this->marshalSuccessResultFromRouteMatch($match);
 }
Example #2
0
 /**
  * Attempt to match an incoming request to a registered route.
  *
  * @param PsrRequest $request
  * @return RouteResult
  */
 public function match(PsrRequest $request)
 {
     $match = $this->zf2Router->match(Psr7ServerRequest::toZend($request, true));
     if (null === $match) {
         return RouteResult::fromRouteFailure();
     }
     $allowedMethods = $this->getAllowedMethods($match->getMatchedRouteName());
     if (!$this->methodIsAllowed($request->getMethod(), $allowedMethods)) {
         return RouteResult::fromRouteFailure($allowedMethods);
     }
     return $this->marshalSuccessResultFromRouteMatch($match);
 }
 public function loadRequestId(MvcEvent $event)
 {
     $request = $event->getRequest();
     if ($request instanceof HttpRequest) {
         $psr7Request = Psr7ServerRequest::fromZend($request);
         $requestIdProvider = $this->requestIdProviderFactory->create($psr7Request);
         $this->requestId = $requestIdProvider->getRequestId();
     } elseif ($this->requestIdGenerator !== null) {
         $this->requestId = $this->requestIdGenerator->generateRequestId();
     }
     return $this->requestId;
 }
 /**
  * Listen to the "dispatch" event
  *
  * @param  MvcEvent $event
  * @return mixed
  */
 public function onDispatch(MvcEvent $event)
 {
     $routeMatch = $event->getRouteMatch();
     $middleware = $routeMatch->getParam('middleware', false);
     if (false === $middleware) {
         return;
     }
     $request = $event->getRequest();
     $application = $event->getApplication();
     $response = $application->getResponse();
     $serviceManager = $application->getServiceManager();
     $middlewareName = is_string($middleware) ? $middleware : get_class($middleware);
     if (is_string($middleware) && $serviceManager->has($middleware)) {
         $middleware = $serviceManager->get($middleware);
     }
     if (!is_callable($middleware)) {
         $return = $this->marshalMiddlewareNotCallable($application::ERROR_MIDDLEWARE_CANNOT_DISPATCH, $middlewareName, $event, $application);
         $event->setResult($return);
         return $return;
     }
     $caughtException = null;
     try {
         $return = $middleware(Psr7Request::fromZend($request), Psr7Response::fromZend($response));
     } catch (\Throwable $ex) {
         $caughtException = $ex;
     } catch (\Exception $ex) {
         // @TODO clean up once PHP 7 requirement is enforced
         $caughtException = $ex;
     }
     if ($caughtException !== null) {
         $event->setName(MvcEvent::EVENT_DISPATCH_ERROR);
         $event->setError($application::ERROR_EXCEPTION);
         $event->setController($middlewareName);
         $event->setControllerClass(get_class($middleware));
         $event->setParam('exception', $caughtException);
         $events = $application->getEventManager();
         $results = $events->triggerEvent($event);
         $return = $results->last();
         if (!$return) {
             $return = $event->getResult();
         }
     }
     if (!$return instanceof PsrResponseInterface) {
         $event->setResult($return);
         return $return;
     }
     $response = Psr7Response::toZend($return);
     $event->setResult($response);
     return $response;
 }
Example #5
0
 /**
  * Attempt to match an incoming request to a registered route.
  *
  * @param PsrRequest $request
  * @return RouteResult
  */
 public function match(PsrRequest $request)
 {
     $match = $this->zf2Router->match(Psr7ServerRequest::toZend($request, true));
     if (null === $match) {
         return RouteResult::fromRouteFailure();
     }
     return $this->marshalSuccessResultFromRouteMatch($match);
 }