From db1d7ae3aaaa25211ffdb92856379a5bf29e2727 Mon Sep 17 00:00:00 2001 From: Michael Dowling Date: Fri, 10 May 2013 13:03:51 -0700 Subject: [PATCH] Using a small clone of a request when setting on a response to avoid a circular reference --- src/Guzzle/Http/Message/Request.php | 19 +++++++++++++++-- src/Guzzle/Http/Message/Response.php | 32 +++++++++++++++++----------- 2 files changed, 36 insertions(+), 15 deletions(-) diff --git a/src/Guzzle/Http/Message/Request.php b/src/Guzzle/Http/Message/Request.php index 6d34d06b..beb1faff 100644 --- a/src/Guzzle/Http/Message/Request.php +++ b/src/Guzzle/Http/Message/Request.php @@ -500,7 +500,8 @@ class Request extends AbstractMessage implements RequestInterface $body = $code >= 200 && $code < 300 ? $this->getResponseBody() : EntityBody::factory(); $this->response = new Response($code, null, $body); - $this->response->setStatus($code, $status)->setRequest($this); + $this->response->setStatus($code, $status); + $this->setRequestOnResponse($this->response); $this->dispatch('request.receive.status_line', array( 'request' => $this, 'line' => $data, @@ -524,7 +525,7 @@ class Request extends AbstractMessage implements RequestInterface { // Never overwrite the request associated with the response (useful for redirect history) if (!$response->getRequest()) { - $response->setRequest($this); + $this->setRequestOnResponse($response); } if ($queued) { @@ -789,4 +790,18 @@ class Request extends AbstractMessage implements RequestInterface } } } + + /** + * Set a request closure on a response + * + * @param Response $response + * @deprecated + */ + protected function setRequestOnResponse(Response $response) + { + $headers = $this->getRawHeaders(); + $response->setRequest(function () use ($headers) { + return RequestFactory::getInstance()->fromMessage($headers); + }); + } } diff --git a/src/Guzzle/Http/Message/Response.php b/src/Guzzle/Http/Message/Response.php index 7d5fc087..6d674f13 100644 --- a/src/Guzzle/Http/Message/Response.php +++ b/src/Guzzle/Http/Message/Response.php @@ -104,7 +104,7 @@ class Response extends AbstractMessage protected $info = array(); /** - * @var RequestInterface Request object that may or may not be set + * @var RequestInterface|callable Request object that may or may not be set */ protected $request = null; @@ -343,16 +343,6 @@ class Response extends AbstractMessage return $headers . "\r\n"; } - /** - * Get the request object (or null) that is associated with this response - * - * @return RequestInterface - */ - public function getRequest() - { - return $this->request; - } - /** * Get the response reason phrase- a human readable version of the numeric * status code @@ -779,17 +769,33 @@ class Response extends AbstractMessage /** * Set the request object associated with the response * - * @param RequestInterface $request The request object used to generate the response + * @param mixed $request The request object used to generate the response or a closure to return a request * * @return Response + * @deprecated */ - public function setRequest(RequestInterface $request) + public function setRequest($request) { $this->request = $request; return $this; } + /** + * Get the request object (or null) that is associated with this response + * + * @return RequestInterface + * @deprecated + */ + public function getRequest() + { + if (is_callable($this->request)) { + $this->request = call_user_func($this->request); + } + + return $this->request; + } + /** * Check if the response can be cached *