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 Net_URL_Mapper, 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();
62 function connect($path, $args, $paramPatterns=array())
64 if (!array_key_exists(self::ACTION, $args)) {
65 throw new Exception(sprintf("Can't connect %s; path has no action.", $path));
68 $action = $args[self::ACTION];
70 $paramNames = $this->getParamNames($path);
72 if (empty($paramNames)) {
73 $this->statics[$path] = $args;
74 if (array_key_exists($action, $this->reverse)) {
75 $this->reverse[$args[self::ACTION]][] = array($args, $path);
77 $this->reverse[$args[self::ACTION]] = array(array($args, $path));
81 // Eff if I understand why some go here and some go there.
82 // Anyways, fixup my preconceptions
84 foreach ($paramNames as $name) {
85 if (!array_key_exists($name, $paramPatterns) &&
86 array_key_exists($name, $args)) {
87 $paramPatterns[$name] = $args[$name];
92 $regex = $this->makeRegex($path, $paramPatterns);
94 $this->variables[] = array($args, $regex, $paramNames);
96 $format = $this->makeFormat($path, $paramPatterns);
98 if (array_key_exists($action, $this->reverse)) {
99 $this->reverse[$args[self::ACTION]][] = array($args, $format, $paramNames);
101 $this->reverse[$args[self::ACTION]] = array(array($args, $format, $paramNames));
106 function match($path)
108 if (array_key_exists($path, $this->statics)) {
109 return $this->statics[$path];
112 foreach ($this->variables as $pattern) {
113 list($args, $regex, $paramNames) = $pattern;
114 if (preg_match($regex, $path, $match)) {
116 foreach ($paramNames as $name) {
117 $results[$name] = $match[$name];
123 throw new Exception(sprintf('No match for path "%s"', $path));
126 function generate($args, $qstring, $fragment)
128 if (!array_key_exists(self::ACTION, $args)) {
129 throw new Exception("Every path needs an action.");
132 $action = $args[self::ACTION];
134 if (!array_key_exists($action, $this->reverse)) {
135 throw new Exception(sprintf('No candidate paths for action "%s"', $action));
138 $candidates = $this->reverse[$action];
140 foreach ($candidates as $candidate) {
141 if (count($candidate) == 2) { // static
142 list($tryArgs, $tryPath) = $candidate;
143 foreach ($tryArgs as $key => $value) {
144 if (!array_key_exists($key, $args) || $args[$key] != $value) {
152 list($tryArgs, $format, $paramNames) = $candidate;
154 foreach ($tryArgs as $key => $value) {
155 if (!array_key_exists($key, $args) || $args[$key] != $value) {
165 foreach ($paramNames as $name) {
166 if (!array_key_exists($name, $args)) {
170 $toFormat[] = $args[$name];
173 $path = vsprintf($format, $toFormat);
176 if (!empty($qstring)) {
177 $formatted = http_build_query($qstring);
178 $path .= '?' . $formatted;
184 // failure; some reporting twiddles
186 unset($args['action']);
189 throw new Exception(sprintf('No matches for action "%s"', $action));
194 foreach ($args as $key => $value) {
195 $argstring .= "$key=$value ";
198 throw new Exception(sprintf('No matches for action "%s" with arguments "%s"', $action, $argstring));
201 protected function getParamNames($path)
203 preg_match_all('/:(?P<name>\w+)/', $path, $match);
204 return $match['name'];
207 protected function makeRegex($path, $paramPatterns)
209 $pr = new PatternReplacer($paramPatterns);
211 $regex = preg_replace_callback('/:(\w+)/',
212 array($pr, 'toPattern'),
215 $regex = '#' . str_replace('#', '\#', $regex) . '#';
220 protected function makeFormat($path, $paramPatterns)
222 $format = preg_replace('/(:\w+)/', '%s', $path);
228 class PatternReplacer
232 function __construct($patterns)
234 $this->patterns = $patterns;
237 function toPattern($matches)
241 if (array_key_exists($name, $this->patterns)) {
242 $pattern = $this->patterns[$name];
247 return '(?P<'.$name.'>'.$pattern.')';