]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - lib/urlmapper.php
Added base64 encoding to get RMQ to work
[quix0rs-gnu-social.git] / lib / urlmapper.php
index dffb32c8149579aa10e29e7492ff26d7ff7e8078..931b5c3c2a1cf554a5f35ac7229d7e2fa59a0c82 100644 (file)
@@ -4,7 +4,7 @@
  * Copyright (C) 2011, StatusNet, Inc.
  *
  * URL mapper
- * 
+ *
  * PHP version 5
  *
  * This program is free software: you can redistribute it and/or modify
@@ -20,7 +20,7 @@
  * You should have received a copy of the GNU Affero General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
- * @category  Cache
+ * @category  URL
  * @package   StatusNet
  * @author    Evan Prodromou <evan@status.net>
  * @copyright 2011 StatusNet, Inc.
@@ -40,7 +40,7 @@ if (!defined('STATUSNET')) {
  * Converts a path into a set of parameters, and vice versa
  *
  * We used to use Net_URL_Mapper, so there's a wrapper class at Router, q.v.
- * 
+ *
  * NUM's vagaries are the main reason we have weirdnesses here.
  *
  * @category  URL
@@ -58,13 +58,16 @@ class URLMapper
     protected $statics = array();
     protected $variables = array();
     protected $reverse = array();
+    protected $allpaths = array();
 
-    function connect($path, $args, $paramPatterns=null)
+    function connect($path, $args, $paramPatterns=array())
     {
         if (!array_key_exists(self::ACTION, $args)) {
             throw new Exception(sprintf("Can't connect %s; path has no action.", $path));
         }
 
+        $this->allpaths[] = $path;
+
         $action = $args[self::ACTION];
 
         $paramNames = $this->getParamNames($path);
@@ -89,7 +92,7 @@ class URLMapper
                 }
             }
 
-            $regex = $this->makeRegex($path, $paramPatterns);
+            $regex = self::makeRegex($path, $paramPatterns);
 
             $this->variables[] = array($args, $regex, $paramNames);
 
@@ -119,8 +122,8 @@ class URLMapper
                 return $results;
             }
         }
-        
-        throw new Exception(sprintf('No match for path "%s"', $path));
+
+        throw new NoRouteMapException($path);
     }
 
     function generate($args, $qstring, $fragment)
@@ -147,7 +150,7 @@ class URLMapper
                     }
                 }
                 // success
-                return $tryPath;
+                $path = $tryPath;
             } else {
                 list($tryArgs, $format, $paramNames) = $candidate;
 
@@ -171,15 +174,18 @@ class URLMapper
                 }
 
                 $path = vsprintf($format, $toFormat);
+            }
 
-                if (!empty($qstring)) {
-                    $path .= '?' . http_build_query($qstring);
-                }
-
-                return $path;
+            if (!empty($qstring)) {
+                $formatted = http_build_query($qstring);
+                $path .= '?' . $formatted;
             }
+
+            return $path;
         }
 
+        // failure; some reporting twiddles
+
         unset($args['action']);
 
         if (empty($args)) {
@@ -201,7 +207,7 @@ class URLMapper
         return $match['name'];
     }
 
-    protected function makeRegex($path, $paramPatterns)
+    static function makeRegex($path, $paramPatterns)
     {
         $pr = new PatternReplacer($paramPatterns);
 
@@ -209,7 +215,7 @@ class URLMapper
                                        array($pr, 'toPattern'),
                                        $path);
 
-        $regex = '#' . str_replace('#', '\#', $regex) . '#';
+        $regex = '#^' . str_replace('#', '\#', $regex) . '$#';
 
         return $regex;
     }
@@ -220,6 +226,11 @@ class URLMapper
 
         return $format;
     }
+
+    public function getPaths()
+    {
+        return $this->allpaths;
+    }
 }
 
 class PatternReplacer