- md5
- 5e34aeb2f897a5eecab3f8b1a7e829f2
- sha1
- 7ba0d059d0ee5539a33048428d0639e0607c0fc3
- sha256
- ae34834caee4cbaf6e939cb620ab2acab28dfbadd0840ab6cfec9f97ddf6dd23
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
<?php
/**
* Joomla! Content Management System
*
* @copyright Copyright (C) 2005 - 2020 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\CMS\Http;
\defined('JPATH_PLATFORM') or die;
use Joomla\CMS\Version;
use Joomla\Http\TransportInterface;
/**
* HTTP factory class.
*
* @since 3.0.0
*/
class HttpFactory
{
/**
* Method to create a JHttp instance.
*
* @param array|\ArrayAccess $options Client options array.
* @param array|string $adapters Adapter (string) or queue of adapters (array) to use for communication.
*
* @return Http
*
* @since 3.0.0
* @throws \RuntimeException
*/
public static function getHttp($options = [], $adapters = null)
{
if (!\is_array($options) && !($options instanceof \ArrayAccess))
{
throw new \InvalidArgumentException(
'The options param must be an array or implement the ArrayAccess interface.'
);
}
// Set default userAgent if nothing else is set
if (!isset($options['userAgent']))
{
$version = new Version;
$options['userAgent'] = $version->getUserAgent('Joomla', true, false);
}
if (!$driver = static::getAvailableDriver($options, $adapters))
{
throw new \RuntimeException('No transport driver available.');
}
return new Http($options, $driver);
}
/**
* Finds an available http transport object for communication
*
* @param array|\ArrayAccess $options Options for creating TransportInterface object
* @param array|string $default Adapter (string) or queue of adapters (array) to use
*
* @return TransportInterface|boolean Interface sub-class or boolean false if no adapters are available
*
* @since 3.0.0
*/
public static function getAvailableDriver($options = [], $default = null)
{
if (\is_null($default))
{
$availableAdapters = static::getHttpTransports();
}
else
{
settype($default, 'array');
$availableAdapters = $default;
}
// Check if there is at least one available http transport adapter
if (!\count($availableAdapters))
{
return false;
}
foreach ($availableAdapters as $adapter)
{
/** @var $class TransportInterface */
$class = __NAMESPACE__ . '\\Transport\\' . ucfirst($adapter) . 'Transport';
if (!class_exists($class))
{
$class = 'JHttpTransport' . ucfirst($adapter);
}
if (class_exists($class) && $class::isSupported())
{
return new $class($options);
}
}
return false;
}
/**
* Get the http transport handlers
*
* @return array An array of available transport handlers
*
* @since 3.0.0
*/
public static function getHttpTransports()
{
$names = array();
$iterator = new \DirectoryIterator(__DIR__ . '/Transport');
/** @type $file \DirectoryIterator */
foreach ($iterator as $file)
{
$fileName = $file->getFilename();
// Only load for php files.
if ($file->isFile() && $file->getExtension() === 'php')
{
$names[] = substr($fileName, 0, strrpos($fileName, 'Transport.'));
}
}
// Keep alphabetical order across all environments
sort($names);
// If curl is available set it to the first position
if ($key = array_search('Curl', $names))
{
unset($names[$key]);
array_unshift($names, 'Curl');
}
return $names;
}
}
The same content is in 5 releases, Joomla 4.0.0-beta to 4.0.0-beta5. See them all, or every version of this file.