From 6573179c4dd65c8761d383737758d30926ec09f6 Mon Sep 17 00:00:00 2001 From: "Michael J. Rubinsky" Date: Mon, 28 Jun 2010 14:52:32 -0400 Subject: [PATCH] use the injector for Jonah_Driver --- jonah/channels/delete.php | 2 +- jonah/feed.php | 4 +-- jonah/lib/Api.php | 2 +- jonah/lib/Application.php | 2 ++ jonah/lib/Injector/Binder/Driver.php | 31 ++++++++++++++++ jonah/lib/Injector/Factory/Driver.php | 68 +++++++++++++++++++++++++++++++++++ jonah/lib/News.php | 8 ++--- 7 files changed, 109 insertions(+), 8 deletions(-) create mode 100644 jonah/lib/Injector/Binder/Driver.php create mode 100644 jonah/lib/Injector/Factory/Driver.php diff --git a/jonah/channels/delete.php b/jonah/channels/delete.php index 1b3d043a3..0efe92e9a 100644 --- a/jonah/channels/delete.php +++ b/jonah/channels/delete.php @@ -11,7 +11,7 @@ * @author Marko Djukic */ -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'; diff --git a/jonah/feed.php b/jonah/feed.php index 102437769..254f048ea 100644 --- a/jonah/feed.php +++ b/jonah/feed.php @@ -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)) { diff --git a/jonah/lib/Api.php b/jonah/lib/Api.php index 6d67dda47..3c17421ca 100644 --- a/jonah/lib/Api.php +++ b/jonah/lib/Api.php @@ -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; } diff --git a/jonah/lib/Application.php b/jonah/lib/Application.php index 4d61a801e..ba8a6b896 100644 --- a/jonah/lib/Application.php +++ b/jonah/lib/Application.php @@ -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 index 000000000..8b7cd3dea --- /dev/null +++ b/jonah/lib/Injector/Binder/Driver.php @@ -0,0 +1,31 @@ + + * @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 index 000000000..cf621ea75 --- /dev/null +++ b/jonah/lib/Injector/Factory/Driver.php @@ -0,0 +1,68 @@ + + * @author Ben Klang + * @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 diff --git a/jonah/lib/News.php b/jonah/lib/News.php index 030f93f38..8cf81540a 100644 --- a/jonah/lib/News.php +++ b/jonah/lib/News.php @@ -13,21 +13,21 @@ * @author Jan Schneider * @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; } -- 2.11.0