diff --git a/.gitignore b/.gitignore index 2902d1e..22d0d82 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1 @@ -*.swp -config.ini -.DS_Store -*.esproj/ -.idea/ -iron.json +vendor diff --git a/build.php b/build.php deleted file mode 100644 index f9cec27..0000000 --- a/build.php +++ /dev/null @@ -1,34 +0,0 @@ -setStub(''); - -# Files -$phar->addFile('../iron_core_php/IronCore.class.php', 'IronCore.class.php'); -$phar->addFile('IronCache.class.php'); -$phar->addFile('LICENSE', 'LICENSE'); - -echo "\ndone - ".(round(filesize('iron_cache.phar')/1024, 2))." KB\n"; - -# Verification -require "phar://iron_cache.phar"; -$cache = new IronCache(); - -echo "Build finished successfully\n"; diff --git a/composer.json b/composer.json index 3a00ccf..1792620 100644 --- a/composer.json +++ b/composer.json @@ -1,29 +1,31 @@ { - "name": "iron-io/iron_cache", - "type": "library", - "description": "Client binding for IronCache (A key/value store in the cloud)", - "keywords": ["cache","iron_cache","iron","store","cloud storage"], - "homepage": "http://github.com/iron-io/iron_cache_php", - "license": "MIT", - "authors": [ - { - "name": "Alexander S", - "email": "alexander@iron.io", - "homepage": "http://iron.io", - "role": "Developer" - } - ], - "require": { - "php": ">=5.2.0", - "iron-io/iron_core": "0.*" - }, - "autoload": { - "files": ["IronCache.class.php"] - }, - "repositories":[ - { - "type":"git", - "url":"http://github.com/iron-io/iron_cache_php" - } - ] -} \ No newline at end of file + "name": "iron-io/iron_cache", + "type": "library", + "description": "Client binding for IronCache (A key/value store in the cloud)", + "keywords": [ + "cache", + "iron_cache", + "iron", + "store", + "cloud storage" + ], + "homepage": "http://github.com/iron-io/iron_cache_php", + "license": "MIT", + "authors": [ + { + "name": "Alexander S", + "email": "alexander@iron.io", + "homepage": "http://iron.io", + "role": "Developer" + } + ], + "require": { + "php": ">=5.2.0", + "iron-io/iron_core": "1.0.*" + }, + "autoload": { + "psr-4": { + "IronCache\\": "src/" + } + } +} diff --git a/iron_cache.phar b/iron_cache.phar deleted file mode 100644 index fe5d507..0000000 Binary files a/iron_cache.phar and /dev/null differ diff --git a/IronCache.class.php b/src/IronCache.php similarity index 62% rename from IronCache.class.php rename to src/IronCache.php index f2d7120..99ea88d 100644 --- a/IronCache.class.php +++ b/src/IronCache.php @@ -5,127 +5,29 @@ * @link https://github.com/iron-io/iron_cache_php * @link http://www.iron.io/products/cache * @link http://dev.iron.io/ - * @version 0.1.3 + * @version 1.0.0 * @package IronCache * @copyright Feel free to copy, steal, take credit for, or whatever you feel like doing with this code. ;) */ +namespace IronCache; -class IronCache_Item -{ - protected $value; - protected $expires_in; - protected $replace; - protected $add; - - const MAX_EXPIRES_IN = 2592000; - - /** - * Create a new item. - * - * @param array|string $item - * An array of item properties or a string of the item value. - * Fields in item array: - * Required: - * - value: string - The item data, as a string. - * Optional: - * - expires_in: integer - How long in seconds to keep the item on the cache before it is deleted. - * Default is 604,800 seconds (7 days). Maximum is 2,592,000 seconds (30 days). - * - replace: boolean - Will only work if key already exists. - * - add: boolean - Will only work if key does not exist. - */ - public function __construct($item) - { - if (is_string($item) || is_integer($item)) { - $this->setValue($item); - } elseif (is_array($item)) { - $this->setValue($item['value']); - if (array_key_exists("replace", $item)) { - $this->setReplace($item['replace']); - } - if (array_key_exists("add", $item)) { - $this->setAdd($item['add']); - } - if (array_key_exists("expires_in", $item)) { - $this->setExpiresIn($item['expires_in']); - } - } - } - - public function setValue($value) - { - if ($value === null) { - throw new InvalidArgumentException("Please specify a value"); - } else { - $this->value = $value; - } - } - - public function getValue() - { - return $this->value; - } - - public function setReplace($replace) - { - $this->replace = $replace; - } - - public function getReplace() - { - return $this->replace; - } - - public function setAdd($add) - { - $this->add = $add; - } - - public function getAdd() - { - return $this->add; - } - - public function setExpiresIn($expires_in) - { - if ($expires_in > self::MAX_EXPIRES_IN) { - throw new InvalidArgumentException("Expires In can't be greater than ".self::MAX_EXPIRES_IN."."); - } else { - $this->expires_in = $expires_in; - } - } - - public function getExpiresIn() - { - return $this->expires_in; - } - - public function asArray() - { - $array = array(); - $array['value'] = $this->getValue(); - if ($this->getExpiresIn() != null) { - $array['expires_in'] = $this->getExpiresIn(); - } - if ($this->getReplace() != null) { - $array['replace'] = $this->getReplace(); - } - if ($this->getAdd() != null) { - $array['add'] = $this->getAdd(); - } - return $array; - } -} +use IronCore\IronCore; +use IronCore\HttpException; +/** + * Class IronCache + * @package IronCache + */ class IronCache extends IronCore { protected $client_version = '0.1.3'; - protected $client_name = 'iron_cache_php'; - protected $product_name = 'iron_cache'; + protected $client_name = 'iron_cache_php'; + protected $product_name = 'iron_cache'; protected $default_values = array( - 'protocol' => 'https', - 'host' => 'cache-aws-us-east-1.iron.io', - 'port' => '443', + 'protocol' => 'https', + 'host' => 'cache-aws-us-east-1.iron.io', + 'port' => '443', 'api_version' => '1', ); @@ -134,20 +36,20 @@ class IronCache extends IronCore public $session_expire_time = 172800; # 2 days /** - * @param string|array $config - * Array of options or name of config file. - * Fields in options array or in config: - * - * Required: - * - token - * - project_id - * Optional: - * - protocol - * - host - * - port - * - api_version - * @param string|null $cache_name set default cache name - */ + * @param string|array $config + * Array of options or name of config file. + * Fields in options array or in config: + * + * Required: + * - token + * - project_id + * Optional: + * - protocol + * - host + * - port + * - api_version + * @param string|null $cache_name set default cache name + */ public function __construct($config = null, $cache_name = null) { $this->getConfigData($config); @@ -156,27 +58,27 @@ public function __construct($config = null, $cache_name = null) } /** - * Switch active project - * - * @param string $project_id Project ID - * @throws InvalidArgumentException - */ + * Switch active project + * + * @param string $project_id Project ID + * @throws \InvalidArgumentException + */ public function setProjectId($project_id) { if (!empty($project_id)) { $this->project_id = $project_id; } if (empty($this->project_id)) { - throw new InvalidArgumentException("Please set project_id"); + throw new \InvalidArgumentException("Please set project_id"); } } /** - * Set default cache name - * - * @param string $cache_name name of cache - * @throws InvalidArgumentException - */ + * Set default cache name + * + * @param string $cache_name name of cache + * @throws \InvalidArgumentException + */ public function setCacheName($cache_name) { if (!empty($cache_name)) { @@ -193,21 +95,23 @@ public function getCaches($page = 0) $params['page'] = $page; } $this->setJsonHeaders(); + return self::json_decode($this->apiCall(self::GET, $url, $params)); } /** - * Get information about cache. - * Also returns cache size. - * - * @param string $cache - * @return mixed - */ + * Get information about cache. + * Also returns cache size. + * + * @param string $cache + * @return mixed + */ public function getCache($cache) { $cache = self::encodeCache($cache); $url = "projects/{$this->project_id}/caches/$cache"; $this->setJsonHeaders(); + return self::json_decode($this->apiCall(self::GET, $url)); } @@ -235,13 +139,14 @@ public function getCache($cache) public function putItem($cache, $key, $item) { $cache = self::encodeCache($cache); - $key = self::encodeKey($key); - $itm = new IronCache_Item($item); - $req = $itm->asArray(); - $url = "projects/{$this->project_id}/caches/$cache/items/$key"; + $key = self::encodeKey($key); + $itm = new IronCacheItem($item); + $req = $itm->asArray(); + $url = "projects/{$this->project_id}/caches/$cache/items/$key"; $this->setJsonHeaders(); $res = $this->apiCall(self::PUT, $url, $req); + return self::json_decode($res); } @@ -251,34 +156,36 @@ public function putItem($cache, $key, $item) * @param string $cache Cache name * @param string $key Cache key * @return mixed|null single item or null - * @throws Http_Exception + * @throws HttpException */ public function getItem($cache, $key) { $cache = self::encodeCache($cache); - $key = self::encodeKey($key); - $url = "projects/{$this->project_id}/caches/$cache/items/$key"; + $key = self::encodeKey($key); + $url = "projects/{$this->project_id}/caches/$cache/items/$key"; $this->setJsonHeaders(); try { $res = $this->apiCall(self::GET, $url); - } catch (Http_Exception $e) { - if ($e->getCode() == Http_Exception::NOT_FOUND) { + } catch (HttpException $e) { + if ($e->getCode() == HttpException::NOT_FOUND) { return null; } else { throw $e; } } + return self::json_decode($res); } public function deleteItem($cache, $key) { $cache = self::encodeCache($cache); - $key = self::encodeKey($key); - $url = "projects/{$this->project_id}/caches/$cache/items/$key"; + $key = self::encodeKey($key); + $url = "projects/{$this->project_id}/caches/$cache/items/$key"; $this->setJsonHeaders(); + return self::json_decode($this->apiCall(self::DELETE, $url)); } @@ -297,12 +204,13 @@ public function deleteItem($cache, $key) public function incrementItem($cache, $key, $amount = 1) { $cache = self::encodeCache($cache); - $key = self::encodeKey($key); + $key = self::encodeKey($key); $url = "projects/{$this->project_id}/caches/$cache/items/$key/increment"; $params = array( 'amount' => $amount ); $this->setJsonHeaders(); + return self::json_decode($this->apiCall(self::POST, $url, $params)); } @@ -313,7 +221,7 @@ public function incrementItem($cache, $key, $amount = 1) * * @param string $key * @return mixed|null - * @throws InvalidArgumentException + * @throws \InvalidArgumentException */ public function get($key) { @@ -327,7 +235,7 @@ public function get($key) * @param string $key * @param array|string $item * @return mixed - * @throws InvalidArgumentException + * @throws \InvalidArgumentException */ public function put($key, $item) { @@ -340,7 +248,7 @@ public function put($key, $item) * * @param string $key * @return mixed|void - * @throws InvalidArgumentException + * @throws \InvalidArgumentException */ public function delete($key) { @@ -354,7 +262,7 @@ public function delete($key) * @param string $key * @param int $amount * @return mixed|void - * @throws InvalidArgumentException + * @throws \InvalidArgumentException */ public function increment($key, $amount = 1) { @@ -377,6 +285,7 @@ public function clear($cache = null) $url = "projects/{$this->project_id}/caches/$cache/clear"; $params = array(); $this->setJsonHeaders(); + return self::json_decode($this->apiCall(self::POST, $url, $params)); } @@ -384,6 +293,7 @@ public function clear($cache = null) public function session_open($savePath, $sessionName) { $this->setCacheName($sessionName); + return true; } @@ -408,6 +318,7 @@ public function session_write($id, $data) "value" => $data, "expires_in" => $this->session_expire_time )); + return true; } @@ -415,9 +326,10 @@ public function session_destroy($id) { try { $this->delete($id); - } catch (Exception $e) { + } catch (\Exception $e) { # ignore any exceptions } + return true; } @@ -453,16 +365,18 @@ public function set_as_session_store($session_expire_time = null) protected static function encodeCache($cache) { if (empty($cache)) { - throw new InvalidArgumentException('Please set $cache variable'); + throw new \InvalidArgumentException('Please set $cache variable'); } + return rawurlencode($cache); } protected static function encodeKey($key) { if (empty($key)) { - throw new InvalidArgumentException('Please set $key variable'); + throw new \InvalidArgumentException('Please set $key variable'); } + return rawurlencode($key); } @@ -475,6 +389,6 @@ protected function setJsonHeaders() protected function setPostHeaders() { $this->setCommonHeaders(); - $this->headers['Content-Type'] ='multipart/form-data'; + $this->headers['Content-Type'] = 'multipart/form-data'; } } diff --git a/src/IronCacheItem.php b/src/IronCacheItem.php new file mode 100644 index 0000000..61154a4 --- /dev/null +++ b/src/IronCacheItem.php @@ -0,0 +1,124 @@ +setValue($item); + } elseif (is_array($item)) { + $this->setValue($item['value']); + if (array_key_exists("replace", $item)) { + $this->setReplace($item['replace']); + } + if (array_key_exists("add", $item)) { + $this->setAdd($item['add']); + } + if (array_key_exists("expires_in", $item)) { + $this->setExpiresIn($item['expires_in']); + } + } + } + + public function setValue($value) + { + if ($value === null) { + throw new \InvalidArgumentException("Please specify a value"); + } else { + $this->value = $value; + } + } + + public function getValue() + { + return $this->value; + } + + public function setReplace($replace) + { + $this->replace = $replace; + } + + public function getReplace() + { + return $this->replace; + } + + public function setAdd($add) + { + $this->add = $add; + } + + public function getAdd() + { + return $this->add; + } + + public function setExpiresIn($expires_in) + { + if ($expires_in > self::MAX_EXPIRES_IN) { + throw new \InvalidArgumentException("Expires In can't be greater than " . self::MAX_EXPIRES_IN . "."); + } else { + $this->expires_in = $expires_in; + } + } + + public function getExpiresIn() + { + return $this->expires_in; + } + + public function asArray() + { + $array = array(); + $array['value'] = $this->getValue(); + if ($this->getExpiresIn() != null) { + $array['expires_in'] = $this->getExpiresIn(); + } + if ($this->getReplace() != null) { + $array['replace'] = $this->getReplace(); + } + if ($this->getAdd() != null) { + $array['add'] = $this->getAdd(); + } + + return $array; + } +} diff --git a/test/.gitignore b/test/.gitignore new file mode 100644 index 0000000..fa20ff2 --- /dev/null +++ b/test/.gitignore @@ -0,0 +1,2 @@ +iron.ini +iron.json diff --git a/sample_config.ini b/test/sample_iron.ini similarity index 100% rename from sample_config.ini rename to test/sample_iron.ini diff --git a/TestCache.php b/test/test.php similarity index 86% rename from TestCache.php rename to test/test.php index fb2d69a..cab7c0f 100644 --- a/TestCache.php +++ b/test/test.php @@ -1,10 +1,8 @@ ssl_verifypeer = false; #$cache->debug_enabled = true; $cache->setCacheName('cache #4');