|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright 2017 Facebook, Inc. |
|
4
|
|
|
* |
|
5
|
|
|
* You are hereby granted a non-exclusive, worldwide, royalty-free license to |
|
6
|
|
|
* use, copy, modify, and distribute this software in source code or binary |
|
7
|
|
|
* form for use in connection with the web services and APIs provided by |
|
8
|
|
|
* Facebook. |
|
9
|
|
|
* |
|
10
|
|
|
* As with any software that integrates with the Facebook platform, your use |
|
11
|
|
|
* of this software is subject to the Facebook Developer Principles and |
|
12
|
|
|
* Policies [http://developers.facebook.com/policy/]. This copyright notice |
|
13
|
|
|
* shall be included in all copies or substantial portions of the software. |
|
14
|
|
|
* |
|
15
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
16
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
17
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
|
18
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
19
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
|
20
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
|
21
|
|
|
* DEALINGS IN THE SOFTWARE. |
|
22
|
|
|
* |
|
23
|
|
|
*/ |
|
24
|
|
|
namespace Facebook\HttpClients; |
|
25
|
|
|
|
|
26
|
|
|
use GuzzleHttp\Client; |
|
27
|
|
|
use InvalidArgumentException; |
|
28
|
|
|
use Exception; |
|
29
|
|
|
|
|
30
|
|
|
class HttpClientsFactory |
|
31
|
|
|
{ |
|
32
|
|
|
private function __construct() |
|
33
|
|
|
{ |
|
34
|
|
|
// a factory constructor should never be invoked |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* HTTP client generation. |
|
39
|
|
|
* |
|
40
|
|
|
* @param FacebookHttpClientInterface|Client|string|null $handler |
|
41
|
|
|
* |
|
42
|
|
|
* @throws Exception If the cURL extension or the Guzzle client aren't available (if required). |
|
43
|
|
|
* @throws InvalidArgumentException If the http client handler isn't "curl", "stream", "guzzle", or an instance of Facebook\HttpClients\FacebookHttpClientInterface. |
|
44
|
|
|
* |
|
45
|
|
|
* @return FacebookHttpClientInterface |
|
46
|
|
|
*/ |
|
47
|
|
|
public static function createHttpClient($handler) |
|
48
|
|
|
{ |
|
49
|
|
|
if (!$handler) { |
|
50
|
|
|
return self::detectDefaultClient(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
if ($handler instanceof FacebookHttpClientInterface) { |
|
54
|
|
|
return $handler; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
if ('stream' === $handler) { |
|
58
|
|
|
return new FacebookStreamHttpClient(); |
|
59
|
|
|
} |
|
60
|
|
|
if ('curl' === $handler) { |
|
61
|
|
|
if (!extension_loaded('curl')) { |
|
62
|
|
|
throw new Exception('The cURL extension must be loaded in order to use the "curl" handler.'); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return new FacebookCurlHttpClient(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
if ('guzzle' === $handler && !class_exists('GuzzleHttp\Client')) { |
|
69
|
|
|
throw new Exception('The Guzzle HTTP client must be included in order to use the "guzzle" handler.'); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
if ($handler instanceof Client) { |
|
73
|
|
|
return new FacebookGuzzleHttpClient($handler); |
|
74
|
|
|
} |
|
75
|
|
|
if ('guzzle' === $handler) { |
|
76
|
|
|
return new FacebookGuzzleHttpClient(); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
throw new InvalidArgumentException('The http client handler must be set to "curl", "stream", "guzzle", be an instance of GuzzleHttp\Client or an instance of Facebook\HttpClients\FacebookHttpClientInterface'); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Detect default HTTP client. |
|
84
|
|
|
* |
|
85
|
|
|
* @return FacebookHttpClientInterface |
|
86
|
|
|
*/ |
|
87
|
|
|
private static function detectDefaultClient() |
|
88
|
|
|
{ |
|
89
|
|
|
if (extension_loaded('curl')) { |
|
90
|
|
|
return new FacebookCurlHttpClient(); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
if (class_exists('GuzzleHttp\Client')) { |
|
94
|
|
|
return new FacebookGuzzleHttpClient(); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return new FacebookStreamHttpClient(); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|