Skip to content

Instantly share code, notes, and snippets.

@e0ipso
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save e0ipso/cb0da3b3b9a102339caa to your computer and use it in GitHub Desktop.
Save e0ipso/cb0da3b3b9a102339caa to your computer and use it in GitHub Desktop.
RESTful 7.x-2.x - Mocks
// Declares the CRUD operations (including the request parsing for filters, fields, sorts, …)
// Composed by a swappable DataResource.
// Gets and sets data from / to the back-end. Note that a DataSource can be a ResourceHandler, for nested resources.
<?php
/**
* Contains \Drupal\restful\Manager
*/
namespace Drupal\restful;
/**
* Singleton that gets saved into a global variable.
*/
class Manager implements ManagerInterface {
/**
* Holds the class instance.
*
* Notice how this property is declared as static.
*
* @var Manager
*/
private static $instance;
protected $request;
protected $response;
protected $resource;
/**
* Private constructor.
*
* The singleton pattern declares the constructor private so you can make sure
* that only one object can be instantiated.
*/
private function __construct() {
$this->request = new Request();
$this->response = new Response();
}
/**
* Factory method for the singleton.
*/
public static function getInstance() {
if (static::$instance) {
return static::$instance;
}
$GLOBAL['restful'] = static::$instance = new static();
return static::$instance;
}
/**
* Lazy loads the resource handler.
*/
public function getResource() {
if ($this->resource) {
return $this->resource;
}
// Call the factory method on the ResourceInterface with the request information.
return Resource::create($this->request);
}
}
<?php
/**
* @file
* Contains \Drupal\restful\http\Request
*/
namespace Drupal\restful\http;
use Drupal\restful\http\RequestInterface;
/**
* Deals with everything coming from the comsumer.
*/
class Request implements RequestInterface {
/**
* HTTP Method.
*
* @var string
*/
protected $method;
/**
* URL
*
* @var string
*/
protected $url;
/**
* Query parameters.
*
* @var \ArrayObject
*/
protected $query;
/**
* The input HTTP headers.
*
* @var \ArrayObject
*/
protected $headers;
/**
* The unprocessed body of the request.
*
* This should be a PHP stream, but let's keep it simple.
*
* @var string
*/
protected $body;
/**
* Holds the parsed body.
*
* @internal
* @var \ArrayObject
*/
private $parsedBody;
/**
* Constructor.
*
* Parses the URL and the query params. It also uses input:// to get the body.
*/
public __construct() { … }
/**
* Parses the body string.
*
* @return \ArrayObject
*/
public getParsedBody() {
if ($this->parsedBody) {
return $this->parsedBody;
}
// Find out the body format and parse it into the \ArrayObject.
return $this->parsedBody;
}
/**
* Gets the fully qualified URL with the query params.
*
* @return string
* The URL.
*/
public function href() { … }
// This class also contains:
// - Mutators and accessors.
// - Validation methods for HTTP methods and helpers (isWriteMethod, …)
// - MOVE TO A NEW 'HttpHeader' CLASS: Helper methods to read and parse headers (think of multiple value headers and modifiers like '; charset utf-8').
// - Other helpers that act on the raw user input.
}
<?php
/**
* @file
* Contains \Drupal\restful\resource\entity\ResourceFields.
*/
namespace Drupal\restful\resource\entity;
/**
* This class needs to be extended and filled in by the implementor.
*/
abstract class ResourceFields implements ResourceFieldsInterface {
/**
* Default public fields.
*/
abstract public function getPublicFields();
/**
* Helper method that determines if a public property is Field API.
*
* @param string $public_name
*
* @return boolean
* TRUE if the property maps to a Field API field. FALSE otherwise.
*/
public function isFieldAPI($public_name) {
$public_fields = $this->getPublicFields();
if (empty($public_fields[$public_name]['property'])) {
return FALSE;
}
return !empty(field_info_field($field_name));
}
}
<?php
/**
* @file
* Contains \Drupal\restful_example\resource\entity\ResourceFieldsExample.
*/
namespace Drupal\restful_example\resource\entity;
use Drupal\restful\resource\entity\ResourceFields;
/**
* This class needs to be extended and filled in by the implementor.
*/
class ResourceFieldsExample extends ResourceFields {
/**
* Default public fields.
*/
public function getPublicFields() {
return array(
'title' => array(
'wrapper_method' => 'label',
'wrapper_on_entity' => TRUE,
),
'custom' => array(
'callback' => array($this, 'customMethod', array('arg1', 'arg2')),
),
'id' => array(
'property' => 'uuid',
),
);
}
}
// Composed by a ResourceFields, a DataProvider and a Formatter object. This is the resource plugin that the implementor declares.
// This is the natural substitute of RestfulBase.
<?php
// This deals with what is going to be sent back to the consumer.
/**
* @file
* Contains \Drupal\restful\http\Response
*/
namespace Drupal\restful\http;
use Drupal\restful\http\ResponseInterface;
use Drupal\restful\http\HttpHeader;
/**
* Deals with everything sent to the comsumer.
*/
class Response implements ResponseInterface {
/**
* The output HTTP headers.
*
* @var \ArrayObject
*/
protected $headers;
/**
* The body of the request ready to be sent.
*
* This should be a PHP stream, but let's keep it simple.
*
* @var string
*/
protected $body;
/**
* Constructor.
*
* Initializes the values.
*
* @param string $body
* The body of the response.
* @param array $headers.
* Headers to send.
*/
public __construct($body, array $headers) { … }
/**
* Parses the body string.
*
* @param HttpHeader $header
* The header object.
* @param bool $replace
* Set to TRUE to overwrite any value present in the header.
*/
public addHeader(HttpHeader $header, $replace = FALSE) {
if ($replace) {
$this->headers[$key] = new \ArrayObject(array($header->getName(), $header));
return;
}
$this->headers[$key] = HttpHeader::append($this->headers[$key], $header);
}
// This class also contains:
// - Mutators and accessors.
// - Other helpers that act on the raw user output.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment