Move automatic request object generation to a factory method
authorChuck Hagenbuch <chuck@horde.org>
Sun, 27 Sep 2009 16:24:20 +0000 (12:24 -0400)
committerChuck Hagenbuch <chuck@horde.org>
Sun, 27 Sep 2009 16:24:20 +0000 (12:24 -0400)
framework/Http/lib/Horde/Http/Client.php
framework/Http/lib/Horde/Http/Request/Factory.php [new file with mode: 0644]
framework/Http/package.xml

index 097170b..e27533d 100644 (file)
@@ -56,7 +56,8 @@ class Horde_Http_Client
             $this->_request = $args['request'];
             unset($args['request']);
         } else {
-            $this->_request = $this->_createBestAvailableRequest();
+            $requestFactory = new Horde_Http_Request_Factory();
+            $this->_request = $requestFactory->create();
         }
 
         foreach ($args as $key => $val) {
@@ -172,22 +173,4 @@ class Horde_Http_Client
     {
         $this->{'_' . $name} = $value;
     }
-
-    /**
-     * Find the best available request backend
-     *
-     * @return Horde_Http_Request_Base
-     */
-    public function _createBestAvailableRequest()
-    {
-        if (class_exists('HttpRequest', false)) {
-            return new Horde_Http_Request_Peclhttp();
-        } elseif (extension_loaded('curl')) {
-            return new Horde_Http_Request_Curl();
-        } elseif (ini_get('allow_url_fopen')) {
-            return new Horde_Http_Request_Fopen();
-        } else {
-            throw new Horde_Http_Exception('No HTTP request backends are available. You must install pecl_http, curl, or enable allow_url_fopen.');
-        }
-    }
 }
diff --git a/framework/Http/lib/Horde/Http/Request/Factory.php b/framework/Http/lib/Horde/Http/Request/Factory.php
new file mode 100644 (file)
index 0000000..75614e0
--- /dev/null
@@ -0,0 +1,36 @@
+<?php
+/**
+ * Copyright 2007-2009 The Horde Project (http://www.horde.org/)
+ *
+ * @author   Chuck Hagenbuch <chuck@horde.org>
+ * @license  http://opensource.org/licenses/bsd-license.php BSD
+ * @category Horde
+ * @package  Horde_Http
+ */
+
+/**
+ * @author   Chuck Hagenbuch <chuck@horde.org>
+ * @license  http://opensource.org/licenses/bsd-license.php BSD
+ * @category Horde
+ * @package  Horde_Http
+ */
+class Horde_Http_Request_Factory
+{
+    /**
+     * Find the best available request backend
+     *
+     * @return Horde_Http_Request_Base
+     */
+    public function create()
+    {
+        if (class_exists('HttpRequest', false)) {
+            return new Horde_Http_Request_Peclhttp();
+        } elseif (extension_loaded('curl')) {
+            return new Horde_Http_Request_Curl();
+        } elseif (ini_get('allow_url_fopen')) {
+            return new Horde_Http_Request_Fopen();
+        } else {
+            throw new Horde_Http_Exception('No HTTP request backends are available. You must install pecl_http, curl, or enable allow_url_fopen.');
+        }
+    }
+}
index 2d9a647..89d9d69 100644 (file)
@@ -35,6 +35,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
       <dir name="Request">
        <file name="Base.php" role="php" />
        <file name="Curl.php" role="php" />
+       <file name="Factory.php" role="php" />
        <file name="Fopen.php" role="php" />
        <file name="Mock.php" role="php" />
        <file name="Peclhttp.php" role="php" />
@@ -73,6 +74,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
    <install name="lib/Horde/Http/Exception.php" as="Horde/Http/Exception.php" />
    <install name="lib/Horde/Http/Request/Base.php" as="Horde/Http/Request/Base.php" />
    <install name="lib/Horde/Http/Request/Curl.php" as="Horde/Http/Request/Curl.php" />
+   <install name="lib/Horde/Http/Request/Factory.php" as="Horde/Http/Request/Factory.php" />
    <install name="lib/Horde/Http/Request/Fopen.php" as="Horde/Http/Request/Fopen.php" />
    <install name="lib/Horde/Http/Request/Mock.php" as="Horde/Http/Request/Mock.php" />
    <install name="lib/Horde/Http/Request/Peclhttp.php" as="Horde/Http/Request/Peclhttp.php" />