Esempio n. 1
0
function routeRequest(Response $response, Request $request)
{
    $routesDefinitionsCallback = function (RouteCollector $r) {
        foreach (require __DIR__ . "/routes.php" as $route) {
            $r->addRoute($route[0], $route[1], $route[2]);
        }
    };
    $dispatcher = \FastRoute\simpleDispatcher($routesDefinitionsCallback);
    $routeInfo = $dispatcher->dispatch($request->getMethod(), $request->getPath());
    switch ($routeInfo[0]) {
        case Dispatcher::NOT_FOUND:
            $response->setStatusCode(404);
            $response->setContent('404 - not found');
            break;
        case Dispatcher::METHOD_NOT_ALLOWED:
            $response->setStatusCode(403);
            $response->setContent('403 - not allowed');
            break;
        case Dispatcher::FOUND:
            $response->setStatusCode(200);
            $className = $routeInfo[1][0];
            $method = $routeInfo[1][1];
            $vars = $routeInfo[2];
            return new Step("{$className}::{$method}", InjectionParams::fromRouteParams($vars));
    }
    return new step(function () {
        echo "something went wrong";
    });
}
 /**
  * Find the patterned filters matching a request.
  *
  * @param  \Http\Request  $request
  * @return array
  */
 public function findPatternFilters($request)
 {
     $results = array();
     list($path, $method) = array($request->path(), $request->getMethod());
     foreach ($this->patternFilters as $pattern => $filters) {
         if (str_is($pattern, $path)) {
             $merge = $this->patternsByMethod($method, $filters);
             $results = array_merge($results, $merge);
         }
     }
     foreach ($this->regexFilters as $pattern => $filters) {
         if (preg_match($pattern, $path)) {
             $merge = $this->patternsByMethod($method, $filters);
             $results = array_merge($results, $merge);
         }
     }
     return $results;
 }
 /**
  * Determine if any routes match on another HTTP verb.
  *
  * @param  \Http\Request  $request
  * @return array
  */
 protected function checkForAlternateMethods($request)
 {
     $methods = array_diff(Router::$methods, array($request->getMethod()));
     //
     $others = array();
     foreach ($methods as $method) {
         if (!is_null($this->check($this->get($method), $request, false))) {
             $others[] = $method;
         }
     }
     return $others;
 }
 /**
  * Determine if the filter fails the "on" constraint.
  *
  * @param  array  $filter
  * @param  \Http\Request  $request
  * @param  string  $method
  * @return bool
  */
 protected function filterFailsOn($filter, $request, $method)
 {
     $on = array_get($filter, 'options.on');
     if (is_null($on)) {
         return false;
     }
     if (is_string($on)) {
         $on = explode('|', $on);
     }
     return !in_array(strtolower($request->getMethod()), $on);
 }
 /**
  * Validate a given rule against a route and request.
  *
  * @param  \Routing\Route  $route
  * @param  \Http\Request  $request
  * @return bool
  */
 public function matches(Route $route, Request $request)
 {
     return in_array($request->getMethod(), $route->methods());
 }