/**
  * Validate http method or verb.
  *
  * @param Route          $route
  * @param RequestContext $request
  *
  * @return bool
  */
 public function match(Route $route, RequestContext $request)
 {
     if (0 === $route->getMethod()->count()) {
         return true;
     }
     return $route->getMethod()->has($request->getMethod());
 }
 /**
  * Validate host if exists.
  *
  * @param Route          $route
  * @param RequestContext $request
  *
  * @return bool
  */
 public function match(Route $route, RequestContext $request)
 {
     if (false === $route->getDomain()->hasDomain()) {
         return true;
     }
     return (string) $route->getDomain() === $request->getHost();
 }
 /**
  * Push route.
  *
  * @param string $name
  * @param array  $config
  */
 private function addRoute($name, array $config)
 {
     $objRoute = new Route($config['path'], $config['defaults'], $config['requirements'], $config['methods'], $config['controller'], $config['schemas'], $config['domain']);
     if (isset($config['middlewares'])) {
         $objRoute->setBag(new ParameterBag('middlewares', (array) $config['middlewares']));
     }
     $this->add($name, $objRoute);
 }
 /**
  * Build RouteRegex from Route.
  *
  * @param Route $route
  *
  * @return RouteRegex
  */
 public static function create(Route $route)
 {
     $matches = array();
     $tokens = array();
     $variables = array();
     $pattern = $route->getPath();
     $pos = 0;
     // match all variables between variable prefix and suffix.
     preg_match_all(self::getVariablePattern(), $pattern, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
     foreach ((array) $matches as $i => $match) {
         $variable = substr($match[0][0], 1, -1);
         $preceding = substr($pattern, $pos, $match[0][1] - $pos);
         $pos = $match[0][1] + strlen($match[0][0]);
         $precedingChar = strlen($preceding) > 0 ? substr($preceding, -1) : '';
         $isSeparator = '' !== $precedingChar && false !== strpos(static::SEPARATORS, $precedingChar);
         if (is_numeric($variable)) {
             throw new InvalidArgumentException(sprintf('Variable name "%s" must be string.', $variable));
         }
         if (in_array($variable, $variables)) {
             throw new InvalidArgumentException(sprintf('Variable name "%s" can\'t define more than once.', $variable));
         }
         if ($isSeparator && strlen($preceding) > 1) {
             $tokens[] = array('type' => 'text', 'path' => substr($preceding, 0, -1));
         } elseif (!$isSeparator && strlen($preceding) > 0) {
             $tokens[] = array('type' => 'text', 'path' => $preceding);
         }
         $regex = $route->getRequirementBag()->get($variable);
         if (null === $regex) {
             $regex = '[^/]++';
         }
         $tokens[] = array('type' => 'variable', 'preceding' => $isSeparator ? $precedingChar : '', 'regex' => $regex, 'variable' => $variable);
         $variables[] = $variable;
     }
     if ($pos < strlen($pattern)) {
         $tokens[] = array('type' => 'text', 'path' => substr($pattern, $pos));
     }
     $numberOfToken = count($tokens);
     $lastToken = $tokens[$numberOfToken - 1];
     $optionalIndex = -1;
     if ('variable' === $lastToken['type'] && $route->getDefaultBag()->has($lastToken['variable'])) {
         $optionalIndex = $numberOfToken - 1;
     }
     $regex = '';
     foreach ($tokens as $i => $token) {
         $regex .= self::buildRegex($token, $i, $optionalIndex);
     }
     return new RouteRegex(sprintf('%s^%s$%ss', static::$regexDelimiter, $regex, static::$regexDelimiter), $variables, $tokens);
 }
 public function testBasicRouteRegex()
 {
     $collection = new RouteCollection();
     $collection->add('category', new Route('/category/{name}'));
     $collection->add('page', Route::create('/page/{page}.{ext}')->setRequirement('ext', 'htm|html')->setRequirement('page', '[a-z]+'));
     $collection->add('profile', Route::create('/profile/{username}')->setDefault('username', 'me'));
     $collection->add('read', RouteFactory::createHttpGet('/read/{slug}')->setRequirement('slug', '[a-z\\-]+'));
     $this->assertTrue((bool) preg_match($collection->get('category')->getRegex()->getRegexPattern(), '/category/music'));
     $this->assertTrue((bool) preg_match($collection->get('page')->getRegex()->getRegexPattern(), '/page/index.html'));
     $this->assertTrue((bool) preg_match($collection->get('page')->getRegex()->getRegexPattern(), '/page/index.htm'));
     $this->assertTrue((bool) preg_match($collection->get('page')->getRegex()->getRegexPattern(), '/page/index05.html') === false);
     $this->assertTrue((bool) preg_match($collection->get('page')->getRegex()->getRegexPattern(), '/page/index.php') === false);
     $this->assertTrue((bool) preg_match($collection->get('profile')->getRegex()->getRegexPattern(), '/profile') === true);
     $this->assertTrue((bool) preg_match($collection->get('profile')->getRegex()->getRegexPattern(), '/profile/danu') === true);
     $this->assertTrue((bool) preg_match($collection->get('read')->getRegex()->getRegexPattern(), '/read/welcome-to-the-jungle') === true);
     $this->assertTrue((bool) preg_match($collection->get('read')->getRegex()->getRegexPattern(), '/read/WELCOME-TO-THE') === false);
 }
 public function testRouteHandler()
 {
     $handler = function () {
         return "welcome";
     };
     $collection = new RouteCollection();
     $route = Route::create('/view/{id}');
     $route->getDefaultBag()->set('id', 5);
     $route->getRequirementBag()->set('id', '\\d+');
     $route->getMethod()->replace(array('GET'));
     $route->handleWith($handler);
     $collection->add('view', $route);
     $this->assertArrayHasKey('view', $collection->all());
     $this->assertTrue($collection->get('view')->getRequirementBag()->has('id'));
     $this->assertTrue($collection->get('view')->getDefaultBag()->has('id'));
     $this->assertTrue($collection->get('view')->getMethod()->has('GET'));
     $this->assertTrue($collection->get('view')->getHandler() === $handler);
     $this->assertTrue(call_user_func($collection->get('view')->getHandler()) === 'welcome');
 }
 /**
  * Validate schema.
  *
  * @param Route          $route
  * @param RequestContext $request
  *
  * @return bool
  */
 public function match(Route $route, RequestContext $request)
 {
     return $route->getSchema()->has($request->getSchema());
 }
 /**
  * Handle route request on router.
  *
  * @param Route|null     $route
  * @param RequestContext $request
  * @param Router         $router
  * @param RequestContext $previousRequest
  */
 public function handleRouteRequest(Route $route = null, RequestContext $request, Router $router, RequestContext $previousRequest = null)
 {
     $caller = \Closure::bind($route->getHandler(), null);
     $stmt = $caller($request->getParameter('slug'));
     $this->assertTrue(in_array($stmt, ['Page: welcome-nerd', 'Page: hello-world']));
 }
 /**
  * Validate given path pattern.
  *
  * @param Route          $route
  * @param RequestContext $request
  *
  * @return bool
  */
 public function match(Route $route, RequestContext $request)
 {
     return (bool) preg_match($route->getRegex()->getRegexPattern(), rawurldecode($request->getPath()));
 }
 /**
  * Get middleware bag.
  *
  * @param Route $route
  *
  * @return ParameterBagInterface|null
  */
 private function getMiddlewareBag(Route $route)
 {
     if ($route->hasBag('middlewares') && null !== ($bag = $route->getBag('middlewares'))) {
         return $bag;
     }
     return null;
 }
 /**
  * Add new route and append the prefix.
  *
  * @param string $name
  * @param Route  $route
  *
  * @return Route
  */
 public function add($name, Route $route)
 {
     // replacing route will not place the new route at end of collection
     // so we need to remove it first
     unset($this->routes[$name]);
     $path = '/' !== $route->getPath() ? trim($route->getPath(), '/') : ltrim($route->getPath(), '/');
     $this->routes[$name] = $route;
     $this->routes[$name]->setPath(sprintf('%s/%s', $this->prefix, $path));
     if (null !== $this->domain && !empty($this->domain)) {
         $this->routes[$name]->setDomain($this->domain);
     }
     if (count($this->schemas) > 0) {
         $this->routes[$name]->getSchema()->replace($this->schemas);
     }
     return $route->setParent($this);
 }