* Implement a Horde_Controller_Dispatcher::singleton() method
authorMichael J. Rubinsky <mrubinsk@horde.org>
Wed, 10 Dec 2008 00:21:30 +0000 (19:21 -0500)
committerMichael J. Rubinsky <mrubinsk@horde.org>
Wed, 10 Dec 2008 00:24:30 +0000 (19:24 -0500)
* 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
framework/Controller/lib/Horde/Controller/Dispatcher.php
framework/Controller/lib/Horde/Controller/UrlWriter.php [new file with mode: 0644]
framework/Controller/package.xml

index ef8bbdb..438c63b 100644 (file)
@@ -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.
      *
index 2091140..497e95b 100644 (file)
@@ -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 (file)
index 0000000..6f71b1e
--- /dev/null
@@ -0,0 +1,85 @@
+<?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
index 778f9d7..3f2781e 100644 (file)
@@ -53,6 +53,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
       <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 -->
@@ -73,6 +74,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
    <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" />