3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2011, StatusNet, Inc.
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU Affero General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Affero General Public License for more details.
20 * You should have received a copy of the GNU Affero General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 * @author Evan Prodromou <evan@status.net>
26 * @copyright 2011 StatusNet, Inc.
27 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
28 * @link http://status.net/
31 if (!defined('STATUSNET')) {
32 // This check helps protect against security problems;
33 // your code file can't be executed directly from the web.
40 * Converts a path into a set of parameters, and vice versa
42 * We used to use URLMapper, so there's a wrapper class at Router, q.v.
44 * NUM's vagaries are the main reason we have weirdnesses here.
48 * @author Evan Prodromou <evan@status.net>
49 * @copyright 2011 StatusNet, Inc.
50 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
51 * @link http://status.net/
56 const ACTION = 'action';
58 protected $statics = array();
59 protected $variables = array();
60 protected $reverse = array();
61 protected $allpaths = array();
63 function connect($path, $args, $paramPatterns=array())
65 if (!array_key_exists(self::ACTION, $args)) {
66 throw new Exception(sprintf("Can't connect %s; path has no action.", $path));
71 $action = $args[self::ACTION];
73 $paramNames = $this->getParamNames($path);
75 if (empty($paramNames)) {
76 $this->statics[$path] = $args;
77 if (array_key_exists($action, $this->reverse)) {
78 $this->reverse[$args[self::ACTION]][] = array($args, $path);
80 $this->reverse[$args[self::ACTION]] = array(array($args, $path));
84 // Eff if I understand why some go here and some go there.
85 // Anyways, fixup my preconceptions
87 foreach ($paramNames as $name) {
88 if (!array_key_exists($name, $paramPatterns) &&
89 array_key_exists($name, $args)) {
90 $paramPatterns[$name] = $args[$name];
95 $regex = $this->makeRegex($path, $paramPatterns);
97 $this->variables[] = array($args, $regex, $paramNames);
99 $format = $this->makeFormat($path, $paramPatterns);
101 if (array_key_exists($action, $this->reverse)) {
102 $this->reverse[$args[self::ACTION]][] = array($args, $format, $paramNames);
104 $this->reverse[$args[self::ACTION]] = array(array($args, $format, $paramNames));
109 function match($path)
111 if (array_key_exists($path, $this->statics)) {
112 return $this->statics[$path];
115 foreach ($this->variables as $pattern) {
116 list($args, $regex, $paramNames) = $pattern;
117 if (preg_match($regex, $path, $match)) {
119 foreach ($paramNames as $name) {
120 $results[$name] = $match[$name];
126 throw new Exception(sprintf('No match for path "%s"', $path));
129 function generate($args, $qstring, $fragment)
131 if (!array_key_exists(self::ACTION, $args)) {
132 throw new Exception("Every path needs an action.");
135 $action = $args[self::ACTION];
137 if (!array_key_exists($action, $this->reverse)) {
138 throw new Exception(sprintf('No candidate paths for action "%s"', $action));
141 $candidates = $this->reverse[$action];
143 foreach ($candidates as $candidate) {
144 if (count($candidate) == 2) { // static
145 list($tryArgs, $tryPath) = $candidate;
146 foreach ($tryArgs as $key => $value) {
147 if (!array_key_exists($key, $args) || $args[$key] != $value) {
155 list($tryArgs, $format, $paramNames) = $candidate;
157 foreach ($tryArgs as $key => $value) {
158 if (!array_key_exists($key, $args) || $args[$key] != $value) {
168 foreach ($paramNames as $name) {
169 if (!array_key_exists($name, $args)) {
173 $toFormat[] = $args[$name];
176 $path = vsprintf($format, $toFormat);
179 if (!empty($qstring)) {
180 $formatted = http_build_query($qstring);
181 $path .= '?' . $formatted;
187 // failure; some reporting twiddles
189 unset($args['action']);
192 throw new Exception(sprintf('No matches for action "%s"', $action));
197 foreach ($args as $key => $value) {
198 $argstring .= "$key=$value ";
201 throw new Exception(sprintf('No matches for action "%s" with arguments "%s"', $action, $argstring));
204 protected function getParamNames($path)
206 preg_match_all('/:(?P<name>\w+)/', $path, $match);
207 return $match['name'];
210 protected function makeRegex($path, $paramPatterns)
212 $pr = new PatternReplacer($paramPatterns);
214 $regex = preg_replace_callback('/:(\w+)/',
215 array($pr, 'toPattern'),
218 $regex = '#^' . str_replace('#', '\#', $regex) . '$#';
223 protected function makeFormat($path, $paramPatterns)
225 $format = preg_replace('/(:\w+)/', '%s', $path);
230 public function getPaths()
232 return $this->allpaths;
236 class PatternReplacer
240 function __construct($patterns)
242 $this->patterns = $patterns;
245 function toPattern($matches)
249 if (array_key_exists($name, $this->patterns)) {
250 $pattern = $this->patterns[$name];
255 return '(?P<'.$name.'>'.$pattern.')';