Пример #1
0
 public function testCanSpecifyRouteOptions()
 {
     $route = new Route('/foo/:id', 'foo', ['GET']);
     $route->setOptions(['constraints' => ['id' => '\\d+'], 'defaults' => ['bar' => 'baz']]);
     $this->zf2Router->addRoute('/foo/:id', ['type' => 'segment', 'options' => ['route' => '/foo/:id', 'constraints' => ['id' => '\\d+'], 'defaults' => ['bar' => 'baz', 'middleware' => 'foo']]])->shouldBeCalled();
     $router = $this->getRouter();
     $router->addRoute($route);
 }
 public function testAddRouteWithOptions()
 {
     $route = new Route('/foo/:bar', 'Home', ['GET', 'POST'], 'home');
     $route->setOptions(['conditions' => ['bar' => 'es|en'], 'defaults' => ['bar' => 'en']]);
     $this->router->addRoute($route);
     $this->assertCount(1, $this->slimRouter->getMatchedRoutes('GET', '/foo/es'));
     $this->assertCount(0, $this->slimRouter->getMatchedRoutes('GET', '/foo/baz', true));
 }
Пример #3
0
 public function testCanSpecifyRouteOptions()
 {
     $route = new Route('/foo/:id', 'foo', ['GET']);
     $route->setOptions(['constraints' => ['id' => '\\d+'], 'defaults' => ['bar' => 'baz']]);
     $this->zf2Router->addRoute('/foo/:id^GET', ['type' => 'segment', 'options' => ['route' => '/foo/:id', 'constraints' => ['id' => '\\d+'], 'defaults' => ['bar' => 'baz']], 'may_terminate' => false, 'child_routes' => ['GET' => ['type' => 'method', 'options' => ['verb' => 'GET', 'defaults' => ['middleware' => 'foo']]], Zf2Router::METHOD_NOT_ALLOWED_ROUTE => ['type' => 'regex', 'priority' => -1, 'options' => ['regex' => '/*$', 'defaults' => [Zf2Router::METHOD_NOT_ALLOWED_ROUTE => '/foo/:id'], 'spec' => '']]]])->shouldBeCalled();
     $router = $this->getRouter();
     $router->addRoute($route);
 }
Пример #4
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;
 }
Пример #5
0
 public function testCanSpecifyAuraRouteValuesViaRouteOptions()
 {
     $route = new Route('/foo', 'foo', ['GET']);
     $route->setOptions(['values' => ['foo' => 'bar']]);
     $this->auraRoute->setServer(['REQUEST_METHOD' => 'GET'])->shouldBeCalled();
     $this->auraRoute->addValues($route->getOptions()['values'])->shouldBeCalled();
     $this->auraRouter->add('/foo^GET', '/foo', 'foo')->willReturn($this->auraRoute->reveal());
     $router = $this->getRouter();
     $router->addRoute($route);
 }
Пример #6
0
 /**
  * Add a route to the collection.
  *
  * Uses the HTTP methods associated (creating sane defaults for an empty
  * list or Route::HTTP_METHOD_ANY) and the path, and uses the path as
  * the name (to allow later lookup of the middleware).
  *
  * @param Route $route
  */
 public function addRoute(Route $route)
 {
     $methods = $route->getAllowedMethods();
     if ($methods === Route::HTTP_METHOD_ANY) {
         $methods = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS', 'TRACE'];
     }
     if (empty($methods)) {
         $methods = ['GET', 'HEAD', 'OPTIONS'];
     }
     $this->router->addRoute($methods, $route->getPath(), $route->getPath());
     $this->routes[$route->getName()] = $route;
 }
Пример #7
0
 /**
  * Add a route.
  *
  * This method adds a route against which the underlying implementation may
  * match. Implementations MUST aggregate route instances, but MUST NOT use
  * the details to inject the underlying router until `match()` and/or
  * `generateUri()` is called.  This is required to allow consumers to
  * modify route instances before matching (e.g., to provide route options,
  * inject a name, etc.).
  *
  * The method MUST raise Exception\RuntimeException if called after either `match()`
  * or `generateUri()` have already been called, to ensure integrity of the
  * router between invocations of either of those methods.
  *
  * @param Route $route
  * @throws Exception\RuntimeException when called after match() or
  *     generateUri() have been called.
  */
 public function addRoute(Route $route)
 {
     if ($this->_forbidRouteAdd) {
         throw new Exception('called after match() or generateUri() have been called');
     }
     $routeName = $route->getName();
     // currently allowing all methods
     #$methods = $route->getAllowedMethods();
     if (isset($this->_routes[$routeName])) {
         throw new \Exception('Route already defined');
     }
     $this->_routes[$routeName] = $route;
 }
Пример #8
0
 /**
  * {@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());
 }
 /**
  * Inject routes from configuration, if any.
  *
  * @param Application $app
  * @param ContainerInterface $container
  */
 private function injectRoutes(Application $app, ContainerInterface $container)
 {
     $config = $container->has('config') ? $container->get('config') : [];
     if (!isset($config['routes'])) {
         $app->pipeRoutingMiddleware();
         return;
     }
     foreach ($config['routes'] as $spec) {
         if (!isset($spec['path']) || !isset($spec['middleware'])) {
             continue;
         }
         $methods = isset($spec['allowed_methods']) && is_array($spec['allowed_methods']) ? $spec['allowed_methods'] : null;
         $name = isset($spec['name']) ? $spec['name'] : null;
         $methods = null === $methods ? Route::HTTP_METHOD_ANY : $methods;
         $route = new Route($spec['path'], $spec['middleware'], $methods, $name);
         if (isset($spec['options']) && is_array($spec['options'])) {
             $route->setOptions($spec['options']);
         }
         $app->route($route);
     }
 }
Пример #10
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;
 }
Пример #11
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;
         }
     }
 }
Пример #12
0
 /**
  * @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);
 }
 /**
  * Inject routes from configuration, if any.
  *
  * @param array $routes Route definitions
  * @param Application $app
  */
 private function injectRoutes(array $routes, Application $app)
 {
     foreach ($routes as $spec) {
         if (!isset($spec['path']) || !isset($spec['middleware'])) {
             continue;
         }
         if (isset($spec['allowed_methods'])) {
             $methods = $spec['allowed_methods'];
             if (!is_array($methods)) {
                 throw new ContainerInvalidArgumentException(sprintf('Allowed HTTP methods for a route must be in form of an array; received "%s"', gettype($methods)));
             }
         } else {
             $methods = Route::HTTP_METHOD_ANY;
         }
         $name = isset($spec['name']) ? $spec['name'] : null;
         $route = new Route($spec['path'], $spec['middleware'], $methods, $name);
         if (isset($spec['options'])) {
             $options = $spec['options'];
             if (!is_array($options)) {
                 throw new ContainerInvalidArgumentException(sprintf('Route options must be an array; received "%s"', gettype($options)));
             }
             $route->setOptions($options);
         }
         $app->route($route);
     }
 }
Пример #14
0
 public function testRouteNameWithGetAndPost()
 {
     $route = new Route('/test', $this->noopMiddleware, ['GET', 'POST']);
     $this->assertSame('/test^GET' . Route::HTTP_METHOD_SEPARATOR . 'POST', $route->getName());
 }
Пример #15
0
 public function testRouteOptionsAreEmptyByDefault()
 {
     $route = new Route('/foo', $this->noopMiddleware);
     $this->assertSame([], $route->getOptions());
 }
Пример #16
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()]]];
 }
Пример #17
0
 /**
  * Injects route into the underlying router implemetation.
  *
  * @param Route $route
  */
 protected function injectRoute(Route $route)
 {
     $pathParser = new Segment('/', $route->getPath(), []);
     $options = $route->getOptions();
     $defaults = isset($options['defaults']) ? $options['defaults'] : [];
     $methods = Route::HTTP_METHOD_ANY === $route->getAllowedMethods() ? null : $route->getAllowedMethods();
     $this->routeCollection->add($route->getName(), new Generic($pathParser, null, $methods, null, null, $defaults));
 }
 /**
  * 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);
 }
 /**
  * Inject routes from configuration, if any.
  *
  * @param Application $app
  * @param ContainerInterface $container
  */
 private function injectRoutes(Application $app, ContainerInterface $container)
 {
     $config = $container->has('config') ? $container->get('config') : [];
     if (!isset($config['routes'])) {
         $app->pipeRoutingMiddleware();
         return;
     }
     foreach ($config['routes'] as $spec) {
         if (!isset($spec['path']) || !isset($spec['middleware'])) {
             continue;
         }
         if (isset($spec['allowed_methods'])) {
             $methods = $spec['allowed_methods'];
             if (!is_array($methods)) {
                 throw new ContainerInvalidArgumentException(sprintf('Allowed HTTP methods for a route must be in form of an array; received "%s"', gettype($methods)));
             }
         } else {
             $methods = Route::HTTP_METHOD_ANY;
         }
         $name = isset($spec['name']) ? $spec['name'] : null;
         $route = new Route($spec['path'], $spec['middleware'], $methods, $name);
         if (isset($spec['options'])) {
             $options = $spec['options'];
             if (!is_array($options)) {
                 throw new ContainerInvalidArgumentException(sprintf('Route options must be an array; received "%s"', gettype($options)));
             }
             $route->setOptions($options);
         }
         $app->route($route);
     }
 }