1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Ivory Http Adapter package. |
5
|
|
|
* |
6
|
|
|
* (c) Eric GELOEN <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please read the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Ivory\HttpAdapter; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Http adapter factory. |
16
|
|
|
* |
17
|
|
|
* @author GeLo <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
class HttpAdapterFactory |
20
|
|
|
{ |
21
|
|
|
const BUZZ = 'buzz'; |
22
|
|
|
const CAKE = 'cake'; |
23
|
|
|
const CURL = 'curl'; |
24
|
|
|
const FILE_GET_CONTENTS = 'file_get_contents'; |
25
|
|
|
const FOPEN = 'fopen'; |
26
|
|
|
const GUZZLE3 = 'guzzle3'; |
27
|
|
|
const GUZZLE4 = 'guzzle4'; |
28
|
|
|
const GUZZLE5 = 'guzzle5'; |
29
|
|
|
const GUZZLE6 = 'guzzle6'; |
30
|
|
|
const HTTPFUL = 'httpful'; |
31
|
|
|
const PECL_HTTP = 'pecl_http'; |
32
|
|
|
const REACT = 'react'; |
33
|
|
|
const REQUESTS = 'requests'; |
34
|
|
|
const SOCKET = 'socket'; |
35
|
|
|
const ZEND1 = 'zend1'; |
36
|
|
|
const ZEND2 = 'zend2'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
private static $adapters = [ |
42
|
|
|
self::GUZZLE6 => [ |
43
|
|
|
'adapter' => 'Ivory\HttpAdapter\Guzzle6HttpAdapter', |
44
|
|
|
'client' => 'GuzzleHttp\Handler\CurlHandler', |
45
|
|
|
], |
46
|
|
|
self::GUZZLE5 => [ |
47
|
|
|
'adapter' => 'Ivory\HttpAdapter\Guzzle5HttpAdapter', |
48
|
|
|
'client' => 'GuzzleHttp\Ring\Client\CurlHandler', |
49
|
|
|
], |
50
|
|
|
self::GUZZLE4 => [ |
51
|
|
|
'adapter' => 'Ivory\HttpAdapter\Guzzle4HttpAdapter', |
52
|
|
|
'client' => 'GuzzleHttp\Adapter\Curl\CurlAdapter', |
53
|
|
|
], |
54
|
|
|
self::GUZZLE3 => [ |
55
|
|
|
'adapter' => 'Ivory\HttpAdapter\Guzzle3HttpAdapter', |
56
|
|
|
'client' => 'Guzzle\Http\Client', |
57
|
|
|
], |
58
|
|
|
self::ZEND2 => [ |
59
|
|
|
'adapter' => 'Ivory\HttpAdapter\Zend2HttpAdapter', |
60
|
|
|
'client' => 'Zend\Http\Client', |
61
|
|
|
], |
62
|
|
|
self::ZEND1 => [ |
63
|
|
|
'adapter' => 'Ivory\HttpAdapter\Zend1HttpAdapter', |
64
|
|
|
'client' => 'Zend_Http_Client', |
65
|
|
|
], |
66
|
|
|
self::BUZZ => [ |
67
|
|
|
'adapter' => 'Ivory\HttpAdapter\BuzzHttpAdapter', |
68
|
|
|
'client' => 'Buzz\Browser', |
69
|
|
|
], |
70
|
|
|
self::REQUESTS => [ |
71
|
|
|
'adapter' => 'Ivory\HttpAdapter\RequestsHttpAdapter', |
72
|
|
|
'client' => '\Requests', |
73
|
|
|
], |
74
|
|
|
self::REACT => [ |
75
|
|
|
'adapter' => 'Ivory\HttpAdapter\ReactHttpAdapter', |
76
|
|
|
'client' => 'React\HttpClient\Request', |
77
|
|
|
], |
78
|
|
|
self::HTTPFUL => [ |
79
|
|
|
'adapter' => 'Ivory\HttpAdapter\HttpfulHttpAdapter', |
80
|
|
|
'client' => 'Httpful\Request', |
81
|
|
|
], |
82
|
|
|
self::PECL_HTTP => [ |
83
|
|
|
'adapter' => 'Ivory\HttpAdapter\PeclHttpAdapter', |
84
|
|
|
'client' => 'http\Client', |
85
|
|
|
], |
86
|
|
|
self::CAKE => [ |
87
|
|
|
'adapter' => 'Ivory\HttpAdapter\CakeHttpAdapter', |
88
|
|
|
'client' => 'Cake\Network\Http\Client', |
89
|
|
|
], |
90
|
|
|
self::CURL => [ |
91
|
|
|
'adapter' => 'Ivory\HttpAdapter\CurlHttpAdapter', |
92
|
|
|
'client' => 'curl_init', |
93
|
|
|
], |
94
|
|
|
self::FOPEN => [ |
95
|
|
|
'adapter' => 'Ivory\HttpAdapter\FopenHttpAdapter', |
96
|
|
|
'client' => 'allow_url_fopen', |
97
|
|
|
], |
98
|
|
|
self::FILE_GET_CONTENTS => [ |
99
|
|
|
'adapter' => 'Ivory\HttpAdapter\FileGetContentsHttpAdapter', |
100
|
|
|
'client' => 'allow_url_fopen', |
101
|
|
|
], |
102
|
|
|
self::SOCKET => [ |
103
|
|
|
'adapter' => 'Ivory\HttpAdapter\SocketHttpAdapter', |
104
|
|
|
'client' => 'stream_socket_client', |
105
|
|
|
], |
106
|
|
|
]; |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param string $name |
110
|
|
|
* @param string $class |
111
|
|
|
* @param string|null $client |
112
|
|
|
* |
113
|
|
|
* @throws HttpAdapterException |
114
|
|
|
*/ |
115
|
45 |
|
public static function register($name, $class, $client = null) |
116
|
|
|
{ |
117
|
45 |
|
if (!in_array('Ivory\HttpAdapter\HttpAdapterInterface', class_implements($class), true)) { |
118
|
9 |
|
throw HttpAdapterException::httpAdapterMustImplementInterface($class); |
119
|
|
|
} |
120
|
|
|
|
121
|
36 |
|
$adapter = ['adapter' => $class]; |
122
|
|
|
|
123
|
36 |
|
if ($client !== null) { |
124
|
18 |
|
$adapter['client'] = $client; |
125
|
14 |
|
} |
126
|
|
|
|
127
|
36 |
|
self::unregister($name); |
128
|
36 |
|
self::$adapters = array_merge([$name => $adapter], self::$adapters); |
129
|
36 |
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param string $name |
133
|
|
|
*/ |
134
|
45 |
|
public static function unregister($name) |
135
|
|
|
{ |
136
|
45 |
|
unset(self::$adapters[$name]); |
137
|
45 |
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param string $name |
141
|
|
|
* |
142
|
|
|
* @return bool |
143
|
|
|
*/ |
144
|
444 |
|
public static function capable($name) |
145
|
|
|
{ |
146
|
444 |
|
return isset(self::$adapters[$name]) |
147
|
440 |
|
&& (!isset(self::$adapters[$name]['client']) |
148
|
424 |
|
|| (class_exists(self::$adapters[$name]['client']) |
149
|
140 |
|
|| function_exists(self::$adapters[$name]['client']) |
150
|
444 |
|
|| ini_get(self::$adapters[$name]['client']))); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param string $name |
155
|
|
|
* |
156
|
|
|
* @throws HttpAdapterException |
157
|
|
|
* |
158
|
|
|
* @return HttpAdapterInterface |
159
|
|
|
*/ |
160
|
305 |
|
public static function create($name) |
161
|
|
|
{ |
162
|
305 |
|
if (!isset(self::$adapters[$name])) { |
163
|
9 |
|
throw HttpAdapterException::httpAdapterDoesNotExist($name); |
164
|
|
|
} |
165
|
|
|
|
166
|
296 |
|
if (!self::capable($name)) { |
167
|
9 |
|
throw HttpAdapterException::httpAdapterIsNotUsable($name); |
168
|
|
|
} |
169
|
|
|
|
170
|
287 |
|
return new self::$adapters[$name]['adapter'](); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @param string|array $preferred |
175
|
|
|
* |
176
|
|
|
* @throws HttpAdapterException |
177
|
|
|
* |
178
|
|
|
* @return HttpAdapterInterface |
179
|
|
|
*/ |
180
|
157 |
|
public static function guess($preferred = []) |
181
|
|
|
{ |
182
|
157 |
|
$adapters = self::$adapters; |
183
|
|
|
|
184
|
157 |
|
foreach ((array) $preferred as $preference) { |
185
|
139 |
|
if (self::capable($preference)) { |
186
|
130 |
|
return self::create($preference); |
187
|
|
|
} |
188
|
|
|
|
189
|
18 |
|
unset($adapters[$preference]); |
190
|
28 |
|
} |
191
|
|
|
|
192
|
27 |
|
foreach (array_keys($adapters) as $name) { |
193
|
27 |
|
if (self::capable($name)) { |
194
|
20 |
|
return self::create($name); |
195
|
|
|
} |
196
|
15 |
|
} |
197
|
|
|
|
198
|
9 |
|
throw HttpAdapterException::httpAdaptersAreNotUsable(); |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|