1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 | <?php /** * Route definition * * @author Christopher Han <xiphux@gmail.com> * @copyright Copyright (c) 2012 Christopher Han * @package GitPHP * @subpackage Router */ class GitPHP_Route { /** * The route path * * @var string */ protected $path; /** * The route constraints * * @var string[] */ protected $constraints = array(); /** * Additional route parameters * * @var string[] */ protected $extraParameters = array(); /** * Url parameters * * @var string[] */ protected $urlParameters = array(); /** * Used parameters * * @var string[] */ protected $usedParameters = array(); /** * Cached constraints * * @var array[] */ protected $cachedConstraints; /** * Constructor * * @param string $path route path * @param string[] $constraints route constraints * @param string[] $extraParameters additional route parameters * @param GitPHP_Route $parent parent route */ public function __construct($path, $constraints = array(), $extraParameters = array(), $parent = null) { if (empty($path)) throw new Exception('Path is required'); // initialize path if ($parent) $this->path = $parent->GetPath() . '/' . $path; else $this->path = $path; // initialize constraints if ($parent) $this->constraints = array_merge($parent->constraints, $constraints); else $this->constraints = $constraints; // initialise extra parameters if ($parent) $this->extraParameters = array_merge($parent->extraParameters, $extraParameters); else $this->extraParameters = $extraParameters; // initialize url parameters $fullPath = explode('/', $this->path); foreach ($fullPath as $pathpiece) { if (strncmp($pathpiece, ':', 1) === 0) { $param = substr($pathpiece, 1); $this->urlParameters[] = $param; } } // initialize used parameters $this->usedParameters = array_merge($this->urlParameters, array_keys($extraParameters)); if ($parent) $this->usedParameters = array_merge($parent->GetUsedParameters(), $this->usedParameters); $this->usedParameters = array_unique($this->usedParameters); } /** * Get route path * * @return string $path */ public function GetPath() { return $this->path; } /** * Test if this route matches the given path * * @param string $path path * @return array|boolean array of parameters or false if not matched */ public function Match($path) { if (empty($path)) return false; $routepieces = explode('/', $this->GetPath()); foreach ($routepieces as $i => $routepiece) { if (strncmp($routepiece, ':', 1) === 0) { $routepiece = substr($routepiece, 1); if (!empty($this->constraints[$routepiece])) { $pattern = '(?P<' . $routepiece . '>' . $this->constraints[$routepiece] . ')'; } else { $pattern = '(?P<' . $routepiece . '>.+)'; } $routepieces[$i] = $pattern; } } $routepattern = implode('/', $routepieces); if (!preg_match('@^' . $routepattern . '$@', $path, $regs)) return false; $params = array(); foreach ($regs as $key => $register) { if (!is_string($key)) continue; $params[$key] = $register; } if (count($this->extraParameters) > 0) { $params = array_merge($params, $this->extraParameters); } return $params; } /** * Test if route is valid for the given parameters * * @param string[] $params parameters * @return boolean true if matech */ public function Valid($params) { foreach ($this->constraints as $param => $constraint) { if (empty($params[$param])) return false; $paramval = $params[$param]; if (!isset($this->cachedConstraints[$param][$paramval])) { $this->cachedConstraints[$param][$paramval] = preg_match('@^' . $constraint . '$@', $params[$param]); } if (!$this->cachedConstraints[$param][$paramval]) return false; } foreach ($this->urlParameters as $param) { if (empty($params[$param])) return false; } return true; } /** * Build route from parameters * * @param string[] $params parameters * @return string full route */ public function Build($params) { $path = $this->GetPath(); $routepieces = explode('/', $path); foreach ($routepieces as $i => $piece) { if (strncmp($piece, ':', 1) !== 0) { // not a param continue; } $paramname = substr($piece, 1); $routepieces[$i] = $params[$paramname]; } return trim(implode('/', $routepieces), '/'); } /** * Get list of params used in this route * * @return string[] array of parameters */ public function GetUsedParameters() { return $this->usedParameters; } /** * Compare routes for precedence * * @param GitPHP_Route $a route a * @param GitPHP_Route $b route b */ public static function CompareRoute($a, $b) { $apath = $a->GetPath(); $bpath = $b->GetPath(); $acount = substr_count($apath, ':'); $bcount = substr_count($bpath, ':'); if ($acount == $bcount) { $acount2 = substr_count($apath, '/'); $bcount2 = substr_count($bpath, '/'); if ($acount2 == $bcount2) return 0; return $acount2 < $bcount2 ? 1 : -1; } return $acount < $bcount ? 1 : -1; } } |