Exemple #1
0
 public function testExplodeUrl()
 {
     $url = '      /controller/action////id/1////';
     $right = ['controller', 'action', 'id', '1'];
     $result = \Cloudstash\Point\Helper\Routing::explodeUrl($url);
     $result = \Cloudstash\Helper\Arr::similar($right, $result, false);
     $this->assertTrue($result, 'Wrong URL explode');
     $url = '     /     ';
     $right = [];
     $result = \Cloudstash\Point\Helper\Routing::explodeUrl($url);
     $result = \Cloudstash\Helper\Arr::similar($right, $result, false);
     $this->assertTrue($result, 'Wrong empty URL explode');
 }
Exemple #2
0
 /**
  * @param string $url
  * @return bool
  */
 public function isCurrent($url)
 {
     $uriArray = Routing::explodeUrl($url);
     if (count($this->pattern_strict) != count($uriArray)) {
         return false;
     }
     foreach ($this->pattern_strict as $index => $segment) {
         $partial = Arr::get($uriArray, $index, null);
         if (is_null($partial)) {
             return false;
         }
         $type = Arr::get($segment, 'type', self::TYPE_BLOCK);
         $matcher = Arr::get($segment, 'matcher', null);
         $name = Arr::get($segment, 'name', "var{$index}");
         // block must be strong assert with url partial
         if ($type == self::TYPE_BLOCK) {
             if ($partial == $matcher) {
                 continue;
             }
             return false;
         }
         // if it is variable with callable filter
         if (is_callable($matcher)) {
             if (call_user_func_array($matcher, [$partial])) {
                 $this->values[$name] = $partial;
                 continue;
             }
             return false;
         }
         // if not callable then just set to variable all segment value
         if ($type == self::TYPE_VARIABLE) {
             $this->values[$name] = $partial;
             continue;
         }
         return false;
     }
     return true;
 }
Exemple #3
0
 /**
  * @param string $name
  * @param array|string $method
  * @param Route|string $pattern
  * @param string|callback $handler
  * @return $this
  */
 public function register($name, $pattern, $handler, $method = ['GET', 'POST'])
 {
     $pattern = Routing::explodeUrl($pattern);
     if ($pattern instanceof Route) {
         $route = $pattern;
     } else {
         $route = new Route($handler);
         foreach ($pattern as $segment) {
             if (preg_match(self::PREG_VARIABLE, $segment, $matches)) {
                 $var = Arr::get($matches, 1);
                 $value = Arr::get($matches, 3);
                 $route->registerVar($var, Arr::get($this->filters, $value, $value));
                 continue;
             }
             $route->registerBlock($segment);
         }
     }
     $this->collection[$name] = ['method' => $this->prepareMethod($method), 'route' => $route];
     return $this;
 }