1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Part of the Joomla Framework Http Package |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (C) 2005 - 2016 Open Source Matters, Inc. All rights reserved. |
6
|
|
|
* @license GNU General Public License version 2 or later; see LICENSE |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Joomla\Http; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* HTTP factory class. |
13
|
|
|
* |
14
|
|
|
* @since 1.0 |
15
|
|
|
*/ |
16
|
|
|
class HttpFactory |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Method to create an Http instance. |
20
|
|
|
* |
21
|
|
|
* @param array|\ArrayAccess $options Client options array. |
22
|
|
|
* @param array|string $adapters Adapter (string) or queue of adapters (array) to use for communication. |
23
|
|
|
* |
24
|
|
|
* @return Http |
25
|
|
|
* |
26
|
|
|
* @since 1.0 |
27
|
|
|
* @throws \InvalidArgumentException |
28
|
|
|
* @throws \RuntimeException |
29
|
|
|
*/ |
30
|
|
|
public static function getHttp($options = array(), $adapters = null) |
31
|
|
|
{ |
32
|
|
|
if (!is_array($options) && !($options instanceof \ArrayAccess)) |
33
|
|
|
{ |
34
|
|
|
throw new \InvalidArgumentException( |
35
|
|
|
'The options param must be an array or implement the ArrayAccess interface.' |
36
|
|
|
); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
if (!$driver = self::getAvailableDriver($options, $adapters)) |
40
|
|
|
{ |
41
|
|
|
throw new \RuntimeException('No transport driver available.'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return new Http($options, $driver); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Finds an available TransportInterface object for communication |
49
|
|
|
* |
50
|
|
|
* @param array|\ArrayAccess $options Options for creating TransportInterface object |
51
|
|
|
* @param array|string $default Adapter (string) or queue of adapters (array) to use |
52
|
|
|
* |
53
|
|
|
* @return TransportInterface|boolean Interface sub-class or boolean false if no adapters are available |
54
|
|
|
* |
55
|
|
|
* @since 1.0 |
56
|
|
|
* @throws \InvalidArgumentException |
57
|
|
|
*/ |
58
|
|
|
public static function getAvailableDriver($options = array(), $default = null) |
59
|
|
|
{ |
60
|
|
|
if (!is_array($options) && !($options instanceof \ArrayAccess)) |
61
|
|
|
{ |
62
|
|
|
throw new \InvalidArgumentException( |
63
|
|
|
'The options param must be an array or implement the ArrayAccess interface.' |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if (is_null($default)) |
68
|
|
|
{ |
69
|
|
|
$availableAdapters = self::getHttpTransports(); |
70
|
|
|
} |
71
|
|
|
else |
72
|
|
|
{ |
73
|
|
|
settype($default, 'array'); |
74
|
|
|
$availableAdapters = $default; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
// Check if there is at least one available http transport adapter |
78
|
|
|
if (!count($availableAdapters)) |
79
|
|
|
{ |
80
|
|
|
return false; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
foreach ($availableAdapters as $adapter) |
84
|
|
|
{ |
85
|
|
|
/* @var $class TransportInterface */ |
86
|
|
|
$class = 'Joomla\\Http\\Transport\\' . ucfirst($adapter); |
87
|
|
|
|
88
|
|
|
if (class_exists($class)) |
89
|
|
|
{ |
90
|
|
|
if ($class::isSupported()) |
91
|
|
|
{ |
92
|
|
|
return new $class($options); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return false; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get the HTTP transport handlers |
102
|
|
|
* |
103
|
|
|
* @return array An array of available transport handlers |
104
|
|
|
* |
105
|
|
|
* @since 1.0 |
106
|
|
|
*/ |
107
|
|
|
public static function getHttpTransports() |
108
|
|
|
{ |
109
|
|
|
$names = array(); |
110
|
|
|
$iterator = new \DirectoryIterator(__DIR__ . '/Transport'); |
111
|
|
|
|
112
|
|
|
/* @var $file \DirectoryIterator */ |
113
|
|
|
foreach ($iterator as $file) |
114
|
|
|
{ |
115
|
|
|
$fileName = $file->getFilename(); |
116
|
|
|
|
117
|
|
|
// Only load for php files. |
118
|
|
|
if ($file->isFile() && $file->getExtension() == 'php') |
119
|
|
|
{ |
120
|
|
|
$names[] = substr($fileName, 0, strrpos($fileName, '.')); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
// Keep alphabetical order across all environments |
125
|
|
|
sort($names); |
126
|
|
|
|
127
|
|
|
// If curl is available set it to the first position |
128
|
|
|
$key = array_search('Curl', $names); |
129
|
|
|
|
130
|
|
|
if ($key) |
131
|
|
|
{ |
132
|
|
|
unset($names[$key]); |
133
|
|
|
array_unshift($names, 'Curl'); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return $names; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|