From 04a19d561bd2c7706520475b39dfd30edab4d765 Mon Sep 17 00:00:00 2001 From: Chuck Hagenbuch Date: Thu, 19 Feb 2009 17:47:51 -0500 Subject: [PATCH] - add an option to throw an exception when helper methods clash - allow calling addHelper('Tag') or addHelper('MyHelper') which automatically instantiates Horde_View_Helper_Tag or MyHelper objects, respectively. --- framework/View/lib/Horde/View/Base.php | 43 +++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/framework/View/lib/Horde/View/Base.php b/framework/View/lib/Horde/View/Base.php index 32ef96439..554f66826 100644 --- a/framework/View/lib/Horde/View/Base.php +++ b/framework/View/lib/Horde/View/Base.php @@ -49,6 +49,12 @@ abstract class Horde_View_Base private $_encoding = 'UTF-8'; /** + * Should we throw an error if helper methods collide? + * @var boolean + */ + private $_throwOnHelperCollision = false; + + /** * Constructor. * * @param array $config Configuration key-value pairs. @@ -143,11 +149,36 @@ abstract class Horde_View_Base /** * Adds to the stack of helpers in LIFO order. * - * @param Horde_View_Helper $helper The helper instance to add. + * @param Horde_View_Helper $helper The helper instance to add. If this is a + * string instead of a Helper instance, then it will be treated as a class + * name. Names without "_" and that do not have "Helper" in them will be + * prefixed with Horde_View_Helper_; other names will be treated as literal + * class names. Examples: + * + * $v->addHelper('Tag'); // Adds a new Horde_View_Helper_Tag to the view + * $v->addHelper('AppHelper'); // Adds a new AppHelper object to the view if + * // it exists, otherwise throws an exception. */ public function addHelper($helper) { + if (is_string($helper)) { + if (strpos($helper, '_') === false && strpos($helper, 'Helper') === false) { + $class = 'Horde_View_Helper_' . $helper; + } else { + $class = $helper; + } + if (!class_exists($class)) { + throw new Horde_View_Exception('Helper class ' . $helper . ' not found'); + } + $helper = new $class($this); + } + foreach (get_class_methods($helper) as $method) { + if (isset($this->_helpers[$method])) { + $msg = 'Helper method ' . get_class($this->_helpers[$helper]) . '#' . $method . ' overridden by ' . get_class($helper) . '#' . $method; + if ($this->_throwOnHelperCollision) { throw new Horde_View_Exception($msg); } + if ($this->logger) { $this->logger->warn($msg); } + } $this->_helpers[$method] = $helper; } } @@ -239,6 +270,16 @@ abstract class Horde_View_Base } /** + * Control the behavior when a helper method is overridden by another helper + * + * @param boolean $throw Throw an exception when helper methods collide? + */ + public function throwOnHelperCollision($throw = true) + { + $this->_throwOnHelperCollision = $throw; + } + + /** * Finds a template from the available directories. * * @param $name string The base name of the template. -- 2.11.0