Example #1
0
 /**
  * @param Request  $request
  * @param Response $response
  * @param callable $next
  * @return Response
  */
 public function __invoke(Request $request, Response $response, callable $next)
 {
     $dispatcher = new Dispatcher($this->collector->getData());
     $result = $dispatcher->dispatch($request->getMethod(), $request->getUri()->getPath());
     $code = $result[0];
     if ($code != Dispatcher::FOUND) {
         return $next($request, $response);
     }
     /** @var Route $route */
     $route = $result[1];
     $params = $result[2];
     $request = $request->withAttribute('params', $params);
     foreach ($params as $key => $value) {
         $request[$key] = $value;
     }
     // Custom middleware handler.
     // We have to iterate through middleware as it was added so that they retain the proper ordering.
     // We have to check if the middleware is a route and if it was the route matched during dispatching.
     // If so, we can call the route handler, otherwise, we need to skip over it.
     $middleware = $this->middleware;
     $callable = function ($request, $response) use(&$callable, &$middleware, $route, $next) {
         /** @var callable $layer */
         $layer = array_shift($middleware);
         if ($layer instanceof Route) {
             if ($layer !== $route) {
                 return $callable($request, $response, $next);
             }
             $layer = $this->resolver->resolve($route->getHandler());
             return $layer($request, $response);
         }
         return $layer($request, $response, $callable);
     };
     return $callable($request, $response);
 }
Example #2
0
 /**
  * URL からディスパッチ対象を取得する
  *
  * @param string $http_method
  * @param string $url
  * @return array
  */
 public function dispatch($http_method, $url)
 {
     if ($this->dispatcher === null) {
         throw new \RuntimeException('Route dispatcher is not initialized');
     }
     $this->dispatched_http_method = $http_method;
     $this->dispatched_url = $url;
     $this->route_info = $this->dispatcher->dispatch($http_method, $url);
     return $this->route_info;
 }
Example #3
0
 /**
  * @internal
  * @param Request $request
  * @param Response $response
  * @param callable $next
  * @return Response
  */
 public function dispatchHandler(Request $request, Response $response, callable $next)
 {
     $dispatcher = new Dispatcher($this->collector->getData());
     $result = $dispatcher->dispatch($request->getMethod(), $request->getUri()->getPath());
     $code = $result[0];
     $handler = isset($result[1]) ? $result[1] : null;
     $params = isset($result[2]) ? $result[2] : [];
     if ($code != Dispatcher::FOUND) {
         return $next ? $next($request, $response) : $response;
     }
     foreach ($params as $key => $value) {
         $request[$key] = $value;
     }
     return $this->paramHandler($request, $response, $next, $handler, $params);
 }
Example #4
0
 /**
  * Visszaadja a paraméterben megadott adatok alapján, a megfelelő handler-t és az ahhoz tartozó esetleges
  * paramétereket.
  *
  * Ha nem talál egyezést abban az esetben HttpNotFoundException hibát dob.
  *
  *
  * @param $method
  * @param $uri
  * @return array
  * @throws HttpNotFoundException
  */
 public function findRoute($method, $uri)
 {
     // Strip query string (?foo=bar) and decode URI
     if (false !== ($pos = strpos($uri, '?'))) {
         $uri = substr($uri, 0, $pos);
     }
     $uri = rawurldecode($uri);
     $routeInfo = $this->dispatcher->dispatch($method, $uri);
     switch ($routeInfo[0]) {
         case RouterInterface::NOT_FOUND:
             throw new HttpNotFoundException();
         case RouterInterface::METHOD_NOT_ALLOWED:
             throw new HttpNotFoundException();
         case RouterInterface::FOUND:
             $params = $routeInfo[2];
             //Ha van locale, akkor azt beállítjuk és töröljük a paraméter listából
             if (isset($params["locale"])) {
                 Application::Translator()->setLocale($params["locale"]);
                 unset($params["locale"]);
             }
             foreach ($this->routes as $route) {
                 if ($route["handler"] == $routeInfo[1]) {
                     $this->actualRoute = $route;
                     break;
                 }
             }
             return ["handler" => $routeInfo[1], "params" => $params];
         default:
             return [];
     }
 }
 public function it_should_throw_exception_when_unknown_routing_status(ServerRequestInterface $request, UriInterface $uri, ResponseInterface $response, GroupCountBased $dispatcher)
 {
     $request->getMethod()->willReturn('GET');
     $request->getUri()->willReturn($uri);
     $uri->getPath()->willReturn('/');
     $dispatcher->dispatch('GET', '/')->willReturn([99999]);
     $this->shouldThrow(InternalErrorHttpException::class)->during('__invoke', [$request, $response, function ($request, $response) {
         return $response;
     }]);
 }
Example #6
0
 public function match($uri, $method)
 {
     $routeInfo = array();
     $dispatcher = new FastRoute\Dispatcher\GroupCountBased($this->fastRoute->getData());
     $fastRouteInfo = $dispatcher->dispatch($method, $uri);
     switch ($fastRouteInfo[0]) {
         case FastRoute\Dispatcher::NOT_FOUND:
             $routeInfo[0] = self::NOT_FOUND;
             break;
         case FastRoute\Dispatcher::METHOD_NOT_ALLOWED:
             $routeInfo[0] = self::METHOD_NOT_ALLOWED;
             break;
         case FastRoute\Dispatcher::FOUND:
             $routeInfo[0] = self::FOUND;
             $routeInfo[1] = $fastRouteInfo[1];
             $routeInfo[2] = $fastRouteInfo[2];
             break;
     }
     return $routeInfo;
 }
Example #7
0
 /**
  * Dispatch router
  *
  * @param string $requestUri
  * @param string $requestMethod
  * @param callable $callable
  * @return $this
  * @throws MethodNotAllowed
  * @throws NotFound
  */
 public function dispatch(string $requestUri, string $requestMethod, callable $callable)
 {
     $this->registerRoutes();
     $routeInfo = $this->dispatcher->dispatch($requestMethod, $requestUri);
     switch ($routeInfo[0]) {
         case FastRoute\Dispatcher::NOT_FOUND:
             throw new NotFound('Unable to match request for route ' . $requestUri . ' and method: ' . $requestMethod);
             break;
         case FastRoute\Dispatcher::METHOD_NOT_ALLOWED:
             $allowedMethods = $routeInfo[1];
             throw new MethodNotAllowed($requestMethod . ' is not allowed for route', 0, null, $allowedMethods);
             break;
         case FastRoute\Dispatcher::FOUND:
             $handler = $routeInfo[1];
             $vars = $routeInfo[2];
             call_user_func_array($callable, [$handler, $vars]);
             break;
     }
     return $this;
 }
 /**
  * @param ServerRequestInterface $request
  * @param ResponseInterface $response
  * @param callable $next
  * @return ResponseInterface
  * @throws NotFoundHttpException
  * @throws MethodNotAllowedHttpException
  * @throws InternalErrorHttpException
  */
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
 {
     /**
      * dispatch before route
      */
     $this->event->dispatch(VinnigeEvents::BEFORE_ROUTE, new BeforeRouteEvent($request));
     $info = $this->dispatcher->dispatch($request->getMethod(), $request->getUri()->getPath());
     switch ($info[0]) {
         case Dispatcher::NOT_FOUND:
             throw new NotFoundHttpException('not found');
             break;
         case Dispatcher::METHOD_NOT_ALLOWED:
             throw new MethodNotAllowedHttpException([$info[0][1]], 'method not allowed');
             break;
         case Dispatcher::FOUND:
             $request = $request->withAttribute('param', $info[2]);
             /**
              * dispatch before dispatch event
              */
             $this->event->dispatch(VinnigeEvents::BEFORE_DISPATCH_ROUTE, new BeforeDispatchRouteEvent($request, $info[1]));
             if (is_callable($info[1])) {
                 $response = $info[1]($request, $response);
             } else {
                 $controller = $this->app[$info[1]];
                 if (!$controller instanceof ClassBasedRoutingHandlerInterface) {
                     throw new InternalErrorHttpException(sprintf('class %s must implement %s', $info[1], ClassBasedRoutingHandlerInterface::class));
                 }
                 /**
                  * resolve middleware from container and invoke it
                  */
                 $response = $controller($request, $response);
             }
             return $next($request, $response, $next);
             break;
         default:
             throw new InternalErrorHttpException('routing error');
     }
 }
Example #9
0
 /**
  * Match and dispatch a route matching the given http method and uri
  *
  * @param  string $method
  * @param  string $uri
  * @return \Symfony\Component\HttpFoundation\Response
  */
 public function dispatch($method, $uri)
 {
     $match = parent::dispatch($method, $uri);
     if ($match[0] === FastRoute::NOT_FOUND) {
         return $this->handleNotFound();
     }
     if ($match[0] === FastRoute::METHOD_NOT_ALLOWED) {
         $allowed = (array) $match[1];
         return $this->handleNotAllowed($allowed);
     }
     $handler = isset($this->routes[$match[1]]['callback']) ? $this->routes[$match[1]]['callback'] : $match[1];
     $strategy = $this->routes[$match[1]]['strategy'];
     $vars = (array) $match[2];
     return $this->handleFound($handler, $strategy, $vars);
 }
Example #10
0
 public function find($httpMethod, $uri, ServerRequestInterface $request)
 {
     list($this->staticRouteMap, $this->variableRouteData) = $this->getCollector()->getData();
     $result = parent::dispatch($httpMethod, $uri);
     switch ($result[0]) {
         case self::NOT_FOUND:
             throw new RouteNotFoundException();
         case self::METHOD_NOT_ALLOWED:
             throw new MethodNotAllowedException((array) $result[1]);
     }
     $route = $this->getRoute($result[1], $request);
     foreach ((array) $result[2] as $name => $value) {
         $route->setAttribute($name, $value);
     }
     return $route;
 }
Example #11
0
 /**
  * Match and dispatch a route matching the given http method and uri
  *
  * @param  string $method
  * @param  string $uri
  * @return \Orno\Http\ResponseInterface
  */
 public function dispatch($method, $uri)
 {
     $match = parent::dispatch($method, $uri);
     switch ($match[0]) {
         case FastRoute\Dispatcher::NOT_FOUND:
             $response = $this->handleNotFound();
             break;
         case FastRoute\Dispatcher::METHOD_NOT_ALLOWED:
             $allowed = (array) $match[1];
             $response = $this->handleNotAllowed($allowed);
             break;
         case FastRoute\Dispatcher::FOUND:
         default:
             $handler = isset($this->routes[$match[1]]['callback']) ? $this->routes[$match[1]]['callback'] : $match[1];
             $strategy = $this->routes[$match[1]]['strategy'];
             $vars = (array) $match[2];
             $response = $this->handleFound($handler, $strategy, $vars);
             break;
     }
     return $response;
 }
Example #12
0
 /**
  * @param $method
  * @param $uri
  * @return array
  * @throws HttpNotFoundException
  */
 public function findRoute($method, $uri)
 {
     $routes = $this->getRoutes();
     //Hozzá kell még adni a nyelvi url-t, ha van.
     $translator = Application::Translator();
     if ($translator != null) {
         $langRoute = $translator->getCookieSetUrl();
         if ($langRoute != null) {
             $routes["langSetRoute"] = ["method" => $langRoute["method"], "url" => $langRoute["url"], "handler" => $langRoute["handler"]];
         }
     }
     $this->dispatcher = simpleDispatcher(function (RouteCollector $r) use($routes) {
         foreach ($routes as $route) {
             $r->addRoute($route["method"], $route["url"], $route["handler"]);
         }
     });
     // Strip query string (?foo=bar) and decode URI
     if (false !== ($pos = strpos($uri, '?'))) {
         $uri = substr($uri, 0, $pos);
     }
     $uri = rawurldecode($uri);
     $routeInfo = $this->dispatcher->dispatch($method, $uri);
     switch ($routeInfo[0]) {
         case RouterInterface::NOT_FOUND:
             throw new HttpNotFoundException();
         case RouterInterface::METHOD_NOT_ALLOWED:
             throw new HttpNotFoundException();
         case RouterInterface::FOUND:
             foreach ($routes as $route) {
                 if ($route["handler"] == $routeInfo[1]) {
                     $this->actualRoute = $route;
                     break;
                 }
             }
             return ["handler" => $routeInfo[1], "params" => $routeInfo[2]];
         default:
             return [];
     }
 }
 public function checkGroupUrl($path)
 {
     if ($this->cacheRouteData === false) {
         if (!file_exists($this->authRouteFile)) {
             return false;
         }
         $this->cacheRouteData = (require $this->authRouteFile);
     }
     $dispatcher = new GroupCountBased($this->cacheRouteData);
     $route = $dispatcher->dispatch('GET', $path);
     switch ($route[0]) {
         case \FastRoute\Dispatcher::NOT_FOUND:
             return false;
         case \FastRoute\Dispatcher::METHOD_NOT_ALLOWED:
             return false;
         case \FastRoute\Dispatcher::FOUND:
             return call_user_func_array($route[1], $route[2]);
         default:
             return false;
     }
 }
Example #14
0
 /**
  * Wrapper for using resolve() instead of dispatch().
  *
  * @param string $httpMethod The HTTP Method to route
  * @param string $uri        The URI to route
  *
  * @author Benjamin Carl <*****@*****.**>
  * @return array The result as array?
  * @access public
  */
 public function resolve($httpMethod, $uri)
 {
     return parent::dispatch($httpMethod, $uri);
 }
Example #15
0
 /**
  * Parses the user request against the provided HTTP method verb and URI.
  *
  * Returns array (from FastRoute\Dispatcher) with one of the following formats:
  *
  *     [FastRoute\Dispatcher::NOT_FOUND]
  *     [FastRoute\Dispatcher::METHOD_NOT_ALLOWED, ['GET', 'OTHER_ALLOWED_METHODS']]
  *     [FastRoute\Dispatcher::FOUND, $handler, ['varName' => 'value', ...]]
  *
  * @param string $method
  * @param string $uri
  * @return array
  */
 public function parseRequest($method, $uri)
 {
     $this->buildRules();
     $data = $this->routeCollector->getData();
     $dispatcher = new Dispatcher($data);
     $routeInfo = $dispatcher->dispatch($method, $uri);
     if ($routeInfo[0] == Dispatcher::FOUND) {
         $routeInfo[2] = array_merge($this->rules[$routeInfo[1]]->getDefault(), $routeInfo[2]);
     }
     return $routeInfo;
 }
Example #16
0
 public function dispatch($method, $uri)
 {
     $routes = $this->container->get('config')['http']['routes'];
     $uri = rtrim($uri, '/');
     if ($uri === '') {
         $uri .= '/';
     }
     $matchedRoute = parent::dispatch($method, $uri);
     $hasMatchedRoute = false;
     $anonymous = false;
     if ($matchedRoute[0] === Dispatcher::NOT_FOUND) {
         return $this->handleRouteNotFound();
     }
     if ($matchedRoute) {
         foreach ($routes as $key => $route) {
             if ($matchedRoute[0] === Dispatcher::METHOD_NOT_ALLOWED && $route['path'] === $this->request->getPathInfo()) {
                 if (isset($route['strategy'])) {
                     switch ($route['strategy']) {
                         case Route::STRATEGY_RESTFUL:
                             $this->setStrategy(new RestfulStrategy());
                             break;
                         case Route::STRATEGY_REQUEST_RESPONSE:
                         default:
                             $this->setStrategy(new RequestResponseStrategy());
                     }
                 } else {
                     $this->setStrategy(new RequestResponseStrategy());
                 }
             } elseif ($matchedRoute[0] === Dispatcher::FOUND && $route['path'] === $this->request->getPathInfo() && $route['method'] === $this->request->getMethod() && $route['target'] === $matchedRoute[1]) {
                 if (isset($route['anonymous'])) {
                     $anonymous = $route['anonymous'];
                 }
                 if (isset($route['strategy'])) {
                     switch ($route['strategy']) {
                         case Route::STRATEGY_RESTFUL:
                             $this->setStrategy(new RestfulStrategy());
                             break;
                         case Route::STRATEGY_REQUEST_RESPONSE:
                         default:
                             $this->setStrategy(new RequestResponseStrategy());
                     }
                 } else {
                     $this->setStrategy(new RequestResponseStrategy());
                 }
                 break;
             }
         }
     }
     if ($matchedRoute[0] === Dispatcher::METHOD_NOT_ALLOWED) {
         return $this->handleMethodNotAllowed();
     }
     $handler = isset($this->routes[$matchedRoute[1]]['callback']) ? $this->routes[$matchedRoute[1]]['callback'] : $matchedRoute[1];
     $vars = (array) $matchedRoute[2];
     if (!$anonymous) {
         $this->auth->authenticate(Auth::METHOD_TOKEN);
         if (!$this->auth->hasAuthenticatedUser()) {
             return $this->handleUnauthenticated();
         } elseif (!$this->auth->isAuthenticatedUserAuthorized($handler)) {
             return $this->handleUnauthorized();
         }
     }
     return $this->handleResponse($handler, $this->getStrategy(), $vars);
 }
Example #17
0
 /**
  * Dispatch router for HTTP request
  *
  * @param  ServerRequestInterface $request The current HTTP request object
  *
  * @return array
  *
  * @link   https://github.com/nikic/FastRoute/blob/master/src/Dispatcher.php
  */
 public function dispatch(ServerRequestInterface $request)
 {
     $this->finalize();
     $dispatcher = new GroupCountBasedDispatcher($this->getData());
     $uri = '/' . ltrim($request->getUri()->getPath(), '/');
     return $dispatcher->dispatch($request->getMethod(), $uri);
 }