Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
Refactor proxy logic out of Resource subclasses
Browse files Browse the repository at this point in the history
The whole proxy idea is still kind of convoluted. Maybe one or two more passes
at it might help. The alternative is to get rid of proxies altogether and pass
the client into each Resource instance, and have the instance handle its own
object cache... or is there something else that's better?
  • Loading branch information
luciferous committed Mar 12, 2011
1 parent a1a84a8 commit bb598bb
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 41 deletions.
2 changes: 2 additions & 0 deletions Services/Twilio.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require_once dirname(__FILE__) . '/' . 'Twilio/TinyHttp.php';
require_once dirname(__FILE__) . '/' . 'Twilio/Page.php';
require_once dirname(__FILE__) . '/' . 'Twilio/DataProxy.php';
require_once dirname(__FILE__) . '/' . 'Twilio/CachingDataProxy.php';
require_once dirname(__FILE__) . '/' . 'Twilio/ArrayDataProxy.php';
require_once dirname(__FILE__) . '/' . 'Twilio/Resource.php';
require_once dirname(__FILE__) . '/' . 'Twilio/ListResource.php';
require_once dirname(__FILE__) . '/' . 'Twilio/InstanceResource.php';
Expand Down
24 changes: 24 additions & 0 deletions Services/Twilio/ArrayDataProxy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

class Services_Twilio_ArrayDataProxy
implements Services_Twilio_DataProxy
{
protected $array;
public function __construct($array) {
$this->array = $array;
}
function retrieveData($key, array $params = array()) {
return (object) $this->array;
}
function createData($key, array $params = array()) {
return (object) $this->array;
}
function updateData(array $params) {
return $this->array;
}
function __get($prop) {
return is_array($this->array)
? $this->array['prop']
: $this->array->$prop;
}
}
56 changes: 56 additions & 0 deletions Services/Twilio/CachingDataProxy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

class Services_Twilio_CachingDataProxy
implements Services_Twilio_DataProxy
{
protected $proxy;
protected $principal;
protected $cache;
public function __construct($principal, Services_Twilio_DataProxy $proxy,
$cache = NULL
) {
if (is_scalar($principal)) {
$principal = array('sid' => $principal, 'params' => array());
}
$this->principal = $principal;
$this->proxy = $proxy;
$this->cache = $cache;
}
public function setCache($object) {
$this->cache = $object;
}
public function __get($prop) {
if ($prop == 'sid') {
return $this->principal['sid'];
}
if (empty($this->cache)) {
$this->_load();
}
return isset($this->cache->$prop)
? $this->cache->$prop
: NULL;
}
public function retrieveData($path, array $params = array()) {
return $this->proxy->retrieveData(
$this->principal['sid'] . "/$path", $params);
}
public function createData($path, array $params = array()) {
return $this->proxy->createData(
$this->principal['sid'] . "/$path", $params);
}
public function updateData($params) {
$this->cache = $this->proxy->createData(
$this->principal['sid'], $params);
return $this;
}
private function _load($object = NULL) {
$this->cache = $object !== NULL
? $object
: $this->proxy->retrieveData($this->principal['sid']);
if (empty($this->cache->subresource_uris)) return;
foreach ($this->cache->subresource_uris as $res => $uri) {
$type = Services_Twilio_Resource::camelize($res);
$this->cache->$res = new $type($this);
}
}
}
38 changes: 4 additions & 34 deletions Services/Twilio/InstanceResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,16 @@
abstract class Services_Twilio_InstanceResource
extends Services_Twilio_Resource
{
protected $sid;
protected $object;
public function __construct($sid, Services_Twilio_DataProxy $proxy) {
$this->object = is_object($sid) ? $sid : (object)array('sid' => $sid);
$this->sid = $this->object->sid;
parent::__construct($proxy);
}
public function update($params, $value = NULL) {
if (!is_array($params)) {
$params = array($params => $value);
}
$object = $this->proxy->createData($this->sid, $params);
$this->_load($object);
return $this;
$this->proxy->updateData($params);
}
public function setObject($object) {
$this->_load($object);
public function setProxy($proxy) {
$this->proxy = $proxy;
}
public function __get($key) {
if (!isset($this->object->$key)) {
$this->_load();
}
return isset($this->$key)
? $this->$key
: (isset($this->object->$key) ? $this->object->$key : NULL);
}
public function retrieveData($path, array $params = array()) {
return $this->proxy->retrieveData("$this->sid/$path", $params);
}
public function createData($path, array $params = array()) {
return $this->proxy->createData("$this->sid/$path", $params);
}
private function _load($object = NULL) {
$this->object = $object ? $object : $this->proxy->retrieveData($this->sid);
if (empty($this->object->subresource_uris)) return;
foreach ($this->object->subresource_uris as $res => $uri) {
$type = self::camelize($res);
$this->$res = class_exists($type)
? new $type($this)
: new Services_Twilio_ListResource($type, $this);
}
return $this->proxy->$key;
}
}
10 changes: 5 additions & 5 deletions Services/Twilio/ListResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ abstract class Services_Twilio_ListResource
public function get($sid) {
$schema = $this->getSchema();
$type = $schema['instance'];
return new $type($sid, $this);
return new $type(is_object($sid)
? new Services_Twilio_CachingDataProxy(
isset($sid->sid) ? $sid->sid : NULL, $this, $sid
) : new Services_Twilio_CachingDataProxy($sid, $this));
}

protected function _create(array $params) {
$obj = $this->proxy->createData($this->name, $params);
$inst = $this->get($obj->sid);
$inst->setObject($obj);
return $inst;
return $this->get($this->proxy->createData($this->name, $params));
}

public function retrieveData($sid, array $params = array()) {
Expand Down
6 changes: 6 additions & 0 deletions Services/Twilio/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ public function __construct(Services_Twilio_DataProxy $proxy) {
$this->proxy = $proxy;
$this->name = get_class($this);
}
public function retrieveData($path, array $params = array()) {
return $this->proxy->retrieveData($path, $params);
}
public function createData($path, array $params = array()) {
return $this->proxy->createData($path, $params);
}
public static function decamelize($word) {
return preg_replace(
'/(^|[a-z])([A-Z])/e',
Expand Down
4 changes: 4 additions & 0 deletions Services/Twilio/Resources.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ class SmsMessage extends Services_Twilio_InstanceResource {

class AvailablePhoneNumbers extends Services_Twilio_ListResource {
}

class AvailablePhoneNumber extends Services_Twilio_InstanceResource {
}

class OutgoingCallerIds extends Services_Twilio_ListResource {
}
class IncomingPhoneNumbers extends Services_Twilio_ListResource {
Expand Down
2 changes: 2 additions & 0 deletions tests/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@
$loader = new \Mockery\Loader;
$loader->register();

require_once 'Twilio.php';

unset($root, $library, $tests, $path);

2 changes: 0 additions & 2 deletions tests/TwilioTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

use \Mockery as m;

require_once 'Twilio.php';

class TwilioTest extends PHPUnit_Framework_TestCase {
function tearDown() {
m::close();
Expand Down

0 comments on commit bb598bb

Please sign in to comment.