aboutsummaryrefslogtreecommitdiffstats
path: root/includes/http/HttpRequestFactory.php
diff options
context:
space:
mode:
authorStanislav Malyshev <smalyshev@gmail.com>2018-01-16 15:11:08 -0800
committerKunal Mehta <legoktm@member.fsf.org>2018-01-23 11:39:02 -0800
commitebbfe592fe02e38ab2201c0141e7468284e8ff68 (patch)
treeecfd84fc5925cadb648fd0c2aaa790bda8ac42bf /includes/http/HttpRequestFactory.php
parent27f996e866613c861f982c312b642d0f0304e035 (diff)
downloadmediawikicore-ebbfe592fe02e38ab2201c0141e7468284e8ff68.tar.gz
mediawikicore-ebbfe592fe02e38ab2201c0141e7468284e8ff68.zip
Create factory for MWHttpRequest
This will allow classes that need MWHttpRequest to inject HttpRequestFactory and thus make it overridable and testable. Also made MWHttpRequest abstract class since it doesn't implement execute anyway. Maybe a good idea to move execute to an abstract method? Change-Id: I5c0e035542ff5f791a21a95ed13bed4cea6906d0
Diffstat (limited to 'includes/http/HttpRequestFactory.php')
-rw-r--r--includes/http/HttpRequestFactory.php82
1 files changed, 82 insertions, 0 deletions
diff --git a/includes/http/HttpRequestFactory.php b/includes/http/HttpRequestFactory.php
new file mode 100644
index 000000000000..80f9b6886281
--- /dev/null
+++ b/includes/http/HttpRequestFactory.php
@@ -0,0 +1,82 @@
+<?php
+/**
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ */
+namespace MediaWiki\Http;
+
+use CurlHttpRequest;
+use DomainException;
+use Http;
+use MediaWiki\Logger\LoggerFactory;
+use MWHttpRequest;
+use PhpHttpRequest;
+use Profiler;
+
+/**
+ * Factory creating MWHttpRequest objects.
+ */
+class HttpRequestFactory {
+
+ /**
+ * Generate a new MWHttpRequest object
+ * @param string $url Url to use
+ * @param array $options (optional) extra params to pass (see Http::request())
+ * @param string $caller The method making this request, for profiling
+ * @throws DomainException
+ * @return MWHttpRequest
+ * @see MWHttpRequest::__construct
+ */
+ public function create( $url, array $options = [], $caller = __METHOD__ ) {
+ if ( !Http::$httpEngine ) {
+ Http::$httpEngine = function_exists( 'curl_init' ) ? 'curl' : 'php';
+ } elseif ( Http::$httpEngine == 'curl' && !function_exists( 'curl_init' ) ) {
+ throw new DomainException( __METHOD__ . ': curl (http://php.net/curl) is not installed, but' .
+ ' Http::$httpEngine is set to "curl"' );
+ }
+
+ if ( !isset( $options['logger'] ) ) {
+ $options['logger'] = LoggerFactory::getInstance( 'http' );
+ }
+
+ switch ( Http::$httpEngine ) {
+ case 'curl':
+ return new CurlHttpRequest( $url, $options, $caller, Profiler::instance() );
+ case 'php':
+ if ( !wfIniGetBool( 'allow_url_fopen' ) ) {
+ throw new DomainException( __METHOD__ . ': allow_url_fopen ' .
+ 'needs to be enabled for pure PHP http requests to ' .
+ 'work. If possible, curl should be used instead. See ' .
+ 'http://php.net/curl.'
+ );
+ }
+ return new PhpHttpRequest( $url, $options, $caller, Profiler::instance() );
+ default:
+ throw new DomainException( __METHOD__ . ': The setting of Http::$httpEngine is not valid.' );
+ }
+ }
+
+ /**
+ * Simple function to test if we can make any sort of requests at all, using
+ * cURL or fopen()
+ * @return bool
+ */
+ public function canMakeRequests() {
+ return function_exists( 'curl_init' ) || wfIniGetBool( 'allow_url_fopen' );
+ }
+
+}