This repository has been archived by the owner on Apr 2, 2024. It is now read-only.
forked from luciferous/services_twilio
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor proxy logic out of Resource subclasses
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
1 parent
a1a84a8
commit bb598bb
Showing
9 changed files
with
103 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters