Axiom (v1.2.0)

A lightweight PHP framework
H:/Workspace/php-axiom/www/php-axiom/libraries/axiom/axRoute.class.php
Go to the documentation of this file.
00001 <?php
00020 class axRoute {
00021     
00026     protected $_template;
00027     
00033     protected $_pattern;
00034     
00040     protected $_keys;
00041 
00046     protected $_params;
00047 
00052     protected $_options;
00053     
00061     public function __construct ($template, array $params = array(), array $options = array()) {
00062         $this->_template = $template;
00063         $this->_params   = $params;
00064         $this->_options  = $options;
00065         $this->_keys     = array();
00066     }
00067     
00073     public function match ($url) {
00074         $url = '/' . trim($url, '/') . '/';
00075         
00076         if (empty($this->_pattern))
00077             $this->_compileTemplate($this->_template);
00078                 
00079         if (preg_match($this->_pattern, $url, $matches)) {
00080             unset($matches[0]);
00081             if (!empty($this->_keys) && !empty($matches)) {
00082                 $this->_params = array_merge($this->_params, array_intersect_key($matches, $this->_keys));
00083             }
00084             return true;
00085         }
00086         return false;
00087     }
00088     
00096     protected function _compileTemplate ($template) {
00097         $token = strtok($template, '/');
00098         $pattern_pieces = array();
00099         $current_key = 1;
00100         do {
00101             // Here be dragons
00102             if (preg_match('~\{:(?P<key>\w+)(:(?P<tpl>[^\}:]*))?(:(?P<opt>[^\}:]*))?\}~', $token, $matches)) {
00103                 if ($matches['key'] == 'lang' && empty($matches['tpl'])) {
00104                     $matches['tpl'] = '[a-z]{2}';
00105                 }
00106                 if ($matches['key'] == 'id' && empty($matches['tpl'])) {
00107                     $matches['tpl'] = '\d+';
00108                 }
00109                 if (($matches['key'] == 'controller' || $matches['key'] == 'action') && empty($matches['tpl'])) {
00110                     $matches['tpl'] = "\w{3,}";
00111                 }
00112                 if (empty($matches['tpl'])) {
00113                     $matches['tpl'] = "[^/]+";
00114                 }
00115                 
00116                 $subpattern = "(?P<{$matches['key']}>{$matches['tpl']})/";
00117                 
00118                 if (isset($matches['opt']) && strpos($matches['opt'], '?') !== false) {
00119                     $subpattern = "({$subpattern})?";
00120                 }
00121                 
00122                 $this->_keys[$matches['key']] = $matches['key'];
00123                 $pattern_pieces[] = $subpattern;
00124             }
00125             else {
00126                 $pattern_pieces[] = $token . '/';
00127             }
00128         } while ($token = strtok('/'));
00129         $this->_pattern = '~^/' . implode($pattern_pieces) . '$~';
00130     }
00131     
00136     public function getParams () {
00137         return $this->_params;
00138     }
00139     
00144     public function getOptions () {
00145         return $this->_options;
00146     }
00147     
00152     public function getTemplate () {
00153         return $this->_template;
00154     }
00155     
00160     public function getPattern () {
00161         return $this->_pattern;
00162     }
00163 }
 All Data Structures Files Functions Variables