Skip to content
This repository was archived by the owner on Jan 13, 2022. It is now read-only.

Commit 829e487

Browse files
author
Fosco Marotto
committed
Merge pull request #90 from SammyK/http-client-implementation-guzzle-and-stream
Http client implementations for stream wrapper & Guzzle with stream fallback
2 parents 583016d + 1b77bfc commit 829e487

14 files changed

+796
-123
lines changed

composer.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@
1616
},
1717
"require-dev": {
1818
"phpunit/phpunit": "3.7.*",
19-
"mockery/mockery": "dev-master"
19+
"mockery/mockery": "dev-master",
20+
"guzzlehttp/guzzle": "~4.0"
21+
},
22+
"suggest": {
23+
"guzzlehttp/guzzle": "Allows for implementation of the Guzzle HTTP client"
2024
},
2125
"autoload": {
2226
"psr-4": {

src/Facebook/FacebookRequest.php

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
*/
2424
namespace Facebook;
2525

26+
use Facebook\HttpClients\FacebookHttpable;
27+
use Facebook\HttpClients\FacebookCurlHttpClient;
28+
use Facebook\HttpClients\FacebookStreamHttpClient;
29+
2630
/**
2731
* Class FacebookRequest
2832
* @package Facebook
@@ -136,7 +140,7 @@ public function getETag()
136140
* setHttpClientHandler - Returns an instance of the HTTP client
137141
* handler
138142
*
139-
* @param FacebookHttpable
143+
* @param \Facebook\HttpClients\FacebookHttpable
140144
*/
141145
public static function setHttpClientHandler(FacebookHttpable $handler)
142146
{
@@ -151,7 +155,10 @@ public static function setHttpClientHandler(FacebookHttpable $handler)
151155
*/
152156
public static function getHttpClientHandler()
153157
{
154-
return static::$httpClientHandler ?: static::$httpClientHandler = new FacebookCurlHttpClient();
158+
if (static::$httpClientHandler) {
159+
return static::$httpClientHandler;
160+
}
161+
return function_exists('curl_init') ? new FacebookCurlHttpClient() : new FacebookStreamHttpClient();
155162
}
156163

157164
/**
@@ -168,7 +175,8 @@ public static function getHttpClientHandler()
168175
*/
169176
public function __construct(
170177
$session, $method, $path, $parameters = null, $version = null, $etag = null
171-
) {
178+
)
179+
{
172180
$this->session = $session;
173181
$this->method = $method;
174182
$this->path = $path;
@@ -198,7 +206,8 @@ public function __construct(
198206
*
199207
* @return string
200208
*/
201-
protected function getRequestURL() {
209+
protected function getRequestURL()
210+
{
202211
return static::BASE_GRAPH_URL . '/' . $this->version . $this->path;
203212
}
204213

@@ -210,7 +219,8 @@ protected function getRequestURL() {
210219
* @throws FacebookSDKException
211220
* @throws FacebookRequestException
212221
*/
213-
public function execute() {
222+
public function execute()
223+
{
214224
$url = $this->getRequestURL();
215225
$params = $this->getParameters();
216226

@@ -228,13 +238,10 @@ public function execute() {
228238
$connection->addRequestHeader('If-None-Match', $this->etag);
229239
}
230240

241+
// Should throw `FacebookSDKException` exception on HTTP client error.
242+
// Don't catch to allow it to bubble up.
231243
$result = $connection->send($url, $this->method, $params);
232244

233-
// Client error
234-
if ($result === false) {
235-
throw new FacebookSDKException($connection->getErrorMessage(), $connection->getErrorCode());
236-
}
237-
238245
$etagHit = 304 == $connection->getResponseHttpStatusCode();
239246

240247
$headers = $connection->getResponseHeaders();
@@ -257,7 +264,6 @@ public function execute() {
257264
return new FacebookResponse($this, $decodedResult, $result, $etagHit, $etagReceived);
258265
}
259266

260-
261267
/**
262268
* Generate and return the appsecret_proof value for an access_token
263269
*

src/Facebook/FacebookCurl.php renamed to src/Facebook/HttpClients/FacebookCurl.php

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
* DEALINGS IN THE SOFTWARE.
2222
*
2323
*/
24-
namespace Facebook;
24+
namespace Facebook\HttpClients;
2525

2626
/**
2727
* Class FacebookCurl
28-
* Abstraction for the procedural curl elements so that curl can me mocked
28+
* Abstraction for the procedural curl elements so that curl can be mocked
2929
* and the implementation can be tested.
3030
* @package Facebook
3131
*/
@@ -40,7 +40,8 @@ class FacebookCurl
4040
/**
4141
* Make a new curl reference instance
4242
*/
43-
public function init() {
43+
public function init()
44+
{
4445
$this->curl = curl_init();
4546
}
4647

@@ -50,7 +51,8 @@ public function init() {
5051
* @param $key
5152
* @param $value
5253
*/
53-
public function setopt($key, $value) {
54+
public function setopt($key, $value)
55+
{
5456
curl_setopt($this->curl, $key, $value);
5557
}
5658

@@ -59,7 +61,8 @@ public function setopt($key, $value) {
5961
*
6062
* @param array $options
6163
*/
62-
public function setopt_array(array $options) {
64+
public function setopt_array(array $options)
65+
{
6366
curl_setopt_array($this->curl, $options);
6467
}
6568

@@ -68,7 +71,8 @@ public function setopt_array(array $options) {
6871
*
6972
* @return mixed
7073
*/
71-
public function exec() {
74+
public function exec()
75+
{
7276
return curl_exec($this->curl);
7377
}
7478

@@ -77,7 +81,8 @@ public function exec() {
7781
*
7882
* @return int
7983
*/
80-
public function errno() {
84+
public function errno()
85+
{
8186
return curl_errno($this->curl);
8287
}
8388

@@ -86,7 +91,8 @@ public function errno() {
8691
*
8792
* @return string
8893
*/
89-
public function error() {
94+
public function error()
95+
{
9096
return curl_error($this->curl);
9197
}
9298

@@ -97,7 +103,8 @@ public function error() {
97103
*
98104
* @return mixed
99105
*/
100-
public function getinfo($type) {
106+
public function getinfo($type)
107+
{
101108
return curl_getinfo($this->curl, $type);
102109
}
103110

@@ -106,14 +113,16 @@ public function getinfo($type) {
106113
*
107114
* @return array
108115
*/
109-
public function version() {
116+
public function version()
117+
{
110118
return curl_version();
111119
}
112120

113121
/**
114122
* Close the resource connection to curl
115123
*/
116-
public function close() {
124+
public function close()
125+
{
117126
curl_close($this->curl);
118127
}
119128

0 commit comments

Comments
 (0)