Esempio n. 1
0
 /**
  * @param Route $route
  */
 public function addRoute(Route $route)
 {
     $path = $route->getPath();
     $options = $route->getOptions() ?: [];
     $options = array_replace_recursive($options, ['route' => $route->getPath(), 'defaults' => ['middleware' => $route->getMiddleware()]]);
     $spec = ['type' => 'segment', 'options' => $options];
     $this->zf2Router->addRoute($path, $spec);
     $this->routes[$path] = $route;
 }
 /**
  * {@inheritdoc}
  */
 public function addRoute(Route $route)
 {
     // TODO allow named parameters through options.
     // TODO allow usage of :controller, :action, etc.
     // TODO allow using prefixes
     if ($this->router->wasMatched()) {
         throw new Exception\RuntimeException('Route was already matched.');
     }
     // Necessary for phalcon not to alter the original middleware.
     $middleware = $route->getMiddleware() . '\\MockController::mockAction';
     $r = $this->router->add($route->getPath(), $middleware, $route->getAllowedMethods());
     $r->setName($route->getName());
 }
Esempio n. 3
0
 /**
  * Add a route to the underlying router.
  *
  * Adds the route to the Aura.Router, using the path as the name, and a
  * middleware value equivalent to the middleware in the Route instance.
  *
  * If HTTP methods are defined (and not the wildcard), they are imploded
  * with a pipe symbol and added as server REQUEST_METHOD criteria.
  *
  * If tokens or values are present in the options array, they are also
  * added to the router.
  *
  * @param Route $route
  */
 public function addRoute(Route $route)
 {
     $auraRoute = $this->router->add($route->getPath(), $route->getPath(), $route->getMiddleware());
     $httpMethods = $route->getAllowedMethods();
     if (is_array($httpMethods)) {
         $auraRoute->setServer(['REQUEST_METHOD' => implode('|', $httpMethods)]);
     }
     foreach ($route->getOptions() as $key => $value) {
         switch ($key) {
             case 'tokens':
                 $auraRoute->addTokens($value);
                 break;
             case 'values':
                 $auraRoute->addValues($value);
                 break;
         }
     }
 }
 /**
  * @param Route $route
  */
 public function addRoute(Route $route)
 {
     $slimRoute = new \Slim\Route($route->getPath(), [$this, 'dummyCallable']);
     $slimRoute->setName($route->getName());
     $allowedMethods = $route->getAllowedMethods();
     $slimRoute->via($allowedMethods === Route::HTTP_METHOD_ANY ? 'ANY' : $allowedMethods);
     // Process options
     $options = $route->getOptions();
     if (isset($options['conditions']) && is_array($options['conditions'])) {
         $slimRoute->setConditions($options['conditions']);
     }
     // The middleware is merged with the rest of the route params
     $params = ['middleware' => $route->getMiddleware()];
     if (isset($options['defaults']) && is_array($options['defaults'])) {
         $params = array_merge($options['defaults'], $params);
     }
     $slimRoute->setParams($params);
     $this->router->map($slimRoute);
 }
Esempio n. 5
0
 /**
  * Add a route to the underlying router.
  *
  * Adds the route to the Aura.Router, using the path as the name, and a
  * middleware value equivalent to the middleware in the Route instance.
  *
  * If HTTP methods are defined (and not the wildcard), they are imploded
  * with a pipe symbol and added as server REQUEST_METHOD criteria.
  *
  * If tokens or values are present in the options array, they are also
  * added to the router.
  *
  * @param Route $route
  */
 public function addRoute(Route $route)
 {
     $path = $route->getPath();
     $auraRoute = $this->router->add($route->getName(), $path, $route->getMiddleware());
     foreach ($route->getOptions() as $key => $value) {
         switch ($key) {
             case 'tokens':
                 $auraRoute->addTokens($value);
                 break;
             case 'values':
                 $auraRoute->addValues($value);
                 break;
         }
     }
     $allowedMethods = $route->getAllowedMethods();
     if (Route::HTTP_METHOD_ANY === $allowedMethods) {
         return;
     }
     $auraRoute->setServer(['REQUEST_METHOD' => implode('|', $allowedMethods)]);
     if (array_key_exists($path, $this->routes)) {
         $allowedMethods = array_merge($this->routes[$path], $allowedMethods);
     }
     $this->routes[$path] = $allowedMethods;
 }
Esempio n. 6
0
 public function testRouteMiddlewareMayBeANonCallableString()
 {
     $route = new Route('/foo', 'Application\\Middleware\\HelloWorld');
     $this->assertSame('Application\\Middleware\\HelloWorld', $route->getMiddleware());
 }
Esempio n. 7
0
 /**
  * Create route configuration for matching one or more HTTP methods.
  *
  * @param Route $route
  * @return array
  */
 private function createHttpMethodRoute($route)
 {
     return ['type' => 'method', 'options' => ['verb' => implode(',', $route->getAllowedMethods()), 'defaults' => ['middleware' => $route->getMiddleware()]]];
 }
 /**
  * Inject route into the underlying router implemetation.
  *
  * @param Route $route
  */
 private function injectRoute(Route $route)
 {
     $name = $route->getName();
     $path = $route->getPath();
     $options = $route->getOptions();
     $options = array_replace_recursive($options, ['route' => $path, 'defaults' => ['middleware' => $route->getMiddleware()]]);
     $allowedMethods = $route->getAllowedMethods();
     if (Route::HTTP_METHOD_ANY === $allowedMethods) {
         $this->zendRouter->addRoute($name, ['type' => 'segment', 'options' => $options]);
         $this->routeNameMap[$name] = $name;
         return;
     }
     // Remove the middleware from the segment route in favor of method route
     unset($options['defaults']['middleware']);
     if (empty($options['defaults'])) {
         unset($options['defaults']);
     }
     $httpMethodRouteName = implode(':', $allowedMethods);
     $httpMethodRoute = $this->createHttpMethodRoute($route);
     $methodNotAllowedRoute = $this->createMethodNotAllowedRoute($path);
     $spec = ['type' => 'segment', 'options' => $options, 'may_terminate' => false, 'child_routes' => [$httpMethodRouteName => $httpMethodRoute, self::METHOD_NOT_ALLOWED_ROUTE => $methodNotAllowedRoute]];
     if (array_key_exists($path, $this->allowedMethodsByPath)) {
         $allowedMethods = array_merge($this->allowedMethodsByPath[$path], $allowedMethods);
         // Remove the method not allowed route as it is already present for the path
         unset($spec['child_routes'][self::METHOD_NOT_ALLOWED_ROUTE]);
     }
     $this->zendRouter->addRoute($name, $spec);
     $this->allowedMethodsByPath[$path] = $allowedMethods;
     $this->routeNameMap[$name] = sprintf('%s/%s', $name, $httpMethodRouteName);
 }