From f4fdd83746e9c8fe066c4e543858978f40076cce Mon Sep 17 00:00:00 2001 From: Bartosz Hernas Date: Thu, 7 Nov 2013 16:14:35 +0100 Subject: [PATCH 1/4] Added ability to specify headers Added ability to do single rest request without mapping --- lib/Doctrine/REST/Client/Client.php | 2 +- .../REST/Client/EntityConfiguration.php | 13 ++++++++++- lib/Doctrine/REST/Client/Manager.php | 1 + lib/Doctrine/REST/Client/Request.php | 11 ++++++++++ .../AbstractResponseTransformer.php | 22 ++++++++++++++++++- .../StandardResponseTransformer.php | 2 +- 6 files changed, 47 insertions(+), 4 deletions(-) diff --git a/lib/Doctrine/REST/Client/Client.php b/lib/Doctrine/REST/Client/Client.php index 87d2026..80d7fb0 100644 --- a/lib/Doctrine/REST/Client/Client.php +++ b/lib/Doctrine/REST/Client/Client.php @@ -67,7 +67,7 @@ public function execute(Request $request) curl_setopt($ch, CURLOPT_URL, $request->getUrl()); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); - curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:')); + curl_setopt($ch, CURLOPT_HTTPHEADER, $request->getHeaders()); $username = $request->getUsername(); $password = $request->getPassword(); diff --git a/lib/Doctrine/REST/Client/EntityConfiguration.php b/lib/Doctrine/REST/Client/EntityConfiguration.php index ac489a3..6b35f5b 100644 --- a/lib/Doctrine/REST/Client/EntityConfiguration.php +++ b/lib/Doctrine/REST/Client/EntityConfiguration.php @@ -56,7 +56,8 @@ public function __construct($class) { $this->_attributes['class'] = $class; $this->_attributes['urlGeneratorImpl'] = new StandardURLGenerator($this); - $this->_attributes['responseTransformerImpl'] = new StandardResponseTransformer($this); + $this->_attributes['responseTransformerImpl'] = new StandardResponseTransformer(); + $this->_attributes['responseTransformerImpl']->setEntityConfiguration($this); $this->_reflection = new \ReflectionClass($class); foreach ($this->_reflection->getProperties() as $property) { @@ -192,6 +193,16 @@ public function getResponseTransformerImpl() return $this->_attributes['responseTransformerImpl']; } + public function setHeaders($headers) + { + $this->_attributes['headers'] = $headers; + } + + public function getHeaders() + { + return $this->_attributes['headers']; + } + public function newInstance() { if ($this->_prototype === null) { diff --git a/lib/Doctrine/REST/Client/Manager.php b/lib/Doctrine/REST/Client/Manager.php index 6450c17..6660740 100644 --- a/lib/Doctrine/REST/Client/Manager.php +++ b/lib/Doctrine/REST/Client/Manager.php @@ -91,6 +91,7 @@ public function execute($entity, $url = null, $method = Client::GET, $parameters $request->setParameters($parameters); $request->setUsername($configuration->getUsername()); $request->setPassword($configuration->getPassword()); + $request->setHeaders($configuration->getHeaders()); $request->setResponseType($configuration->getResponseType()); $request->setResponseTransformerImpl($configuration->getResponseTransformerImpl()); diff --git a/lib/Doctrine/REST/Client/Request.php b/lib/Doctrine/REST/Client/Request.php index 93d0380..757835a 100644 --- a/lib/Doctrine/REST/Client/Request.php +++ b/lib/Doctrine/REST/Client/Request.php @@ -35,6 +35,7 @@ class Request private $_url; private $_method = Client::GET; private $_parameters = array(); + private $_headers = array(); private $_username; private $_password; private $_responseType = 'xml'; @@ -100,6 +101,16 @@ public function getPassword() return $this->_password; } + public function setHeaders($headers) + { + $this->_headers = $headers; + } + + public function getHeaders() + { + return $this->_headers; + } + public function setResponseTransformerImpl($responseTransformerImpl) { $this->_responseTransformerImpl = $responseTransformerImpl; diff --git a/lib/Doctrine/REST/Client/ResponseTransformer/AbstractResponseTransformer.php b/lib/Doctrine/REST/Client/ResponseTransformer/AbstractResponseTransformer.php index 3c32c9b..67ce562 100644 --- a/lib/Doctrine/REST/Client/ResponseTransformer/AbstractResponseTransformer.php +++ b/lib/Doctrine/REST/Client/ResponseTransformer/AbstractResponseTransformer.php @@ -35,11 +35,31 @@ abstract class AbstractResponseTransformer { protected $_entityConfiguration; + protected $_responseType; - public function __construct(EntityConfiguration $entityConfiguration) + public function __construct($responseType=null) + { + $this->_responseType = $responseType; + } + public function setEntityConfiguration(EntityConfiguration $entityConfiguration) { $this->_entityConfiguration = $entityConfiguration; } + public function getResponseType() { + if($this->_responseType!=null) { + return $this->_responseType; + } + if($this->_entityConfiguration) { + return $this->_entityConfiguration->getResponseType(); + } + return null; + } + + public function setResponseType($responseType) { + $this->_responseType = $responseType; + } + + abstract public function transform($data); } \ No newline at end of file diff --git a/lib/Doctrine/REST/Client/ResponseTransformer/StandardResponseTransformer.php b/lib/Doctrine/REST/Client/ResponseTransformer/StandardResponseTransformer.php index 9f4a714..88fe282 100644 --- a/lib/Doctrine/REST/Client/ResponseTransformer/StandardResponseTransformer.php +++ b/lib/Doctrine/REST/Client/ResponseTransformer/StandardResponseTransformer.php @@ -35,7 +35,7 @@ class StandardResponseTransformer extends AbstractResponseTransformer { public function transform($data) { - switch ($this->_entityConfiguration->getResponseType()) { + switch ($this->getResponseType()) { case 'xml': return $this->xmlToArray($data); case 'json': From 7b5f29caaab38e4a59749b4b1b5542c0bb3ab353 Mon Sep 17 00:00:00 2001 From: Bartosz Hernas Date: Thu, 7 Nov 2013 16:31:30 +0100 Subject: [PATCH 2/4] Added method to get entityConfiguration in ResponseTransformers and fixed issue with json_decode --- .../ResponseTransformer/AbstractResponseTransformer.php | 6 ++++++ .../ResponseTransformer/StandardResponseTransformer.php | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/Doctrine/REST/Client/ResponseTransformer/AbstractResponseTransformer.php b/lib/Doctrine/REST/Client/ResponseTransformer/AbstractResponseTransformer.php index 67ce562..abc7391 100644 --- a/lib/Doctrine/REST/Client/ResponseTransformer/AbstractResponseTransformer.php +++ b/lib/Doctrine/REST/Client/ResponseTransformer/AbstractResponseTransformer.php @@ -41,11 +41,17 @@ public function __construct($responseType=null) { $this->_responseType = $responseType; } + public function setEntityConfiguration(EntityConfiguration $entityConfiguration) { $this->_entityConfiguration = $entityConfiguration; } + public function getEntityConfiguration() + { + return $this->_entityConfiguration; + } + public function getResponseType() { if($this->_responseType!=null) { return $this->_responseType; diff --git a/lib/Doctrine/REST/Client/ResponseTransformer/StandardResponseTransformer.php b/lib/Doctrine/REST/Client/ResponseTransformer/StandardResponseTransformer.php index 88fe282..443af8a 100644 --- a/lib/Doctrine/REST/Client/ResponseTransformer/StandardResponseTransformer.php +++ b/lib/Doctrine/REST/Client/ResponseTransformer/StandardResponseTransformer.php @@ -77,6 +77,6 @@ public function xmlToArray($object, &$array = array()) public function jsonToArray($json) { - return (array) json_decode($json); + return (array) json_decode($json, true); } } From 9b83039bb9c1495c1b44aeffc2af24faa218b91f Mon Sep 17 00:00:00 2001 From: Bartosz Hernas Date: Thu, 7 Nov 2013 17:55:14 +0100 Subject: [PATCH 3/4] Added requestType to support requests where data is sent via JSON --- lib/Doctrine/REST/Client/Client.php | 30 +++++++++++++++++++++++++--- lib/Doctrine/REST/Client/Entity.php | 2 +- lib/Doctrine/REST/Client/Manager.php | 7 +------ lib/Doctrine/REST/Client/Request.php | 11 ++++++++++ 4 files changed, 40 insertions(+), 10 deletions(-) diff --git a/lib/Doctrine/REST/Client/Client.php b/lib/Doctrine/REST/Client/Client.php index 80d7fb0..d5546b3 100644 --- a/lib/Doctrine/REST/Client/Client.php +++ b/lib/Doctrine/REST/Client/Client.php @@ -76,11 +76,35 @@ public function execute(Request $request) curl_setopt ($ch, CURLOPT_USERPWD, $username . ':' . $password); } + if($request->getMethod()==self::POST || $request->getMethod()==self::PUT) { + + if($request->getRequestType()=='json') { + $requestBody = json_encode($request->getParameters()); + $requestLength = strlen($requestBody); + + $fh = fopen('php://memory', 'rw'); + fwrite($fh, $requestBody); + rewind($fh); + + $headers = array_merge($request->getHeaders(), array( + 'Expect:', + 'Content-length: '.$requestLength, + 'Content-type: application/json;charset="utf-8"' + )); + + curl_setopt($ch, CURLOPT_INFILE, $fh); + curl_setopt($ch, CURLOPT_INFILESIZE, $requestLength); + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); + } else { + curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($request->getParameters())); + } + } switch ($request->getMethod()) { case self::POST: - case self::PUT: curl_setopt($ch, CURLOPT_POST, 1); - curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($request->getParameters())); + break; + case self::PUT: + curl_setopt($ch, CURLOPT_PUT, 1); break; case self::DELETE: curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); @@ -91,7 +115,7 @@ public function execute(Request $request) } $result = curl_exec($ch); - + xdebug_var_dump($result); if ( ! $result) { $errorNumber = curl_errno($ch); $error = curl_error($ch); diff --git a/lib/Doctrine/REST/Client/Entity.php b/lib/Doctrine/REST/Client/Entity.php index 3028b20..0a0a983 100644 --- a/lib/Doctrine/REST/Client/Entity.php +++ b/lib/Doctrine/REST/Client/Entity.php @@ -83,7 +83,7 @@ public function save($action = null) { $parameters = $this->toArray(); $exists = $this->exists(); - $method = $exists ? Client::POST : Client::PUT; + $method = $exists ? Client::PUT : Client::POST; $id = $exists ? $this->getIdentifier() : null; $path = $this->generateUrl(get_defined_vars()); return self::$_manager->execute($this, $path, $method, $parameters, $action); diff --git a/lib/Doctrine/REST/Client/Manager.php b/lib/Doctrine/REST/Client/Manager.php index 6660740..261a199 100644 --- a/lib/Doctrine/REST/Client/Manager.php +++ b/lib/Doctrine/REST/Client/Manager.php @@ -149,12 +149,7 @@ public function execute($entity, $url = null, $method = Client::GET, $parameters private function _hydrate($configuration, $instance, $data) { foreach ($data as $key => $value) { - if (is_array($value)) - { - $configuration->setValue($instance, $key, (string) $value); - } else { - $configuration->setValue($instance, $key, $value); - } + $configuration->setValue($instance, $key, $value); } return $instance; diff --git a/lib/Doctrine/REST/Client/Request.php b/lib/Doctrine/REST/Client/Request.php index 757835a..0128466 100644 --- a/lib/Doctrine/REST/Client/Request.php +++ b/lib/Doctrine/REST/Client/Request.php @@ -38,6 +38,7 @@ class Request private $_headers = array(); private $_username; private $_password; + private $_requestType; private $_responseType = 'xml'; private $_responseTransformerImpl; @@ -81,6 +82,16 @@ public function getResponseType() return $this->_responseType; } + public function setRequestType($requestType) + { + $this->_requestType = $requestType; + } + + public function getRequestType() + { + return ($this->_requestType?$this->_requestType:$this->getResponseType()); + } + public function setUsername($username) { $this->_username = $username; From 07b34c1f68b0bef151c45a7e3c86335ca4139d49 Mon Sep 17 00:00:00 2001 From: Bartosz Hernas Date: Thu, 7 Nov 2013 18:10:29 +0100 Subject: [PATCH 4/4] Added legacy support --- lib/Doctrine/REST/Client/EntityConfiguration.php | 3 +-- .../ResponseTransformer/AbstractResponseTransformer.php | 6 +++++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/Doctrine/REST/Client/EntityConfiguration.php b/lib/Doctrine/REST/Client/EntityConfiguration.php index 6b35f5b..201314b 100644 --- a/lib/Doctrine/REST/Client/EntityConfiguration.php +++ b/lib/Doctrine/REST/Client/EntityConfiguration.php @@ -56,8 +56,7 @@ public function __construct($class) { $this->_attributes['class'] = $class; $this->_attributes['urlGeneratorImpl'] = new StandardURLGenerator($this); - $this->_attributes['responseTransformerImpl'] = new StandardResponseTransformer(); - $this->_attributes['responseTransformerImpl']->setEntityConfiguration($this); + $this->_attributes['responseTransformerImpl'] = new StandardResponseTransformer($this); $this->_reflection = new \ReflectionClass($class); foreach ($this->_reflection->getProperties() as $property) { diff --git a/lib/Doctrine/REST/Client/ResponseTransformer/AbstractResponseTransformer.php b/lib/Doctrine/REST/Client/ResponseTransformer/AbstractResponseTransformer.php index abc7391..b45271f 100644 --- a/lib/Doctrine/REST/Client/ResponseTransformer/AbstractResponseTransformer.php +++ b/lib/Doctrine/REST/Client/ResponseTransformer/AbstractResponseTransformer.php @@ -39,7 +39,11 @@ abstract class AbstractResponseTransformer public function __construct($responseType=null) { - $this->_responseType = $responseType; + if(is_object($responseType) && get_class($responseType) == 'EntityConfiguration') { //legacy + $this->setEntityConfiguration($responseType); + } else { + $this->_responseType = $responseType; + } } public function setEntityConfiguration(EntityConfiguration $entityConfiguration)