This repository has been archived by the owner on Apr 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #218 Integrate Guzzle 6.0 (csarrazi)
This PR was squashed before being merged into the 2.0-dev branch (closes #218). Discussion ---------- Integrate Guzzle 6.0 | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | yes | Deprecations? | no | Tests pass? | yes | Fixed tickets | #215 | License | MIT It is important to note that this PR breaks compatibility due to Guzzle 6.0 requiring PHP 5.5+, and due to Guzzle 6.0's API being different from Guzzle 4.x or 5.x. This last issue should not have any incidence, provided the user does not directly access the Guzzle client from Goutte. Of course, these BC changes mean that the major version number should be updated, to respect semver. Tests have been updated, and pass. Commits ------- d12732a Integrate Guzzle 6.0
- Loading branch information
Showing
6 changed files
with
149 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,6 @@ php: | |
- 7.0 | ||
- 5.6 | ||
- 5.5 | ||
- 5.4 | ||
- hhvm | ||
|
||
install: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,10 +13,9 @@ | |
|
||
use GuzzleHttp\Client as GuzzleClient; | ||
use GuzzleHttp\ClientInterface as GuzzleClientInterface; | ||
use GuzzleHttp\Cookie\CookieJar; | ||
use GuzzleHttp\Exception\RequestException; | ||
use GuzzleHttp\Message\RequestInterface; | ||
use GuzzleHttp\Message\Response as GuzzleResponse; | ||
use GuzzleHttp\Post\PostFile; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Symfony\Component\BrowserKit\Client as BaseClient; | ||
use Symfony\Component\BrowserKit\Response; | ||
|
||
|
@@ -25,6 +24,7 @@ | |
* | ||
* @author Fabien Potencier <[email protected]> | ||
* @author Michael Dowling <[email protected]> | ||
* @author Charles Sarrazin <[email protected]> | ||
*/ | ||
class Client extends BaseClient | ||
{ | ||
|
@@ -90,44 +90,44 @@ protected function doRequest($request) | |
} | ||
} | ||
|
||
$body = null; | ||
$cookies = CookieJar::fromArray( | ||
$this->getCookieJar()->allRawValues($request->getUri()), | ||
$request->getServer()['HTTP_HOST'] | ||
); | ||
|
||
$requestOptions = array( | ||
'cookies' => $cookies, | ||
'allow_redirects' => false, | ||
'auth' => $this->auth, | ||
); | ||
|
||
if (!in_array($request->getMethod(), array('GET', 'HEAD'))) { | ||
if (null !== $request->getContent()) { | ||
$body = $request->getContent(); | ||
$requestOptions['body'] = $request->getContent(); | ||
} else { | ||
$body = $request->getParameters(); | ||
$requestOptions['form_params'] = $request->getParameters(); | ||
|
||
if ($files = $request->getFiles()) { | ||
$requestOptions['multipart'] = []; | ||
$this->addPostFiles($files, $requestOptions['multipart']); | ||
} | ||
} | ||
} | ||
|
||
$this->getClient()->setDefaultOption('auth', $this->auth); | ||
|
||
$requestOptions = array( | ||
'body' => $body, | ||
'cookies' => $this->getCookieJar()->allRawValues($request->getUri()), | ||
'allow_redirects' => false, | ||
); | ||
|
||
if (!empty($headers)) { | ||
$requestOptions['headers'] = $headers; | ||
} | ||
|
||
$guzzleRequest = $this->getClient()->createRequest( | ||
$request->getMethod(), | ||
$request->getUri(), | ||
$requestOptions | ||
); | ||
$method = $request->getMethod(); | ||
$uri = $request->getUri(); | ||
|
||
foreach ($this->headers as $name => $value) { | ||
$guzzleRequest->setHeader($name, $value); | ||
} | ||
|
||
if ('POST' == $request->getMethod() || 'PUT' == $request->getMethod() && $request->getFiles()) { | ||
$this->addPostFiles($guzzleRequest, $request->getFiles()); | ||
$requestOptions['headers'][$name] = $value; | ||
} | ||
|
||
// Let BrowserKit handle redirects | ||
try { | ||
$response = $this->getClient()->send($guzzleRequest); | ||
$response = $this->getClient()->request($method, $uri, $requestOptions); | ||
} catch (RequestException $e) { | ||
$response = $e->getResponse(); | ||
if (null === $response) { | ||
|
@@ -138,33 +138,45 @@ protected function doRequest($request) | |
return $this->createResponse($response); | ||
} | ||
|
||
protected function addPostFiles(RequestInterface $request, array $files, $arrayName = '') | ||
protected function addPostFiles(array $files, array &$multipart, $arrayName = '') | ||
{ | ||
if (empty($files)) { | ||
return; | ||
} | ||
|
||
foreach ($files as $name => $info) { | ||
if (!empty($arrayName)) { | ||
$name = $arrayName.'['.$name.']'; | ||
} | ||
|
||
$file = [ | ||
'name' => $name, | ||
]; | ||
|
||
if (is_array($info)) { | ||
if (isset($info['tmp_name'])) { | ||
if ('' !== $info['tmp_name']) { | ||
$request->getBody()->addFile(new PostFile($name, fopen($info['tmp_name'], 'r'), isset($info['name']) ? $info['name'] : null)); | ||
$file['contents'] = fopen($info['tmp_name'], 'r'); | ||
if (isset($info['name'])) { | ||
$file['filename'] = $info['name']; | ||
} | ||
} else { | ||
continue; | ||
} | ||
} else { | ||
$this->addPostFiles($request, $info, $name); | ||
$this->addPostFiles($info, $multipart, $name); | ||
continue; | ||
} | ||
} else { | ||
$request->getBody()->addFile(new PostFile($name, fopen($info, 'r'))); | ||
$file['contents'] = fopen($info, 'r'); | ||
} | ||
|
||
$multipart[] = $file; | ||
} | ||
} | ||
|
||
protected function createResponse(GuzzleResponse $response) | ||
protected function createResponse(ResponseInterface $response) | ||
{ | ||
$headers = $response->getHeaders(); | ||
|
||
return new Response($response->getBody(true), $response->getStatusCode(), $headers); | ||
return new Response((string) $response->getBody(), $response->getStatusCode(), $response->getHeaders()); | ||
} | ||
} |
Oops, something went wrong.