-
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathHttpFactory.php
More file actions
130 lines (111 loc) · 3.76 KB
/
HttpFactory.php
File metadata and controls
130 lines (111 loc) · 3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
/**
* Part of the Joomla Framework Http Package
*
* @copyright Copyright (C) 2005 - 2021 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
namespace Joomla\Http;
/**
* HTTP factory class.
*
* @since 1.0
*/
class HttpFactory
{
/**
* Method to create an Http 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 1.0
* @throws \InvalidArgumentException
* @throws \RuntimeException
*/
public 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'])) {
$options['userAgent'] = 'Mozilla/5.0 Joomla!/Framework';
}
if (!$driver = $this->getAvailableDriver($options, $adapters)) {
throw new \RuntimeException('No transport driver available.');
}
return new Http($options, $driver);
}
/**
* Finds an available TransportInterface 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 1.0
* @throws \InvalidArgumentException
*/
public function getAvailableDriver($options = [], $default = null)
{
if (!\is_array($options) && !($options instanceof \ArrayAccess)) {
throw new \InvalidArgumentException(
'The options param must be an array or implement the ArrayAccess interface.'
);
}
if ($default === null) {
$availableAdapters = $this->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) {
$class = __NAMESPACE__ . '\\Transport\\' . ucfirst($adapter);
if (class_exists($class)) {
if ($class::isSupported()) {
return new $class($options);
}
}
}
return false;
}
/**
* Get the HTTP transport handlers
*
* @return string[] An array of available transport handler types
*
* @since 1.0
*/
public function getHttpTransports()
{
$names = [];
$iterator = new \DirectoryIterator(__DIR__ . '/Transport');
/** @var \DirectoryIterator $file */
foreach ($iterator as $file) {
$fileName = $file->getFilename();
// Only load for php files.
if ($file->isFile() && $file->getExtension() == 'php') {
$names[] = substr($fileName, 0, strrpos($fileName, '.'));
}
}
// Keep alphabetical order across all environments
sort($names);
// If curl is available set it to the first position
$key = array_search('Curl', $names);
if ($key) {
unset($names[$key]);
array_unshift($names, 'Curl');
}
return $names;
}
}