123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327 |
- <?php
- if (!defined('STATUSNET')) {
-
-
- exit(1);
- }
- class URLMapper
- {
- const ACTION = 'action';
- protected $statics = [];
- protected $variables = [];
- protected $reverse_dynamics = [];
- protected $reverse_statics = [];
- protected $allpaths = [];
-
- public function connect(string $path, array $args, array $paramPatterns = [], bool $acceptHeaders = false, array $headers = [])
- {
- if (!array_key_exists(self::ACTION, $args)) {
- throw new Exception(sprintf("Can't connect %s; path has no action.", $path));
- }
- $should = true;
- if ($acceptHeaders) {
-
-
-
- $should = empty($headers) || self::should($headers);
- }
- $this->allpaths[] = $path;
- $action = $args[self::ACTION];
- $paramNames = $this->getParamNames($path);
- if (empty($paramNames)) {
- $this->statics[$path] = $args;
- if (array_key_exists($action, $this->reverse_statics)) {
- $this->reverse_statics[$action][] = [$args, $path];
- } else {
- $this->reverse_statics[$action] = [[$args, $path]];
- }
- } else {
-
- foreach ($paramNames as $name) {
- if (!array_key_exists($name, $paramPatterns) &&
- array_key_exists($name, $args)) {
- $paramPatterns[$name] = $args[$name];
- unset($args[$name]);
- }
- }
-
- if ($should) {
- $regex = self::makeRegex($path, $paramPatterns);
- if (isset($this->variables[$regex]) || !$acceptHeaders) {
- $this->variables[$regex] = [$args, $paramNames];
- } else {
-
-
-
- $this->variables = [$regex => [$args, $paramNames]] + $this->variables;
- }
- }
- $format = $this->makeFormat($path);
- if (array_key_exists($action, $this->reverse_dynamics)) {
- $this->reverse_dynamics[$action][] = [$args, $format, $paramNames];
- } else {
- $this->reverse_dynamics[$action] = [[$args, $format, $paramNames]];
- }
- }
- }
- public function match($path)
- {
- if (array_key_exists($path, $this->statics)) {
- return $this->statics[$path];
- }
- foreach ($this->variables as $regex => $pattern) {
- list($args, $paramNames) = $pattern;
- if (preg_match($regex, $path, $match)) {
- $results = $args;
- foreach ($paramNames as $name) {
- $results[$name] = $match[$name];
- }
- return $results;
- }
- }
- throw new NoRouteMapException($path);
- }
- public function generate($args, $qstring, $fragment)
- {
- if (!array_key_exists(self::ACTION, $args)) {
- throw new Exception("Every path needs an action.");
- }
- $action = $args[self::ACTION];
- if (!array_key_exists($action, $this->reverse_dynamics) && !array_key_exists($action, $this->reverse_statics)) {
- throw new Exception(sprintf('No candidate paths for action "%s"', $action));
- }
- if (array_key_exists($action, $this->reverse_dynamics)){
- $candidates = $this->reverse_dynamics[$action];
- foreach ($candidates as $candidate) {
- list($tryArgs, $format, $paramNames) = $candidate;
- foreach ($tryArgs as $key => $value) {
- if (!array_key_exists($key, $args) || $args[$key] != $value) {
-
- continue 2;
- }
- }
-
- $toFormat = [];
- foreach ($paramNames as $name) {
- if (!array_key_exists($name, $args)) {
-
- continue 2;
- }
- $toFormat[] = $args[$name];
- }
- $path = vsprintf($format, $toFormat);
- if (!empty($qstring)) {
- $formatted = http_build_query($qstring);
- $path .= '?' . $formatted;
- }
- return $path;
- }
- }
- if (array_key_exists($action, $this->reverse_statics)) {
- $candidates = $this->reverse_statics[$action];
- foreach ($candidates as $candidate) {
- list($tryArgs, $tryPath) = $candidate;
- foreach ($tryArgs as $key => $value) {
- if (!array_key_exists($key, $args) || $args[$key] != $value) {
-
- continue 2;
- }
- }
-
- $path = $tryPath;
- if (!empty($qstring)) {
- $formatted = http_build_query($qstring);
- $path .= '?' . $formatted;
- }
- return $path;
- }
- }
-
- unset($args['action']);
- if (empty($args)) {
- throw new Exception(sprintf('No matches for action "%s"', $action));
- }
- $argstring = '';
- foreach ($args as $key => $value) {
- $argstring .= "$key=$value ";
- }
- throw new Exception(sprintf('No matches for action "%s" with arguments "%s"', $action, $argstring));
- }
- protected function getParamNames($path)
- {
- preg_match_all('/:(?P<name>\w+)/', $path, $match);
- return $match['name'];
- }
- public static function makeRegex($path, $paramPatterns)
- {
- $pr = new PatternReplacer($paramPatterns);
- $regex = preg_replace_callback('/:(\w+)/',
- [$pr, 'toPattern'],
- $path);
- $regex = '#^' . str_replace('#', '\#', $regex) . '$#';
- return $regex;
- }
- protected function makeFormat($path)
- {
- $format = preg_replace('/(:\w+)/', '%s', $path);
- return $format;
- }
- public function getPaths()
- {
- return array_unique($this->allpaths);
- }
-
- public static function should(array $headers): bool
- {
- if (!isset($_SERVER['HTTP_ACCEPT'])) {
- return false;
- }
- $acceptHeader = new AcceptHeader($_SERVER['HTTP_ACCEPT']);
- foreach ($acceptHeader as $ah) {
- if (isset($headers[$ah['raw']])) {
- return true;
- }
- }
- return false;
- }
- }
- class PatternReplacer
- {
- private $patterns;
- public function __construct($patterns)
- {
- $this->patterns = $patterns;
- }
- public function toPattern($matches)
- {
-
- $name = $matches[1];
- if (array_key_exists($name, $this->patterns)) {
- $pattern = $this->patterns[$name];
- } else {
-
- $pattern = '\w+';
- }
- return '(?P<'.$name.'>'.$pattern.')';
- }
- }
|