lithium\net\http\Route::compile PHP Method

compile() public method

Compiles URL templates into regular expression patterns for matching against request URLs, and extracts template parameters into match-parameter arrays.
public compile ( ) : void
return void
    public function compile()
    {
        foreach ($this->_params as $key => $value) {
            if (!strpos($key, ':')) {
                continue;
            }
            unset($this->_params[$key]);
            $this->_meta[$key] = $value;
        }
        $this->_match = $this->_params;
        if ($this->_template === '/' || $this->_template === '') {
            $this->_pattern = '@^/*$@';
            return;
        }
        $this->_pattern = "@^{$this->_template}\$@";
        $match = '@([/.])?\\{:([^:}]+):?((?:[^{]+?(?:\\{[0-9,]+\\})?)*?)\\}@S';
        if ($this->_config['unicode']) {
            $this->_pattern .= 'u';
        }
        preg_match_all($match, $this->_pattern, $m);
        if (!($tokens = $m[0])) {
            return;
        }
        $slashes = $m[1];
        $params = $m[2];
        $regexs = $m[3];
        unset($m);
        $this->_keys = array();
        foreach ($params as $i => $param) {
            $this->_keys[$param] = $param;
            $this->_pattern = $this->_regex($regexs[$i], $param, $tokens[$i], $slashes[$i]);
        }
        $this->_defaults += array_intersect_key($this->_params, $this->_keys);
        $this->_match = array_diff_key($this->_params, $this->_defaults);
    }

Usage Example

Example #1
0
 /**
  * Tests fix for route parameter matching.
  */
 public function testTwoParameterRoutes()
 {
     $route = new Route(array('template' => '/personnel/{:personnel_id}/position/{:position_id}/actions/create', 'params' => array('controller' => 'actions', 'action' => 'create')));
     $route->compile();
     $data = $route->export();
     $actual = $data['pattern'];
     $expected = '@^/personnel(?:/(?P<personnel_id>[^\\/]+))/position(?:/';
     $expected .= '(?P<position_id>[^\\/]+))/actions/create$@u';
     $this->assertEqual($expected, $actual);
 }