Skip to content
This repository has been archived by the owner on Apr 1, 2023. It is now read-only.

Commit

Permalink
feature #218 Integrate Guzzle 6.0 (csarrazi)
Browse files Browse the repository at this point in the history
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
fabpot committed Jun 21, 2015
2 parents 3578769 + d12732a commit 8b647d9
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 143 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Expand Up @@ -4,7 +4,6 @@ php:
- 7.0
- 5.6
- 5.5
- 5.4
- hhvm

install:
Expand Down
78 changes: 45 additions & 33 deletions Goutte/Client.php
Expand Up @@ -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;

Expand All @@ -25,6 +24,7 @@
*
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @author Michael Dowling <michael@guzzlephp.org>
* @author Charles Sarrazin <charles@sarraz.in>
*/
class Client extends BaseClient
{
Expand Down Expand Up @@ -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) {
Expand All @@ -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());
}
}

0 comments on commit 8b647d9

Please sign in to comment.