Ejemplo n.º 1
0
 /**
  * Run the application.
  *
  * @param string $httpMethod
  * @param string $path
  * @throws \Exception
  */
 public function run($httpMethod, $path)
 {
     $routeInfo = $this->dispatcher->dispatch($httpMethod, $path);
     switch ($routeInfo[0]) {
         case Dispatcher::NOT_FOUND:
             echo '404';
             break;
         case Dispatcher::METHOD_NOT_ALLOWED:
             echo 'Method not allowed';
             break;
         case Dispatcher::FOUND:
             $handler = $routeInfo[1];
             $vars = $routeInfo[2];
             if (is_string($handler)) {
                 $parts = explode('.', $handler);
                 // If we have only 1 part, we assume it's just a global function.
                 if (count($parts) === 2) {
                     list($className, $methodName) = $parts;
                     $controller = new $className();
                     echo call_user_func_array([$controller, $methodName], $vars);
                 } elseif (count($parts) === 1) {
                     $funcName = $parts[0];
                     echo call_user_func_array($funcName, $vars);
                 } else {
                     throw new \Exception('Invalid handler name.');
                 }
             }
             break;
         default:
             assert(false);
             break;
     }
 }
Ejemplo n.º 2
0
 /**
  * Tries to find a route matching the current request. If found the defined action is called.
  */
 protected function dispatch()
 {
     $httpMethod = $_SERVER['REQUEST_METHOD'];
     $uri = rawurldecode(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
     $routeInfo = $this->dispatcher->dispatch($httpMethod, $uri);
     if (!isset($routeInfo[0])) {
         throw new RuntimeException('Could not dispatch request.');
     }
     switch ($routeInfo[0]) {
         case FastRoute\Dispatcher::NOT_FOUND:
             $responder = new NotFoundResponder($this->config);
             $responder->__invoke();
             break;
         case FastRoute\Dispatcher::METHOD_NOT_ALLOWED:
             $responder = new HttpResponder($this->config);
             $responder->methodNotAllowed();
             break;
         case FastRoute\Dispatcher::FOUND:
             $handler = $routeInfo[1];
             $arguments = $routeInfo[2];
             $this->runAction($handler, $arguments);
             break;
         default:
             throw new RuntimeException('Could not dispatch request.');
     }
 }
Ejemplo n.º 3
0
 public function getEventForRequest(RequestInterface $request)
 {
     $requestedRoute = $request->getUrl()->getPath();
     $requestedMethod = $request->getMethod();
     $routeInfo = $this->dispatcher->dispatch($requestedMethod, $requestedRoute);
     switch ($routeInfo[0]) {
         case Dispatcher::NOT_FOUND:
             throw new NotFoundException('Route \'' . $requestedRoute . '\' with method \'' . $requestedMethod . '\' not found.');
             break;
         case Dispatcher::METHOD_NOT_ALLOWED:
             //$allowedMethods = $routeInfo[1];
             throw new Exception('Method not allowed', StatusCode::METHOD_NOT_ALLOWED);
             break;
         case Dispatcher::FOUND:
             /** @var string $eventClass */
             $eventClass = $routeInfo[1];
             /** @var RouteFoundEvent $event */
             $event = new $eventClass();
             /** @var string[] $vars */
             $vars = $routeInfo[2];
             $request->getRouteParameterCollection()->setAll($vars);
             $event->setRequest($request);
             return $event;
         default:
             throw new Exception('Error in FastRoute!');
     }
 }
 public function getCommand()
 {
     $request = $this->requestKnowledge->getRequest();
     $requestedRoute = $request->getUri()->getPath();
     $requestedMethod = $request->getMethod();
     $routeInfo = $this->dispatcher->dispatch($requestedMethod, $requestedRoute);
     switch ($routeInfo[0]) {
         case Dispatcher::NOT_FOUND:
             throw new NotFoundException('Route \'' . $requestedRoute . '\' with method \'' . $requestedMethod . '\' not found.');
         case Dispatcher::METHOD_NOT_ALLOWED:
             //$allowedMethods = $routeInfo[1];
             throw new MethodNotAllowedException('Method not allowed');
         case Dispatcher::FOUND:
             /** @var string $commandCreatorClass */
             $commandCreatorClass = $routeInfo[1];
             $sl = $this->serviceLocator;
             /** @var CommandCreatorInterface $commandCreator */
             $commandCreator = $sl($commandCreatorClass);
             /** @var string[] $vars */
             $vars = $routeInfo[2];
             $request = $request->withAttribute(RouterInterface::MATCHED_VARIABLES_ARRAY_KEY, $vars);
             $this->requestKnowledge->setRequest($request);
             return $commandCreator->createCommand($request);
         default:
             throw new Exception('Error in FastRoute!');
     }
 }
Ejemplo n.º 5
0
 /**
  * @param ServerRequestInterface $request
  * @param ResponseInterface $response
  * @param callable $next
  * @return ResponseInterface
  */
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
 {
     $routeInfo = $this->dispatcher->dispatch($request->getMethod(), $request->getUri()->getPath());
     switch ($routeInfo[0]) {
         case Dispatcher::NOT_FOUND:
             $response = $response->withStatus(404);
             break;
         case Dispatcher::METHOD_NOT_ALLOWED:
             $response = $response->withStatus(405);
             break;
         case Dispatcher::FOUND:
             $callable = $routeInfo[1];
             $args = $routeInfo[2];
             if (isset($this->container)) {
                 $response = $this->container->call($callable, ['request' => $request, 'response' => $response, 'arguments' => $args]);
             } else {
                 if (is_callable($callable)) {
                     call_user_func($callable, $request, $response, $args);
                 } else {
                     $callable = new $callable();
                     call_user_func($callable, $request, $response, $args);
                 }
             }
             break;
         default:
             return $response->withStatus(500);
     }
     $response = $next($request, $response);
     return $response;
 }
 /**
  * {@inheritdoc}
  */
 public function route(ServerRequestInterface $request) : RoutingResponse
 {
     $httpMethod = $request->getMethod();
     $uri = $request->getUri()->getPath();
     $routeInfo = $this->dispatcher->dispatch($httpMethod, $uri);
     return $this->processDispatcherResult($routeInfo);
 }
Ejemplo n.º 7
0
 /**
  * @param \Psr\Http\Message\ServerRequestInterface $request
  * @param \Psr\Http\Message\ResponseInterface $response
  * @param callable|null $next
  *
  * @throws \Wiring\Exception\MethodNotAllowedException
  * @throws \Wiring\Exception\NotFoundException
  * @return \Psr\Http\Message\ResponseInterface
  */
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
 {
     $routeInfo = $this->fastRoute->dispatch($request->getMethod(), $request->getUri()->getPath());
     if ($routeInfo[0] == Dispatcher::FOUND) {
         // Get request params
         foreach ($routeInfo[2] as $param => $value) {
             $request = $request->withAttribute($param, $value);
         }
         // Get request with attribute
         $request = $request->withAttribute($this->attribute, $routeInfo[1]);
         return $next($request, $response);
     }
     if ($routeInfo[0] == Dispatcher::METHOD_NOT_ALLOWED) {
         // Check has handler
         if (!$this->container->has(MethodNotAllowedHandlerInterface::class)) {
             throw new MethodNotAllowedException($request, $response, $routeInfo[1]);
         }
         /** @var callable $notAllowedHandler */
         $notAllowedHandler = $this->container->get(MethodNotAllowedHandlerInterface::class);
         return $notAllowedHandler($request, $response, $routeInfo[1]);
     }
     // Check has handler
     if (!$this->container->has(NotFoundHandlerInterface::class)) {
         throw new NotFoundException($request, $response);
     }
     /** @var callable $notFoundHandler */
     $notFoundHandler = $this->container->get(NotFoundHandlerInterface::class);
     return $notFoundHandler($request, $response);
 }
Ejemplo n.º 8
0
 /**
  * Dispatches against the provided HTTP method verb and URI.
  *
  * @param  string $httpMethod
  * @param  string $uri
  * @return array
  */
 public function dispatch($httpMethod, $uri)
 {
     $result = $this->dispatcher->dispatch($httpMethod, $uri);
     $route = $this->router->getRoute($httpMethod, $uri);
     $this->throwException($result[0], $uri);
     $middlewares = $route[2] == $route[1] && isset($route[3]) ? $route[3] : [];
     return [$result[1], $result[2], $middlewares];
 }
Ejemplo n.º 9
0
 function it_throws_not_found_exception(ServerRequestInterface $request, RouteCollection $routeCollection, Route $route, UriInterface $uri, Dispatcher $dispatcher)
 {
     $routeCollection->getRoutes()->willReturn([$route]);
     $request->getUri()->willReturn($uri);
     $uri->getPath()->willReturn('/url');
     $request->getMethod()->willReturn('GET');
     $dispatcher->dispatch('GET', '/url')->willReturn([Dispatcher::NOT_FOUND]);
     $this->shouldThrow(NotFoundException::class)->match($request, $routeCollection);
 }
Ejemplo n.º 10
0
 /**
  * {@inheritdoc}
  */
 public function match($method, $uri)
 {
     $routeInfo = $this->dispatcher->dispatch($method, $uri);
     switch ($routeInfo[0]) {
         case Dispatcher::METHOD_NOT_ALLOWED:
             throw new MethodNotAllowedException($method, $routeInfo[1]);
         case Dispatcher::NOT_FOUND:
             throw new NotFoundException($uri);
     }
     return new Result($this->routes[$routeInfo[1]], $routeInfo[2]);
 }
 /**
  * @param ServerRequestInterface $request
  * 
  * @return mixed
  */
 public function data(ServerRequestInterface $request)
 {
     /** @var IDataProvider $provider */
     list($result, $provider, $parameters) = $this->dispatcher->dispatch($request->getMethod(), $request->getUri()->getPath()) + [null, null, []];
     switch ($result) {
         case \FastRoute\Dispatcher::FOUND:
             $queryParamsFromRoute = array_merge($request->getQueryParams(), $parameters);
             $request = $request->withQueryParams($queryParamsFromRoute);
             return $provider->data($request);
         default:
             return null;
     }
 }
Ejemplo n.º 12
0
 /**
  * @param Dispatcher $dispatcher
  * @param string $method
  * @param string $path
  *
  * @return [Action, $arguments]
  *
  * @throws HttpNotFound
  * @throws HttpMethodNotAllowed
  */
 private function dispatch(Dispatcher $dispatcher, $method, $path)
 {
     $route = $dispatcher->dispatch($method, $path);
     $status = array_shift($route);
     if (Dispatcher::FOUND === $status) {
         return $route;
     }
     if (Dispatcher::METHOD_NOT_ALLOWED === $status) {
         $allowed = array_shift($route);
         throw HttpException::methodNotAllowed($path, $method, $allowed);
     }
     throw HttpException::notFound($path);
 }
Ejemplo n.º 13
0
 /**
  * Get a route matching the provided request information.
  *
  * @param string $pathInfo
  * @param string $httpMethod
  * @return Route
  */
 public function route(string $pathInfo, string $httpMethod) : Route
 {
     $matchedRoute = $this->dispatcher->dispatch($httpMethod, $pathInfo);
     if ($matchedRoute[0] === Dispatcher::NOT_FOUND) {
         throw new \RuntimeException("Route not found");
     }
     if ($matchedRoute[0] === Dispatcher::METHOD_NOT_ALLOWED) {
         throw new \RuntimeException("Method not allowed");
     }
     $route = $matchedRoute[1];
     $route->setParams($matchedRoute[2]);
     return $route;
 }
Ejemplo n.º 14
0
 public function processRequest(ServerRequestInterface $request) : int
 {
     $ret = $this->dispatcher->dispatch($request->getMethod(), $request->getUri()->getPath());
     switch ($ret[0]) {
         case Dispatcher::FOUND:
             $this->route = $this->collection->getRoute($ret[1]);
             $this->vars = $ret[2];
             return RouterInterface::STATUS_FOUND;
         case Dispatcher::METHOD_NOT_ALLOWED:
             $this->allowed = $ret[1];
             return RouterInterface::STATUS_NOT_ALLOWED;
         default:
             return RouterInterface::STATUS_NOT_FOUND;
     }
 }
Ejemplo n.º 15
0
 /**
  * @throws \WoohooLabs\Harmony\Exception\MethodNotAllowed
  * @throws \WoohooLabs\Harmony\Exception\RouteNotFound
  */
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) : ResponseInterface
 {
     $route = $this->fastRoute->dispatch($request->getMethod(), $request->getUri()->getPath());
     if ($route[0] === Dispatcher::NOT_FOUND) {
         throw new RouteNotFound($request->getUri()->getPath());
     }
     if ($route[0] === Dispatcher::METHOD_NOT_ALLOWED) {
         throw new MethodNotAllowed($request->getMethod());
     }
     foreach ($route[2] as $name => $value) {
         $request = $request->withAttribute($name, $value);
     }
     $request = $request->withAttribute($this->actionAttributeName, $route[1]);
     return $next($request, $response);
 }
Ejemplo n.º 16
0
 /**
  * @param HttpdRequest $request
  * @param HttpdResponse $response
  * @param array $labels
  */
 protected function dispatch(HttpdRequest $request, HttpdResponse $response, array $labels = [])
 {
     $labels['method'] = strtolower($request->getMethod());
     if (empty($this->routes)) {
         $request->subscribeCallback(null, null, function () use($request, $response, $labels) {
             $this->notifyNext(new HttpdEvent("/httpd/request", ['request' => $request, 'response' => $response], $labels));
         });
         return;
     }
     $info = $this->dispatcher->dispatch($request->getMethod(), $request->getPath());
     switch ($info[0]) {
         case Dispatcher::NOT_FOUND:
             $response->sendError("Route does not exist", 404);
             break;
         case Dispatcher::METHOD_NOT_ALLOWED:
             $response->sendError("Method not allowed", 405);
             break;
         case Dispatcher::FOUND:
             $labels['method'] = $request->getMethod();
             $labels['path'] = $request->getPath();
             $labels = array_merge($labels, $info[2]);
             $callable = $info[1];
             $request->setRouteParams($info[2]);
             // For streamable route, subscribe on event
             $this->notifyNext(new HttpdEvent("/httpd/request", ['request' => $request, 'response' => $response], $labels));
             // On end of request (whole data received)
             $request->subscribeCallback(null, null, function () use($request, $response, $callable) {
                 $callable($request, $response);
             });
     }
 }
Ejemplo n.º 17
0
 /**
  * @param string $method - example: 'GET'
  * @param string $uri - example: '/users', '/some/{path}/[0-9]'
  * @return $this
  * @throws AclException
  */
 public function got($method, $uri)
 {
     $this->logger->debug("Try to dispatch {$method} {$uri}.");
     $this->routeInfo = $this->dispatcher->dispatch($method, $uri);
     $this->method = $method;
     $this->uri = $uri;
     return $this;
 }
Ejemplo n.º 18
0
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
 {
     $routeInfo = $this->dispatcher->dispatch($request->getMethod(), $request->getUri()->getPath());
     switch ($routeInfo[0]) {
         case FastRoute\Dispatcher::NOT_FOUND:
             return $response->withStatus(404);
         case FastRoute\Dispatcher::METHOD_NOT_ALLOWED:
             return $response->withStatus(405);
         case FastRoute\Dispatcher::FOUND:
             $action = $routeInfo[1];
             $parameters = $routeInfo[2];
             $request = $request->withAttribute($this->actionAttributeName, $action)->withAttribute($this->parametersAttributeName, $parameters);
             return $next($request, $response, $next);
         default:
             return $response->withStatus(500);
     }
 }
Ejemplo n.º 19
0
 /**
  * @param \Psr\Http\Message\ServerRequestInterface $request
  * @param \Psr\Http\Message\ResponseInterface $response
  * @param \WoohooLabs\Harmony\Harmony $next
  * @throws \WoohooLabs\Harmony\Exception\MethodNotAllowedException
  * @throws \WoohooLabs\Harmony\Exception\RouteNotFoundException
  */
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response, Harmony $next)
 {
     $routeInfo = $this->fastRoute->dispatch($request->getMethod(), $request->getUri()->getPath());
     switch ($routeInfo[0]) {
         case Dispatcher::NOT_FOUND:
             throw new RouteNotFoundException();
         case Dispatcher::METHOD_NOT_ALLOWED:
             throw new MethodNotAllowedException();
         case Dispatcher::FOUND:
             foreach ($routeInfo[2] as $param => $value) {
                 $request = $request->withAttribute($param, $value);
             }
             $request = $request->withAttribute("__callable", $routeInfo[1]);
             break;
     }
     $next($request, $response);
 }
Ejemplo n.º 20
0
 /**
  * Look for a route that matches the request.
  *
  * @param $http_method
  * @param $uri
  *
  * @return array
  */
 public function match($http_method = NULL, $uri = NULL)
 {
     // Fetch method and uri from injected request object
     $http_method = $http_method ?: $this->request->getMethod();
     $uri = $uri ?: $this->request->getUri()->getPath();
     // parse the request and return a status array
     $routeInfo = $this->dispatcher->dispatch($http_method, $uri);
     return $routeInfo;
 }
Ejemplo n.º 21
0
 /**
  * @return array
  */
 public function dispatch()
 {
     $this->http_fields = $_GET;
     $info = $this->fast_router->dispatch($_SERVER['REQUEST_METHOD'], $_SERVER['PATH_INFO']);
     switch ($info[0]) {
         case Dispatcher::FOUND:
             $attributes = explode('.', $info[1]);
             $resource = $attributes[0];
             $action = $attributes[1];
             $this->http_fields = array_merge($this->http_fields, $info[2]);
             break;
         default:
             $not_found_attributes = $this->getNotFoundAttributes();
             $resource = $not_found_attributes[1];
             $action = $not_found_attributes[2];
             break;
     }
     $this->input = file_get_contents("php://input");
     $data_type = $this->options['data_type'];
     // Get query parameters and args depending from type of data in the http request body
     $args = [];
     if ($data_type == 'query_string') {
         parse_str($this->input, $args);
     } else {
         if ($data_type == 'json') {
             $args = $this->input ? json_decode($this->input, true) : [];
             if (!is_array($args)) {
                 $args = [];
             }
         }
     }
     $this->http_fields = array_merge($this->http_fields, $args);
     // Trim all fields if auto_trim setting enabled
     if ($this->options['auto_trim']) {
         $this->http_fields = Arr::trim($this->http_fields);
     }
     // Convert empty strings to null values if auto_null setting enabled
     if ($this->options['auto_null']) {
         $this->http_fields = Arr::convertValues($this->http_fields, '', null);
     }
     return [$resource, $action, []];
 }
Ejemplo n.º 22
0
 public function matchRequest(SymfonyRequest $request)
 {
     $method = $request->getMethod();
     $path = $request->getPathInfo();
     $routeInfo = $this->dispatcher->dispatch($method, $path);
     switch ($routeInfo[0]) {
         case Dispatcher::METHOD_NOT_ALLOWED:
             //                $allowedMethods = $routeInfo[1];
             throw new MethodNotAllowedException();
         case Dispatcher::FOUND:
             $vars = $routeInfo[2];
             $vars['_route'] = $method . ' ' . $path;
             $vars['_controller'] = $routeInfo[1];
             $vars['_module'] = $this->getModuleName();
             return $vars;
         case Dispatcher::NOT_FOUND:
             throw new ResourceNotFoundException();
         default:
             throw new ResourceNotFoundException();
     }
 }
Ejemplo n.º 23
0
 /**
  * @param   ServerRequestInterface  $request
  * @param   ResponseInterface       $response
  * @param   callable                $out
  */
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $out = null)
 {
     $path = $request->getUri()->getPath();
     $endSlash = substr($path, -1);
     if (strlen($path) > 1 && $endSlash === '/') {
         // redirect *example* /tag/ --> /tag
         return $response->withHeader('Location', rtrim($request->getUri()->getPath(), '/'));
     }
     $currentRoute = $this->dispatcher->dispatch($request->getMethod(), $request->getUri()->getPath());
     switch ($currentRoute[0]) {
         case Dispatcher::METHOD_NOT_ALLOWED:
             throw new \UnexpectedValueException('Method not allowed');
         case Dispatcher::NOT_FOUND:
             throw new \OutOfRangeException('Route not found');
         case Dispatcher::FOUND:
             $response = $this->handleRoute($request, $response, $currentRoute);
             break;
     }
     if (!$response instanceof ResponseInterface) {
         throw new \InvalidArgumentException(sprintf("Action handler must return '%s' instance", ResponseInterface::class));
     }
     $response = $response instanceof Response ?: new Response($response);
     return $out($request, $response);
 }
 /**
  * Get the handler service name for a request.
  * 
  * @param ServerRequestInterface $request  Request.
  * @param ResponseInterface      $response Response.
  * 
  * @return ResponseInterface The response.
  * 
  * @throws ApplicationException On request not found or method not allowed.
  */
 public function getResponse(ServerRequestInterface $request, ResponseInterface $response)
 {
     $method = $request->getMethod();
     $path = $request->getUri()->getPath();
     $routeInfo = $this->dispatcher->dispatch($method, $path);
     switch ($routeInfo[0]) {
         case FastRoute\Dispatcher::FOUND:
             break;
         case FastRoute\Dispatcher::NOT_FOUND:
             throw $this->createNotFoundException();
         case FastRoute\Dispatcher::METHOD_NOT_ALLOWED:
             throw $this->createMethodNotAllowedException();
         default:
             throw new \Exception('Unexpected dispatcher response');
     }
     foreach ($routeInfo[2] as $name => $value) {
         $request = $request->withAttribute($name, $value);
     }
     $service = $this->container->get($routeInfo[1]);
     if (!is_callable($service)) {
         throw new \Exception('Route handler is not callable');
     }
     return $service($request, $response);
 }
Ejemplo n.º 25
0
 private function finish()
 {
     $routeInfo = $this->dispatcher->dispatch($this->request->method(), $this->request->uri());
     switch ($routeInfo[0]) {
         case Dispatcher::NOT_FOUND:
             $this->response->code(404);
             $this->response->getLayout()->content = (new Controller())->error404();
             break;
         case Dispatcher::METHOD_NOT_ALLOWED:
             $allowedMethods = $routeInfo[1];
             $this->response->code(415);
             break;
         case Dispatcher::FOUND:
             $handler = explode('::', $routeInfo[1]);
             /** @var Controller $class */
             $class = $handler[0];
             $method = $handler[1];
             $vars = $routeInfo[2];
             $this->response->type($class::type());
             $this->response->getLayout()->content = call_user_func_array([new $class(), $method], $vars);
             break;
     }
     $this->response->output();
 }
Ejemplo n.º 26
0
 private function registeredHandlerTest(Dispatcher $dispatcher, $method, $route, $resultNum = Dispatcher::FOUND)
 {
     list($result) = $dispatcher->dispatch($method, $route);
     $this->assertEquals($resultNum, $result);
 }
Ejemplo n.º 27
0
 /**
  * @param \FastRoute\Dispatcher $dispatcher
  * @param string $method
  * @param string $path
  * @return array
  */
 protected function runDispatcher(FastDispatcher $dispatcher, $method, $path)
 {
     $routeInfo = $dispatcher->dispatch($method, $path);
     switch ($routeInfo[0]) {
         case FastDispatcher::NOT_FOUND:
             throw new RouteNotFoundException();
         case FastDispatcher::METHOD_NOT_ALLOWED:
             throw new MethodNotAllowedException();
         case FastDispatcher::FOUND:
             return $routeInfo;
     }
 }
Ejemplo n.º 28
0
 /**
  * Gets the "method not allowed (405)" route
  *
  * @param \FastRoute\Dispatcher $dispatcher The request dispatcher
  *
  * @return array The route
  */
 private function runMethodNotAllowed(Dispatcher $dispatcher) : array
 {
     return $dispatcher->dispatch('GET', '/method-not-allowed');
 }
Ejemplo n.º 29
0
 /**
  * @param Dispatcher $dispatcher
  * @param string $method
  * @param string $path
  * @return string
  */
 protected function runDispatcher(Dispatcher $dispatcher, $method, $path)
 {
     $routeInfo = $dispatcher->dispatch($method, $path);
     try {
         switch ($routeInfo[0]) {
             case Dispatcher::NOT_FOUND:
                 throw new HandlerNotFoundException();
             case Dispatcher::METHOD_NOT_ALLOWED:
                 throw new MethodNotAllowedException();
             case Dispatcher::FOUND:
                 return $routeInfo;
         }
     } catch (RuntimeException $e) {
         if (isset($routeInfo[1]) && in_array('*', $routeInfo[1])) {
             return $this->runDispatcher($dispatcher, '*', $path);
         } else {
             throw $e;
         }
     }
 }
Ejemplo n.º 30
0
 /**
  * Creates a request handler
  *
  * @param Dispatcher $dispatcher
  * @return callable
  */
 protected function requestHandler(Dispatcher $dispatcher)
 {
     return function (Request $req, Response $res) use($dispatcher) {
         try {
             $result = $dispatcher->dispatch($req->getMethod(), $req->getPath());
             switch ($result[0]) {
                 case Dispatcher::FOUND:
                     // @todo param handling -> result 2
                     $this->runNext(array_merge($this->middlewares, (array) $result[1]), $req, $res);
                     break;
                 case Dispatcher::METHOD_NOT_ALLOWED:
                 case Dispatcher::NOT_FOUND:
                     $res->writeHead(404);
                     $res->end();
                     break;
             }
         } catch (\Exception $e) {
             $this->emit('error', [$e, $req, $res]);
         }
     };
 }