Base::mask PHP Method

mask() public method

Applies the specified URL mask and returns parameterized matches
public mask ( $pattern, $url = NULL )
$pattern string
$url string|NULL
    function mask($pattern, $url = NULL)
    {
        if (!$url) {
            $url = $this->rel($this->hive['URI']);
        }
        $case = $this->hive['CASELESS'] ? 'i' : '';
        $wild = preg_quote($pattern, '/');
        $i = 0;
        while (is_int($pos = strpos($wild, '\\*'))) {
            $wild = substr_replace($wild, '(?P<_' . $i . '>[^\\?]*)', $pos, 2);
            $i++;
        }
        preg_match('/^' . preg_replace('/((\\\\{)?@(\\w+\\b)(?(2)\\\\}))/', '(?P<\\3>[^\\/\\?]+)', $wild) . '\\/?$/' . $case . 'um', $url, $args);
        foreach (array_keys($args) as $key) {
            if (preg_match('/_\\d+/', $key)) {
                if (empty($args['*'])) {
                    $args['*'] = $args[$key];
                } else {
                    if (is_string($args['*'])) {
                        $args['*'] = [$args['*']];
                    }
                    array_push($args['*'], $args[$key]);
                }
                unset($args[$key]);
            } elseif (is_numeric($key) && $key) {
                unset($args[$key]);
            }
        }
        return $args;
    }

Usage Example

コード例 #1
0
ファイル: middleware.php プロジェクト: Kekesed/Kambeng-Blog
 public function run($event = 'before')
 {
     if (!isset($this->routes[$event])) {
         return true;
     }
     foreach ($keys = array_keys($this->routes[$event]) as $key) {
         $paths[] = str_replace('@', '*@', $key);
     }
     $vals = array_values($this->routes[$event]);
     array_multisort($paths, SORT_DESC, $keys, $vals);
     $this->routes[$event] = array_combine($keys, $vals);
     // Convert to BASE-relative URL
     $req = $this->f3->rel(urldecode($this->f3->URI));
     foreach ($this->routes[$event] as $pattern => $routes) {
         if (!($args = $this->f3->mask($pattern, $req))) {
             continue;
         }
         ksort($args);
         $route = NULL;
         if (isset($routes[$ptr = $this->f3->AJAX + 1][$this->f3->VERB])) {
             $route = $routes[$ptr];
         } elseif (isset($routes[\Base::REQ_SYNC | \Base::REQ_AJAX])) {
             $route = $routes[\Base::REQ_SYNC | \Base::REQ_AJAX];
         }
         if (!$route) {
             continue;
         }
         if ($this->f3->VERB != 'OPTIONS' && isset($route[$this->f3->VERB])) {
             $parts = parse_url($req);
             if ($this->f3->VERB == 'GET' && preg_match('/.+\\/$/', $parts['path'])) {
                 $this->f3->reroute(substr($parts['path'], 0, -1) . (isset($parts['query']) ? '?' . $parts['query'] : ''));
             }
             $handler = $route[$this->f3->VERB][0];
             if (is_bool(strpos($pattern, '/*'))) {
                 foreach (array_keys($args) as $key) {
                     if (is_numeric($key) && $key) {
                         unset($args[$key]);
                     }
                 }
             }
             if (is_string($handler)) {
                 // Replace route pattern tokens in handler if any
                 $handler = preg_replace_callback('/@(\\w+\\b)/', function ($id) use($args) {
                     return isset($args[$id[1]]) ? $args[$id[1]] : $id[0];
                 }, $handler);
                 if (preg_match('/(.+)\\h*(?:->|::)/', $handler, $match) && !class_exists($match[1])) {
                     $this->f3->error(500, 'PreRoute handler not found');
                 }
             }
             // Call route handler
             return $this->f3->call($handler, array($this->f3, $args), 'beforeroute,afterroute') !== FALSE;
         }
     }
     return true;
 }