mirror of
https://github.com/joomla-extensions/patchtester.git
synced 2024-06-03 22:20:48 +00:00
Use JHttp
This commit is contained in:
parent
18738067f1
commit
961a8c7a35
|
@ -1,195 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* @package Joomla.Platform
|
||||
* @subpackage Client
|
||||
*
|
||||
* @copyright Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
|
||||
* @license GNU General Public License version 2 or later; see LICENSE
|
||||
*/
|
||||
|
||||
defined('JPATH_PLATFORM') or die;
|
||||
|
||||
/**
|
||||
* A cURL client class.
|
||||
*
|
||||
* @TODO: Use the Joomla! Curl class
|
||||
*
|
||||
* @package Joomla.Platform
|
||||
* @subpackage Client
|
||||
* @since ?
|
||||
*/
|
||||
class PTCurl extends JURI
|
||||
{
|
||||
/**
|
||||
* @var array curlOptions.
|
||||
*/
|
||||
protected $curlOptions = array();
|
||||
|
||||
/**
|
||||
* @var string Target path where to save the response
|
||||
*/
|
||||
protected $target = '';
|
||||
|
||||
/**
|
||||
* Get a cURL adapter.
|
||||
*
|
||||
* @param string $uri The URI to work with.
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return PTCurl
|
||||
*/
|
||||
public static function getAdapter($uri = null)
|
||||
{
|
||||
if (false == function_exists('curl_init'))
|
||||
{
|
||||
throw new Exception('cURL is not available');
|
||||
}
|
||||
|
||||
return new PTCurl($uri);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* You should pass a URI string to the constructor to initialise a specific URI.
|
||||
*
|
||||
* @param string $uri The URI string
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public function __construct($uri = null)
|
||||
{
|
||||
if (false == function_exists('curl_init'))
|
||||
{
|
||||
throw new Exception('cURL is not available');
|
||||
}
|
||||
|
||||
parent::__construct($uri);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set cURL options.
|
||||
*
|
||||
* @param array $options The cURL options.
|
||||
*
|
||||
* @return PTCurl
|
||||
*/
|
||||
public function setOptions($options)
|
||||
{
|
||||
$this->curlOptions = (array)$options;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read URL contents.
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return object The cURL response.
|
||||
*/
|
||||
public function fetch()
|
||||
{
|
||||
$ch = curl_init();
|
||||
|
||||
curl_setopt_array($ch, $this->curlOptions);
|
||||
|
||||
curl_setopt($ch, CURLOPT_URL, $this->uri);
|
||||
|
||||
if ( ! array_key_exists(CURLOPT_SSL_VERIFYHOST, $this->curlOptions))
|
||||
{
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true);
|
||||
}
|
||||
|
||||
if ( ! array_key_exists(CURLOPT_SSL_VERIFYPEER, $this->curlOptions))
|
||||
{
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
||||
}
|
||||
|
||||
if ( ! array_key_exists(CURLOPT_FOLLOWLOCATION, $this->curlOptions))
|
||||
{
|
||||
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
|
||||
}
|
||||
|
||||
if ( ! array_key_exists(CURLOPT_MAXREDIRS, $this->curlOptions))
|
||||
{
|
||||
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
|
||||
}
|
||||
|
||||
if ( ! array_key_exists(CURLOPT_TIMEOUT, $this->curlOptions))
|
||||
{
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
|
||||
}
|
||||
|
||||
if ( ! array_key_exists(CURLOPT_RETURNTRANSFER, $this->curlOptions))
|
||||
{
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
}
|
||||
|
||||
if ($this->target)
|
||||
{
|
||||
// Write the response to a file
|
||||
$fp = fopen($this->target, 'w');
|
||||
|
||||
if ( ! $fp)
|
||||
{
|
||||
throw new Exception('Can not open target file at: '.$this->target);
|
||||
}
|
||||
|
||||
// Use CURLOPT_FILE to speed things up
|
||||
curl_setopt($ch, CURLOPT_FILE, $fp);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Return the response
|
||||
if ( ! array_key_exists(CURLOPT_RETURNTRANSFER, $this->curlOptions))
|
||||
{
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
}
|
||||
}
|
||||
|
||||
$response = curl_exec($ch);
|
||||
|
||||
if (curl_errno($ch))
|
||||
{
|
||||
throw new Exception('Curl Error: '.curl_error($ch));
|
||||
}
|
||||
|
||||
$info = curl_getinfo($ch);
|
||||
|
||||
if (isset($info['http_code']) && $info['http_code'] != 200)
|
||||
{
|
||||
$response = false;
|
||||
}
|
||||
|
||||
curl_close($ch);
|
||||
|
||||
$return = JArrayHelper::toObject($info);
|
||||
$return->body = $response;
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save the response to a file.
|
||||
*
|
||||
* @param string $target Target path
|
||||
*
|
||||
* @return boolean true on success
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public function saveToFile($target)
|
||||
{
|
||||
$this->target = $target;
|
||||
|
||||
$response = $this->fetch();
|
||||
|
||||
if (false === $response)
|
||||
{
|
||||
throw new Exception('File cannot be downloaded');
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -14,6 +14,31 @@ defined('_JEXEC') or die;
|
|||
*/
|
||||
class PatchtesterModelPull extends JModelLegacy
|
||||
{
|
||||
/**
|
||||
* @var JHttp
|
||||
*/
|
||||
protected $transport;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param array $config An array of configuration options (name, state, dbo, table_path, ignore_request).
|
||||
*
|
||||
* @since 12.2
|
||||
* @throws Exception
|
||||
*/
|
||||
public function __construct($config = array())
|
||||
{
|
||||
parent::__construct($config);
|
||||
|
||||
// Set up the JHttp object
|
||||
$options = new JRegistry;
|
||||
$options->set('userAgent', 'JPatchTester/1.0');
|
||||
$options->set('timeout', 120);
|
||||
|
||||
$this->transport = JHttpFactory::getHttp($options, 'curl');
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to auto-populate the model state.
|
||||
*
|
||||
|
@ -104,8 +129,7 @@ class PatchtesterModelPull extends JModelLegacy
|
|||
throw new Exception(JText::_('COM_PATCHTESTER_REPO_IS_GONE'));
|
||||
}
|
||||
|
||||
$patch = PTCurl::getAdapter($pull->diff_url)
|
||||
->fetch()->body;
|
||||
$patch = $this->transport->get($pull->diff_url)->body;
|
||||
|
||||
$files = $this->parsePatch($patch);
|
||||
|
||||
|
@ -132,8 +156,7 @@ class PatchtesterModelPull extends JModelLegacy
|
|||
$url = 'https://raw.github.com/' . $pull->head->user->login . '/' . $pull->head->repo->name . '/' .
|
||||
$pull->head->ref . '/' . $file->new;
|
||||
|
||||
$file->body = PTCurl::getAdapter($url)
|
||||
->fetch()->body;
|
||||
$file->body = $this->transport->get($url)->body;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user