Use a factory instead of a binder for Horde_Http_Client.
authorJan Schneider <jan@horde.org>
Tue, 6 Jul 2010 18:29:19 +0000 (20:29 +0200)
committerJan Schneider <jan@horde.org>
Tue, 6 Jul 2010 18:37:24 +0000 (20:37 +0200)
framework/Core/lib/Horde/Core/Binder/Facebook.php
framework/Core/lib/Horde/Core/Binder/HttpClient.php
framework/Core/lib/Horde/Core/Binder/Twitter.php
framework/Core/lib/Horde/Core/Factory/HttpClient.php [new file with mode: 0644]
framework/Core/package.xml
horde/lib/Block/feed/reader.php
imp/lib/Compose.php
jonah/lib/Jonah.php

index ba2be04..e69af3f 100644 (file)
@@ -18,7 +18,7 @@ class Horde_Core_Binder_Facebook implements Horde_Injector_Binder
         $secret = $conf['facebook']['secret'];
 
         /* Create required objects */
-        $context = array('http_client' => $injector->getInstance('Horde_Http_Client'),
+        $context = array('http_client' => $injector->getInstance('Horde_Http_Client')->getClient(),
                          'http_request' => $injector->getInstance('Horde_Controller_Request_Http'));
 
        return new Horde_Service_Facebook($apikey, $secret, $context);
index 235ceb7..f42f812 100644 (file)
@@ -7,22 +7,7 @@ class Horde_Core_Binder_HttpClient implements Horde_Injector_Binder
 {
     public function create(Horde_Injector $injector)
     {
-        global $conf;
-
-        $client_opts = array();
-
-        if (!empty($conf['http']['proxy']['proxy_host'])) {
-            $client_opts['request.proxyServer'] = $conf['http']['proxy']['proxy_host'];
-            $client_opts['request.proxyPort'] = $conf['http']['proxy']['proxy_port'];
-            if (!empty($conf['http']['proxy']['proxy_user'])) {
-                $client_opts['request.proxyUsername'] = $conf['http']['proxy']['proxy_user'];
-                if (!empty($conf['http']['proxy']['proxy_pass'])) {
-                    $client_opts['request.proxyPassword'] = $conf['http']['proxy']['proxy_pass'];
-                }
-            }
-        }
-
-        return new Horde_Http_Client($client_opts);
+        return new Horde_Core_Factory_HttpClient($injector);
     }
 
     public function equals(Horde_Injector_Binder $binder)
index c032111..b2c4485 100644 (file)
@@ -35,7 +35,7 @@ class Horde_Core_Binder_Twitter implements Horde_Injector_Binder
 
             //$twitter->setCache($injector->getInstance('Horde_Cache'));
             $twitter->setLogger($injector->getInstance('Horde_Log_Logger'));
-            $twitter->setHttpClient($GLOBALS['injector']->getInstance('Horde_Http_Client'));
+            $twitter->setHttpClient($GLOBALS['injector']->getInstance('Horde_Http_Client')->getClient());
 
             return $twitter;
         } else {
diff --git a/framework/Core/lib/Horde/Core/Factory/HttpClient.php b/framework/Core/lib/Horde/Core/Factory/HttpClient.php
new file mode 100644 (file)
index 0000000..9530b18
--- /dev/null
@@ -0,0 +1,43 @@
+<?php
+/**
+ * @category Horde
+ * @package  Core
+ */
+class Horde_Core_Factory_HttpClient
+{
+    /**
+     * 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;
+    }
+
+    public function getClient(array $opts = array())
+    {
+        global $conf;
+
+        $client_opts = array();
+        if (!empty($conf['http']['proxy']['proxy_host'])) {
+            $client_opts['request.proxyServer'] = $conf['http']['proxy']['proxy_host'];
+            $client_opts['request.proxyPort'] = $conf['http']['proxy']['proxy_port'];
+            if (!empty($conf['http']['proxy']['proxy_user'])) {
+                $client_opts['request.proxyUsername'] = $conf['http']['proxy']['proxy_user'];
+                if (!empty($conf['http']['proxy']['proxy_pass'])) {
+                    $client_opts['request.proxyPassword'] = $conf['http']['proxy']['proxy_pass'];
+                }
+            }
+        }
+
+        return new Horde_Http_Client(array_merge($client_opts, $opts));
+    }
+}
\ No newline at end of file
index 7940643..e821ada 100644 (file)
@@ -115,6 +115,7 @@ Application Framework.</description>
        <file name="Db.php" role="php" />
        <file name="DbPear.php" role="php" />
        <file name="Editor.php" role="php" />
+       <file name="HttpClient.php" role="php" />
        <file name="Identity.php" role="php" />
        <file name="KolabServer.php" role="php" />
        <file name="KolabSession.php" role="php" />
@@ -327,6 +328,7 @@ Application Framework.</description>
    <install as="Horde/Core/Factory/Db.php" name="lib/Horde/Core/Factory/Db.php" />
    <install as="Horde/Core/Factory/DbPear.php" name="lib/Horde/Core/Factory/DbPear.php" />
    <install as="Horde/Core/Factory/Editor.php" name="lib/Horde/Core/Factory/Editor.php" />
+   <install as="Horde/Core/Factory/HttpClient.php" name="lib/Horde/Core/Factory/HttpClient.php" />
    <install as="Horde/Core/Factory/Identity.php" name="lib/Horde/Core/Factory/Identity.php" />
    <install as="Horde/Core/Factory/KolabServer.php" name="lib/Horde/Core/Factory/KolabServer.php" />
    <install as="Horde/Core/Factory/KolabSession.php" name="lib/Horde/Core/Factory/KolabSession.php" />
index d2b9e8d..3c5bde9 100644 (file)
@@ -21,11 +21,10 @@ class Horde_Block_Horde_feed_reader {
         }
 
         try {
-            if (!empty($GLOBALS['conf']['http']['proxy']['proxy_host'])) {
-                Horde_Feed::setHttpClient($GLOBALS['injector']->getInstance('Horde_Http_Client'));
-            }
-
-            $feed = Horde_Feed::readUri($uri);
+            $client = $GLOBALS['injector']
+              ->getInstance('Horde_Http_Client')
+              ->getClient();
+            $feed = Horde_Feed::readUri($uri, $client);
             $GLOBALS['cache']->set($key, serialize($feed));
             return $feed;
         } catch (Exception $e) {
index ca50e05..a9a5630 100644 (file)
@@ -2269,7 +2269,9 @@ class IMP_Compose
             return $mime_part;
         }
 
-        $client = $GLOBALS['injector']->getInstance('Horde_Http_Client');
+        $client = $GLOBALS['injector']
+          ->getInstance('Horde_Http_Client')
+          ->getClient();
         $img_data = $img_parts = array();
 
         /* Go through list of results, download the image, and create
index db56770..c26a29b 100644 (file)
@@ -44,7 +44,9 @@ class Jonah
     {
         global $conf;
 
-        $http = $GLOBALS['injector']->getInstance('Horde_Http_Client');
+        $http = $GLOBALS['injector']
+          ->getInstance('Horde_Http_Client')
+          ->getClient();
 
         try {
             $response = $http->get($url);