Skip to content
This repository has been archived by the owner on Aug 29, 2018. It is now read-only.

Lot of changes to make it more flexible. #4

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions lib/Doctrine/REST/Client/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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');
Expand All @@ -91,7 +115,7 @@ public function execute(Request $request)
}

$result = curl_exec($ch);

xdebug_var_dump($result);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should probably remove this

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do, sorry

Wysłane z iPhone'a / Sent from iPhone

Pozdrawiam / Best regards

Bartosz Hernas

http://hernas.pl

On 24 gru 2013, at 20:16, Colin Hutchinson [email protected] wrote:

In lib/Doctrine/REST/Client/Client.php:

@@ -91,7 +115,7 @@ public function execute(Request $request)
}

     $result = curl_exec($ch);

  •    xdebug_var_dump($result);
    
    should probably remove this


Reply to this email directly or view it on GitHub.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for removal

if ( ! $result) {
$errorNumber = curl_errno($ch);
$error = curl_error($ch);
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/REST/Client/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you sure about this change? I believe PUT is to create (specifies the resource id) or replace while POST is to create (lets the server set the resource id on return) or update

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

http://stackoverflow.com/questions/630453/put-vs-post-in-rest
http://restcookbook.com/HTTP%20Methods/put-vs-post/

POST is to create (no id specified) and PUT is to update (id specified) so I am sure. I tested it with our REST API and its correct behaviour.

$id = $exists ? $this->getIdentifier() : null;
$path = $this->generateUrl(get_defined_vars());
return self::$_manager->execute($this, $path, $method, $parameters, $action);
Expand Down
10 changes: 10 additions & 0 deletions lib/Doctrine/REST/Client/EntityConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,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) {
Expand Down
8 changes: 2 additions & 6 deletions lib/Doctrine/REST/Client/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down Expand Up @@ -148,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;
Expand Down
22 changes: 22 additions & 0 deletions lib/Doctrine/REST/Client/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ class Request
private $_url;
private $_method = Client::GET;
private $_parameters = array();
private $_headers = array();
private $_username;
private $_password;
private $_requestType;
private $_responseType = 'xml';
private $_responseTransformerImpl;

Expand Down Expand Up @@ -80,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;
Expand All @@ -100,6 +112,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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,41 @@
abstract class AbstractResponseTransformer
{
protected $_entityConfiguration;
protected $_responseType;

public function __construct(EntityConfiguration $entityConfiguration)
public function __construct($responseType=null)
{
if(is_object($responseType) && get_class($responseType) == 'EntityConfiguration') { //legacy
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (is_object($responseType) && get_class($responseType) === 'EntityConfiguration') { //legacy

$this->setEntityConfiguration($responseType);
} else {
$this->_responseType = $responseType;
}
}

public function setEntityConfiguration(EntityConfiguration $entityConfiguration)
{
$this->_entityConfiguration = $entityConfiguration;
}

public function getEntityConfiguration()
{
return $this->_entityConfiguration;
}

public function getResponseType() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{ belongs on the next line.

if($this->_responseType!=null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line should be:

if ($this->_responseType !== null) {

return $this->_responseType;
}
if($this->_entityConfiguration) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if ($this->_entityConfiguration) {

return $this->_entityConfiguration->getResponseType();
}
return null;
}

public function setResponseType($responseType) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{ belongs on the next line.

$this->_responseType = $responseType;
}


abstract public function transform($data);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down Expand Up @@ -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);
}
}