From b19a2efc6c7bb24944e732e566e3d86a4517280c Mon Sep 17 00:00:00 2001 From: Dirk Adler Date: Mon, 20 Jul 2015 17:29:55 +0200 Subject: [PATCH 1/2] cleaned up .gitignore + removed common temp & ide config files - best practise ist to add them to ~/.gitignore_global + added vendor dir --- .gitignore | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 2902d1e..dbbdce1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,3 @@ -*.swp +vendor config.ini -.DS_Store -*.esproj/ -.idea/ iron.json From 17050b3877fc2262f1de0b58716edc6a06ad7dac Mon Sep 17 00:00:00 2001 From: Dirk Adler Date: Mon, 20 Jul 2015 18:07:23 +0200 Subject: [PATCH 2/2] refactored to support PSR-4 & IronCore 1.0.* + moved classes to /src folder + moved test into /test folder + removed phar & build.php --- .gitignore | 2 - build.php | 34 ---- composer.json | 58 +++--- iron_cache.phar | Bin 25763 -> 0 bytes IronCache.class.php => src/IronCache.php | 238 +++++++--------------- src/IronCacheItem.php | 124 +++++++++++ test/.gitignore | 2 + sample_config.ini => test/sample_iron.ini | 0 TestCache.php => test/test.php | 6 +- 9 files changed, 234 insertions(+), 230 deletions(-) delete mode 100644 build.php delete mode 100644 iron_cache.phar rename IronCache.class.php => src/IronCache.php (62%) create mode 100644 src/IronCacheItem.php create mode 100644 test/.gitignore rename sample_config.ini => test/sample_iron.ini (100%) rename TestCache.php => test/test.php (86%) diff --git a/.gitignore b/.gitignore index dbbdce1..22d0d82 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1 @@ vendor -config.ini -iron.json 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 fe5d507b37e728c69c83f393db0c3292dd4f5dfd..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 25763 zcmdsAUvnGDalhD=atc*>Nb=-7&B3e!NCP1C=gu*8Cm#eXMfe~920-alij7(VOL8p$ zi`!j@;z|4feoOgdQLP~K%DcPoN&=tI`+Dt%o+#6#!~OjXn0-B=!EgY2Ren^uE^Rf9iLvKRCdbY0 zc+%=W8+5wPu)6z5{Os|cmVW!uM<0D3fBp=A{s@2M=g;xaCicInfArDc)&Bbz^zYyI zfB1ucP!s+_k4_`~&4CWFauavH3zLjU|C7E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|e<(W723@*b2k3Z>#JJ<&g*}~Kt9#P!^q$rP0MI48Qn&N0 zLvNZN_iK!FbDHQMiD%9bm!5F#=t-yB8NXzFN1bsGxJpMr))aWF@VL`D={ARAa56-S z%MrA!c4yS;Hu2hsb_3XfQ8>??-dK$Azjf%QS6Py)B--sYiYE^KF9URGT`(rBQodN2 zmScWufpeg*TPuwQPOF0-zzhdGXbxZ26wDF51PQEYc164StohU#iRvaxfGuH32{OT3 znDFX8&_#PteNlrCe zC=`yM6}a4JhS8#-Bx7rhqUSvAcAh%DmV>9IK0$fW8R2!?&0z~w=YY}HgCYA-N9hT(taYr|-8lZYfB)4d|NI|EkH7!7|M`!< W`QfAg+WnWO-^2e`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');