From edb2aac241aeb2220883108de86be8a80fa89e9a Mon Sep 17 00:00:00 2001 From: "Michael J. Rubinsky" Date: Tue, 9 Dec 2008 19:21:30 -0500 Subject: [PATCH] * Implement a Horde_Controller_Dispatcher::singleton() method * Add Horde_Controller_UrlWriter class and a getter method to return in from a controller. Chuck - in poking around I saw that there is an entry in package.xml for a non-existent Controller/Mime.php file? Should that be nuked or is that file still coming? --- framework/Controller/lib/Horde/Controller/Base.php | 34 +++++++++ .../Controller/lib/Horde/Controller/Dispatcher.php | 22 +++++- .../Controller/lib/Horde/Controller/UrlWriter.php | 85 ++++++++++++++++++++++ framework/Controller/package.xml | 2 + 4 files changed, 141 insertions(+), 2 deletions(-) create mode 100644 framework/Controller/lib/Horde/Controller/UrlWriter.php diff --git a/framework/Controller/lib/Horde/Controller/Base.php b/framework/Controller/lib/Horde/Controller/Base.php index ef8bbdb6d..438c63bf7 100644 --- a/framework/Controller/lib/Horde/Controller/Base.php +++ b/framework/Controller/lib/Horde/Controller/Base.php @@ -72,6 +72,11 @@ abstract class Horde_Controller_Base protected $_viewsDir = ''; /** + * @var + */ + protected $_urlWriter; + + /** * New controller instance */ public function __construct($options) @@ -162,6 +167,35 @@ abstract class Horde_Controller_Base } /** + * Get an instance of UrlWriter for this controller. + * + * @return Horde_Controller_UrlWriter + */ + public function getUrlWriter() + { + // instantiate UrlWriter that will generate URLs for this controller + if (!$this->_urlWriter) { + $defaults = array('controller' => $this->getControllerName()); + $this->_urlWriter = new Horde_Controller_UrlWriter($defaults); + } + return $this->_urlWriter; + } + + /** + * Get the current controller's name. + * + * @return string + */ + protected function getControllerName() + { + if (empty($this->params)) { + $this->_initParams(); + } + + return $this->params[':controller']; + } + + /** * Render the response to the user. Actions are automatically rendered if no other * action is specified. * diff --git a/framework/Controller/lib/Horde/Controller/Dispatcher.php b/framework/Controller/lib/Horde/Controller/Dispatcher.php index 209114039..497e95bc8 100644 --- a/framework/Controller/lib/Horde/Controller/Dispatcher.php +++ b/framework/Controller/lib/Horde/Controller/Dispatcher.php @@ -23,6 +23,9 @@ */ class Horde_Controller_Dispatcher { + /** @var Horde_Controller_Dispatcher */ + private static $_instance; + /** @var Horde_Routes_Mapper */ protected $_mapper; @@ -39,9 +42,24 @@ class Horde_Controller_Dispatcher protected $_viewsDir = ''; /** - * Class constructor. + * Singleton method. This should be the only way of instantiating a + * Horde_Controller_Dispatcher object. + * + * @return Horde_Controller_Dispatcher + */ + public static function singleton($context = array()) + { + if (self::$_instance === null) { + self::$_instance = new self($context); + } + return self::$_instance; + } + + /** + * Class constructor. Client code should use the singleton method to + * instantiate objects. */ - public function __construct($context) + protected function __construct($context) { if (!isset($context['mapper']) || ! $context['mapper'] instanceof Horde_Routes_Mapper) { throw new Horde_Controller_Exception('Mapper object missing from Dispatcher constructor'); diff --git a/framework/Controller/lib/Horde/Controller/UrlWriter.php b/framework/Controller/lib/Horde/Controller/UrlWriter.php new file mode 100644 index 000000000..6f71b1e40 --- /dev/null +++ b/framework/Controller/lib/Horde/Controller/UrlWriter.php @@ -0,0 +1,85 @@ + + * @license http://opensource.org/licenses/bsd-license.php BSD + * @category Horde + * @package Horde_Controller + */ +class Horde_Controller_UrlWriter +{ + /** + * Defaults to merge into route parameters when not using named routes. + * @var array + */ + protected $_defaults; + + /** + * @var Horde_Routes_Util + */ + protected $_utils; + + /** + * Class constructor + * + * @param array $defaults Defaults to merge for urlFor() + * @param null|Horde_Route_Utils $utils Route utilities + */ + public function __construct($defaults = array(), $utils = null) + { + $this->_defaults = $defaults; + var_dump($defaults); + if ($utils === null) { + $utils = Horde_Controller_Dispatcher::singleton()->getRouteUtils(); + } + $this->_utils = $utils; + } + + /** + * Generate a URL. Same signature as Horde_Routes_Utils->urlFor(). + * + * @param $first mixed + * @param $second mixed + * @return string + */ + public function urlFor($first, $second = array()) + { + // anonymous route: serialize to params & merge defaults + // urlFor(array('controller' => 'books')) + if (is_array($first)) { + $first = array_merge($this->_defaults, + $this->_serializeToParams($first)); + } + + // named route: serialize to params only (no merge) + // urlFor('notes', array('action' => 'show', 'id' => 1)) + if (is_array($second)) { + $second = $this->_serializeToParams($second); + } + + // url generation "route memory" is not useful here + $this->_utils->mapperDict = array(); + + // generate url + return $this->_utils->urlFor($first, $second); + } + + /** + * Serialize any objects in the collection supporting toParam() before + * passing the collection to Horde_Routes. + * + * @param array $collection + * @param array + */ + protected function _serializeToParams($collection) + { + foreach ($collection as &$value) { + if (is_object($value) && method_exists($value, 'toParam')) { + $value = $value->toParam(); + } + } + return $collection; + } +} \ No newline at end of file diff --git a/framework/Controller/package.xml b/framework/Controller/package.xml index 778f9d73c..3f2781eeb 100644 --- a/framework/Controller/package.xml +++ b/framework/Controller/package.xml @@ -53,6 +53,7 @@ http://pear.php.net/dtd/package-2.0.xsd"> + @@ -73,6 +74,7 @@ http://pear.php.net/dtd/package-2.0.xsd"> + -- 2.11.0