Skip to content

Commit

Permalink
Abstract the retrieval of a resource, make transformers supply their …
Browse files Browse the repository at this point in the history
…acceptable content-type
  • Loading branch information
brendo committed Dec 1, 2014
1 parent b341061 commit fa4b43a
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 43 deletions.
99 changes: 56 additions & 43 deletions data-sources/datasource.remote.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
class RemoteDatasource extends DataSource implements iDatasource
{

private static $transformer = null;
private static $url_result = null;

private static $cacheable = null;

public static function getName()
Expand Down Expand Up @@ -132,23 +132,7 @@ public static function isValidURL($url, $timeout = 6, $format = 'xml', $fetch_UR
if (trim($url) == '') {
return __('This is a required field');
} elseif ($fetch_URL === true) {
$gateway = new Gateway;
$gateway->init($url);
$gateway->setopt('TIMEOUT', $timeout);

// Set the approtiate Accept: headers depending on the format of the URL.
if ($format == 'xml') {
$gateway->setopt('HTTPHEADER', array('Accept: text/xml, */*'));
} elseif ($format == 'json') {
$gateway->setopt('HTTPHEADER', array('Accept: application/json, */*'));
} elseif ($format == 'csv') {
$gateway->setopt('HTTPHEADER', array('Accept: text/csv, */*'));
}

self::prepareGateway($gateway);

$data = $gateway->exec();
$info = $gateway->getInfoLast();
list($data, $info) = self::fetch($url, $format, $timeout);

// 28 is CURLE_OPERATION_TIMEOUTED
if (isset($info['curl_error']) && $info['curl_error'] == 28) {
Expand Down Expand Up @@ -664,27 +648,8 @@ public function execute(array &$param_pool = null)
|| (time() - $cachedData['creation']) > ($this->dsParamCACHE * 60) // The cache is old.
) {
if (Mutex::acquire($cache_id, $this->dsParamTIMEOUT, TMP)) {
$ch = new Gateway;
$ch->init($this->dsParamURL);
$ch->setopt('TIMEOUT', $this->dsParamTIMEOUT);

// Set the approtiate Accept: headers depending on the format of the URL.
if ($this->dsParamFORMAT == 'xml') {
$ch->setopt('HTTPHEADER', array('Accept: text/xml, */*'));
} elseif ($this->dsParamFORMAT == 'json') {
$ch->setopt('HTTPHEADER', array('Accept: application/json, */*'));
} elseif ($this->dsParamFORMAT == 'csv') {
$ch->setopt('HTTPHEADER', array('Accept: text/csv, */*'));
}

self::prepareGateway($ch);

$data = $ch->exec();
$info = $ch->getInfoLast();

list($data, $info) = self::fetch($this->dsParamURL, $this->dsParamFORMAT, $this->dsParamTIMEOUT);
Mutex::release($cache_id, TMP);

$data = trim($data);
$writeToCache = true;

// Handle any response that is not a 200, or the content type does not include XML, JSON, plain or text
Expand Down Expand Up @@ -819,6 +784,35 @@ public function execute(array &$param_pool = null)
return $result;
}

/**
* Given a URL, Format and Timeout, this function will initalise
* Symphony's Gateway class to retrieve the contents of the URL.
*
* @param string $url
* @param string $format
* @param integer $timeout
* @return array
*/
public static function fetch($url, $format, $timeout)
{
$ch = new Gateway;
$ch->init($url);
$ch->setopt('TIMEOUT', $timeout);

// Set the approtiate Accept: headers depending on the format of the URL.
if ($transformer = self::getTransformer($format)) {
$accepts = $transformer->accepts();
$ch->setopt('HTTPHEADER', array('Accept: ' . $accepts));
}

self::prepareGateway($ch);

return array(
trim($ch->exec()),
$ch->getInfoLast()
);
}

/**
* Given the result (a string), and a desired format, this
* function will transform it to the desired format and return
Expand All @@ -828,16 +822,35 @@ public function execute(array &$param_pool = null)
* @return string
*/
public static function transformResult($data, $format)
{
if ($transformer = self::getTransformer($format)) {
$data = $transformer->transform($data);
} else {
$data = '';
}

return $data;
}

/**
* Given the format, this function will look for the file
* and create a new transformer.
*
* @param string $format
* @return Transformer
*/
public static function getTransformer($format)
{
$transformer = EXTENSIONS . '/remote_datasource/lib/class.' . strtolower($format) . '.php';

if (file_exists($transformer)) {
$classname = require_once $transformer;
$classname = new $classname;
$data = $classname->transform($data);
if (!isset(self::$transformer)) {
if (file_exists($transformer)) {
$classname = require_once $transformer;
self::$transformer = new $classname;
}
}

return $data;
return self::$transformer;
}
}

Expand Down
5 changes: 5 additions & 0 deletions lib/class.csv.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@

class CSVFormatter implements Transformer
{
public function accepts()
{
return 'text/csv, */*';
}

public function transform($data)
{
try {
Expand Down
5 changes: 5 additions & 0 deletions lib/class.json.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

Class JSONFormatter implements Transformer
{
public function accepts()
{
return 'application/json, */*';
}

public function transform($data)
{
try {
Expand Down
5 changes: 5 additions & 0 deletions lib/class.txt.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@

Class TXTFormatter implements Transformer
{
public function accepts()
{
return 'text/plain, */*';
}

public function transform($data)
{
$txtElement = new XMLElement('entry');
Expand Down
5 changes: 5 additions & 0 deletions lib/class.xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@

Class XMLFormatter implements Transformer
{
public function accepts()
{
return 'text/xml, */*';
}

public function transform($data)
{
if (!General::validateXML($data, $errors, false, new XsltProcess)) {
Expand Down
6 changes: 6 additions & 0 deletions lib/interface.transformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

Interface Transformer
{
/**
* The content type of this transformer's format
* @return string
*/
public function accepts();

/**
* Accepts a single string parameter and returns
* back the data in the format specified by this
Expand Down

0 comments on commit fa4b43a

Please sign in to comment.