Move url cloning functions to cAdd() and cRemove()
authorMichael M Slusarz <slusarz@curecanti.org>
Mon, 7 Dec 2009 06:02:49 +0000 (23:02 -0700)
committerMichael M Slusarz <slusarz@curecanti.org>
Mon, 7 Dec 2009 06:08:44 +0000 (23:08 -0700)
framework/Url/lib/Horde/Url.php
framework/Util/lib/Horde/Util.php

index 0c95f2a..7136747 100644 (file)
@@ -133,6 +133,22 @@ class Horde_Url
     }
 
     /**
+     * Identical to add(), except the return value is a cloned object.
+     *
+     * @param mixed $parameters  Either the name value or an array of
+     *                           name/value pairs.
+     * @param string $value      If specified, the value part ($parameters is
+     *                           then assumed to just be the parameter name).
+     *
+     * @return Horde_Url  Cloned object, to allow chaining.
+     */
+    public function cAdd($parameters, $value = null)
+    {
+        $url = clone $this;
+        return $url->add($parameters, $value);
+    }
+
+    /**
      * Removes one ore more parameters.
      *
      * @param mixed $remove  Either a single parameter to remove or an array
@@ -154,6 +170,20 @@ class Horde_Url
     }
 
     /**
+     * Identical to remove(), except the return value is a cloned object.
+     *
+     * @param mixed $remove  Either a single parameter to remove or an array
+     *                       of parameters to remove.
+     *
+     * @return Horde_Url  Cloned object, to allow chaining.
+     */
+    public function cRemove($parameters)
+    {
+        $url = clone $this;
+        return $url->remove($parameters);
+    }
+
+    /**
      * Creates the full URL string.
      *
      * @return string  The string representation of this object.
index 6eaa734..6e20a47 100644 (file)
@@ -235,13 +235,9 @@ class Horde_Util
         }
 
         if ($url instanceof Horde_Url) {
-            // In many places we re-use URLs assuming they are strings. With
-            // Horde_Url objects, this can lead to reference problems and URLs
-            // being mysteriously modified. For now, we have to clone any
-            // objects to avoid modifying them.
-            $url2 = clone($url);
+            $url2 = $url->cAdd($parameter, $value);
             $url2->raw = !$encode;
-            return $url2->add($parameter, $value);
+            return $url2;
         }
 
         $horde_url = new Horde_Url($url, !$encode);
@@ -260,12 +256,7 @@ class Horde_Util
     static public function removeParameter($url, $remove)
     {
         if ($url instanceof Horde_Url) {
-            // In many places we re-use URLs assuming they are strings. With
-            // Horde_Url objects, this can lead to reference problems and URLs
-            // being mysteriously modified. For now, we have to clone any
-            // objects to avoid modifying them.
-            $url2 = clone($url);
-            return $url2->remove($remove);
+            return $url->cRemove($remove);
         }
 
         $horde_url = new Horde_Url($url);