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.
- 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.