From 7c33d95cc50fc9bd58c0c8179494ac776901b2c0 Mon Sep 17 00:00:00 2001 From: Michael M Slusarz Date: Sun, 6 Dec 2009 23:02:49 -0700 Subject: [PATCH] Move url cloning functions to cAdd() and cRemove() --- framework/Url/lib/Horde/Url.php | 30 ++++++++++++++++++++++++++++++ framework/Util/lib/Horde/Util.php | 15 +++------------ 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/framework/Url/lib/Horde/Url.php b/framework/Url/lib/Horde/Url.php index 0c95f2a3c..7136747c5 100644 --- a/framework/Url/lib/Horde/Url.php +++ b/framework/Url/lib/Horde/Url.php @@ -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. diff --git a/framework/Util/lib/Horde/Util.php b/framework/Util/lib/Horde/Util.php index 6eaa734d3..6e20a4781 100644 --- a/framework/Util/lib/Horde/Util.php +++ b/framework/Util/lib/Horde/Util.php @@ -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); -- 2.11.0