use the injector for Jonah_Driver
authorMichael J. Rubinsky <mrubinsk@horde.org>
Mon, 28 Jun 2010 18:52:32 +0000 (14:52 -0400)
committerMichael J. Rubinsky <mrubinsk@horde.org>
Mon, 28 Jun 2010 18:52:32 +0000 (14:52 -0400)
jonah/channels/delete.php
jonah/feed.php
jonah/lib/Api.php
jonah/lib/Application.php
jonah/lib/Injector/Binder/Driver.php [new file with mode: 0644]
jonah/lib/Injector/Factory/Driver.php [new file with mode: 0644]
jonah/lib/News.php

index 1b3d043..0efe92e 100644 (file)
@@ -11,7 +11,7 @@
  * @author Marko Djukic <marko@oblo.com>
  */
 
-require_once dirname(__FILE__) . '/lib/Application.php';
+require_once dirname(__FILE__) . '/../lib/Application.php';
 $jonah = Horde_Registry::appInit('jonah');
 require_once 'Horde/Form.php';
 require_once 'Horde/Form/Renderer.php';
index 1024377..254f048 100644 (file)
@@ -32,13 +32,13 @@ foreach ($templates as $key => $info) {
 }
 
 if (empty($criteria['channel_id']) && !empty($criteria['feed'])) {
-    $criteria['channel_id'] = $jonah_driver->getChannelId($criteria['feed']);
+    $criteria['channel_id'] = $GLOBALS['injector']->getInstance('Jonah_Driver')->getChannelId($criteria['feed']);
 }
 
 if (empty($criteria['channel_id'])) {
     $notification->push(_("No valid feed name or ID requested."), 'horde.error');
 } else {
-    $stories = $jonah_driver->getStories($criteria);
+    $stories = $GLOBALS['injector']->getInstance('Jonah_Driver')->getStories($criteria);
 }
 
 if (!empty($stories)) {
index 6d67dda..3c17421 100644 (file)
@@ -221,7 +221,7 @@ class Jonah_Api extends Horde_Registry_Api
     {
         global $registry;
 
-        $results = $GLOBALS['jonah_driver']->getStoryCount($channel_id);
+        $results = $GLOBALS['injector']->getInstance('Jonah_Driver')->getStoryCount($channel_id);
         if (is_a($results, 'PEAR_Error')) {
             return 0;
         }
index 4d61a80..ba8a6b8 100644 (file)
@@ -34,6 +34,8 @@ class Jonah_Application extends Horde_Registry_Application
      */
     protected function _init()
     {
+        $GLOBALS['injector']->addBinder('Jonah_Driver', new Jonah_Injector_Binder_Driver());
+        
         $GLOBALS['jonah_driver'] = Jonah_Driver::factory();
     }
 
diff --git a/jonah/lib/Injector/Binder/Driver.php b/jonah/lib/Injector/Binder/Driver.php
new file mode 100644 (file)
index 0000000..8b7cd3d
--- /dev/null
@@ -0,0 +1,31 @@
+<?php
+/**
+ * Jonah_Driver binder.
+ * 
+ * Copyright 2010 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file LICENSE for license information (BSD). If you did not
+ * did not receive this file, see http://cvs.horde.org/co.php/jonah/LICENSE.
+ *
+ * @author Michael J. Rubinsky <mrubinsk@horde.org>
+ * @package Jonah
+ */
+class Jonah_Injector_Binder_Driver Implements Horde_Injector_Binder
+{
+    public function create(Horde_Injector $injector)
+    {
+        $driver = $GLOBALS['conf']['news']['storage']['driver'];
+        $params = Horde::getDriverConfig(array('news', 'storage'), $driver);
+
+        $factory = new Jonah_Injector_Factory_Driver();
+        return $factory->getDriver($driver, $params);
+    }
+
+    /**
+     */
+    public function equals(Horde_Injector_Binder $binder)
+    {
+        return false;
+    }
+
+}
diff --git a/jonah/lib/Injector/Factory/Driver.php b/jonah/lib/Injector/Factory/Driver.php
new file mode 100644 (file)
index 0000000..cf621ea
--- /dev/null
@@ -0,0 +1,68 @@
+<?php
+/**
+ * Jonah_Driver factory.
+ * 
+ * Copyright 2010 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file LICENSE for license information (BSD). If you did not
+ * did not receive this file, see http://cvs.horde.org/co.php/jonah/LICENSE.
+ *
+ * @author Michael J. Rubinsky <mrubinsk@horde.org>
+ * @author Ben Klang <ben@alkaloid.net>
+ * @package Jonah
+ */
+class Jonah_Injector_Factory_Driver
+{
+    /**
+     * Instances.
+     *
+     * @var array
+     */
+    private $_instances = array();
+
+    /**
+     * The injector.
+     *
+     * @var Horde_Injector
+     */
+    private $_injector;
+    
+    /**
+     * Constructor.
+     *
+     * @param Horde_Injector $injector  The injector to use.
+     */
+    public function __construct(Horde_Injector $injector)
+    {
+        $this->_injector = $injector;
+    }
+
+    /**
+     * Return the driver instance.
+     *
+     * @param string $driver  The concrete driver to return
+     * @param array $params   An array of additional driver parameters.
+     *
+     * @return Jonah_Driver
+     * @throws Jonah_Exception
+     */
+    public function getDriver($driver, $params = array())
+    {
+        $driver = basename($driver);
+        $sig = md5(driver . serialize($params));
+        if (isset($this->_instances[$sig])) {
+            return $this->_instances[$sig];
+        }
+
+        $class = 'Jonah_Driver_' . $driver;
+        if (class_exists($class)) {
+            $object = new $class($params);
+            $this->_instances[$sig] = $object;
+        } else {
+            throw new Jonah_Exception(sprintf(_("No such backend \"%s\" found"), $driver));
+        }
+
+        return $this->_instances[$sig];
+    }
+
+}
\ No newline at end of file
index 030f93f..8cf8154 100644 (file)
  * @author  Jan Schneider <jan@horde.org>
  * @package Jonah
  */
-class Jonah_News {
-
+class Jonah_News
+{
     /**
      * Hash containing connection parameters.
      *
      * @var array
      */
-    var $_params = array();
+    protected $_params = array();
 
     /**
      * Constructs a new News storage object.
      *
      * @param array $params  A hash containing connection parameters.
      */
-    function Jonah_News($params = array())
+    public function __construct($params = array())
     {
         $this->_params = $params;
     }