protected $_viewsDir = '';
/**
+ * @var
+ */
+ protected $_urlWriter;
+
+ /**
* New controller instance
*/
public function __construct($options)
}
/**
+ * 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.
*
*/
class Horde_Controller_Dispatcher
{
+ /** @var Horde_Controller_Dispatcher */
+ private static $_instance;
+
/** @var Horde_Routes_Mapper */
protected $_mapper;
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');
--- /dev/null
+<?php
+/**
+ * Copyright 2007-2008 Maintainable Software, LLC
+ * Copyright 2008 The Horde Project (http://www.horde.org)
+ *
+ * @author Mike Naberezny <mike@maintainable.com>
+ * @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
<file name="Dispatcher.php" role="php" />
<file name="Exception.php" role="php" />
<file name="Mime.php" role="php" />
+ <file name="UrlWriter.php" role="php" />
</dir> <!-- /lib/Horde/Controller -->
</dir> <!-- /lib/Horde -->
</dir> <!-- /lib -->
<install name="lib/Horde/Controller/Base.php" as="Horde/Controller/Base.php" />
<install name="lib/Horde/Controller/Dispatcher.php" as="Horde/Controller/Dispatcher.php" />
<install name="lib/Horde/Controller/Exception.php" as="Horde/Controller/Exception.php" />
+ <install name="lib/Horde/Controller/UrlWriter.php" as="Horde/Controller/UrlWriter.php" />
<install name="lib/Horde/Controller/Mime/Type.php" as="Horde/Controller/Mime/Type.php" />
<install name="lib/Horde/Controller/Request/Base.php" as="Horde/Controller/Request/Base.php" />
<install name="lib/Horde/Controller/Request/Http.php" as="Horde/Controller/Request/Http.php" />