/**
  * Get the route information for a given route.
  *
  * @param  Route $route
  * @return array
  */
 protected function getRouteInformation(Route $route)
 {
     $result = [];
     $result['uri'] = $route->getMethods()[0] . ' ' . $route->getPattern();
     $result['name'] = $route->getName() ?: '';
     $result['group'] = '';
     foreach ($route->getGroups() as $group) {
         $result['group'] .= $group->getPattern();
     }
     $callable = $route->getCallable();
     $result['middleware'] = [];
     foreach ($route->getMiddleware() as $middleware) {
         $result['middleware'][] = Closure::bind(function () {
             return get_class($this->callable);
         }, $middleware, DeferredCallable::class)->__invoke();
     }
     if (is_array($callable)) {
         $result['callable'] = get_class($callable[0]) . ':' . $callable[1];
         $reflector = new ReflectionMethod($callable[0], $callable[1]);
     } elseif ($callable instanceof Closure) {
         $result['callable'] = $this->formatVar($callable);
         $reflector = new ReflectionFunction($callable);
     } elseif (is_object($callable)) {
         $result['callable'] = get_class($callable);
         $reflector = new ReflectionMethod($callable, '__invoke');
     } else {
         $result['callable'] = $callable;
         $callable = explode(':', $callable);
         if (isset($callable[1])) {
             $reflector = new ReflectionMethod($callable[0], $callable[1]);
         } else {
             $reflector = new ReflectionMethod($callable, '__invoke');
         }
     }
     $result['file'] = $reflector->getFileName() . ':' . $reflector->getStartLine() . '-' . $reflector->getEndLine();
     return $result;
 }
Example #2
0
 /**
  * Add a route object to the router
  * @param  \Slim\Route     $route      The Slim Route
  */
 public function map(\Slim\Route $route)
 {
     list($groupPattern, $groupMiddleware) = $this->processGroups();
     $route->setPattern($groupPattern . $route->getPattern());
     $this->routes[] = $route;
     foreach ($groupMiddleware as $middleware) {
         $route->setMiddleware($middleware);
     }
 }
Example #3
0
 /**
  * Dispatch route
  *
  * This method invokes the route object's callable. If middleware is
  * registered for the route, each callable middleware is invoked in
  * the order specified.
  *
  * This method is smart about trailing slashes on the route pattern.
  * If the route's pattern is defined with a trailing slash, and if the
  * current request URI does not have a trailing slash but otherwise
  * matches the route's pattern, a Slim_Exception_RequestSlash
  * will be thrown triggering an HTTP 301 Permanent Redirect to the same
  * URI _with_ a trailing slash. This Exception is caught in the
  * `Slim::call` loop. If the route's pattern is defined without a
  * trailing slash, and if the current request URI does have a trailing
  * slash, the route will not be matched and a 404 Not Found
  * response will be sent if no subsequent matching routes are found.
  *
  * @param  \Slim\Route                  $route  The route object
  * @return bool                         Was route callable invoked successfully?
  * @throws \Slim\Exception\RequestSlash
  */
 public function dispatch(\Slim\Route $route)
 {
     if (substr($route->getPattern(), -1) === '/' && substr($this->resourceUri, -1) !== '/') {
         throw new Exception\RequestSlash();
     }
     //Invoke middleware
     foreach ($route->getMiddleware() as $mw) {
         if (is_callable($mw)) {
             call_user_func_array($mw, array($route));
         }
     }
     //Invoke callable
     if (is_callable($route->getCallable())) {
         call_user_func_array($route->getCallable(), array_values($route->getParams()));
         return true;
     }
     return false;
 }