diff --git a/README.md b/README.md index 9c8b92e..af1d928 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# minphp/Cache +# Minphp/Cache [![Build Status](https://travis-ci.org/phillipsdata/minphp-cache.svg?branch=master)](https://travis-ci.org/phillipsdata/minphp-cache) @@ -9,7 +9,7 @@ Cache Library. Install via composer: ```sh -composer require minphp/cache:dev-master +composer require minphp/cache ``` ## Basic Usage diff --git a/composer.json b/composer.json index 36bcdae..efea1b5 100644 --- a/composer.json +++ b/composer.json @@ -19,6 +19,9 @@ "satooshi/php-coveralls": "dev-master" }, "autoload": { - "psr-4": {"minphp\\Cache\\": "src"} + "psr-4": {"Minphp\\Cache\\": "src"} + }, + "autoload-dev": { + "psr-4": {"Minphp\\Cache\\Tests\\": "tests"} } } diff --git a/src/Cache.php b/src/Cache.php index 90e22ba..9ab1509 100644 --- a/src/Cache.php +++ b/src/Cache.php @@ -1,5 +1,5 @@ cache_dir_perms = $cache_dir_perms; $this->cache_ext = $cache_ext; } - + /** * Empties the entire cache of all files (directories excluded, not recursive) * @@ -43,14 +43,14 @@ public function clear($path = null) if (!($dir = @opendir($this->cache_dir . $path))) { return; } - + while ($item = @readdir($dir)) { if (is_file($this->cache_dir . $path . $item)) { @unlink($this->cache_dir . $path . $item); } } } - + /** * Removes the given cache file from the cache * @@ -69,7 +69,7 @@ public function remove($name, $path = null) } return false; } - + /** * Writes the given data to the cache using the name given * @@ -83,18 +83,18 @@ public function remove($name, $path = null) public function write($name, $output, $ttl, $path = null) { $cache = $this->cacheName($name, $path); - + $cache_dir = dirname($cache); if (!file_exists($cache_dir)) { mkdir($cache_dir, $this->cache_dir_perms, true); } - + // Save output to cache file file_put_contents($cache, $output); // Set the cache expiration date/time touch($cache, time()+$ttl); } - + /** * Fetches the contents of a cache, if it exists and is valid * @@ -114,7 +114,7 @@ public function fetch($name, $path = null) return false; } } - + /** * Builds the file name of the cache file based on the name given * diff --git a/tests/CacheTest.php b/tests/CacheTest.php index e1cb264..47bc1bb 100644 --- a/tests/CacheTest.php +++ b/tests/CacheTest.php @@ -1,16 +1,17 @@ cache_dir = dirname(__FILE__) . DIRECTORY_SEPARATOR . "Fixtures" . DIRECTORY_SEPARATOR; @@ -36,58 +37,57 @@ public function testClear() { file_put_contents($this->cache_dir . "testfile", "CacheTest::testClear"); $this->assertFileExists($this->cache_dir . "testfile"); - + $this->cache->clear("bad/sub/path/"); $this->assertFileExists($this->cache_dir . "testfile"); - + $this->cache->clear(); $this->assertFileNotExists($this->cache_dir . "testfile"); - + mkdir($this->cache_dir . "sub/path", 0777, true); file_put_contents($this->cache_dir . "sub/path/testfile", "CacheTest::testEmptyCache"); $this->assertFileExists($this->cache_dir . "sub/path/testfile"); $this->cache->clear("sub/path/"); $this->assertFileNotExists($this->cache_dir . "sub/path/testfile"); rmdir($this->cache_dir . "sub/path"); - rmdir($this->cache_dir . "sub"); } /** * @covers ::remove * @covers ::cacheName - * @uses \minphp\Cache\Cache::fetch - * @uses \minphp\Cache\Cache::write + * @uses \Minphp\Cache\Cache::fetch + * @uses \Minphp\Cache\Cache::write */ public function testRemove() { $cache_name = "testfile"; $cache_contents = "CacheTest::testRemove"; $this->assertFalse($this->cache->remove("bad_file_name")); - + $this->cache->write($cache_name, $cache_contents, 10); $this->assertEquals($cache_contents, $this->cache->fetch($cache_name)); $this->assertTrue($this->cache->remove($cache_name)); - + $this->assertFalse($this->cache->fetch($cache_name)); } /** * @covers ::write * @covers ::cacheName - * @uses \minphp\Cache\Cache::fetch - * @uses \minphp\Cache\Cache::remove + * @uses \Minphp\Cache\Cache::fetch + * @uses \Minphp\Cache\Cache::remove */ public function testWriteCache() { $cache_name = "testfile"; $cache_contents = "CacheTest::testWrite"; - + $this->cache->write($cache_name, $cache_contents, -1); $this->assertFalse($this->cache->fetch($cache_name)); - + $this->assertTrue($this->cache->remove($cache_name)); - + $this->cache->write($cache_name, $cache_contents, 1); $this->assertEquals($cache_contents, $this->cache->fetch($cache_name)); @@ -97,19 +97,19 @@ public function testWriteCache() /** * @covers ::fetch * @covers ::cacheName - * @uses \minphp\Cache\Cache::write - * @uses \minphp\Cache\Cache::remove + * @uses \Minphp\Cache\Cache::write + * @uses \Minphp\Cache\Cache::remove */ public function testFetchCache() { $cache_name = "testfile"; $cache_contents = "CacheTest::testFetch"; - + $this->cache->write($cache_name, $cache_contents, -1); $this->assertFalse($this->cache->fetch($cache_name)); - + $this->assertTrue($this->cache->remove($cache_name)); - + $this->cache->write($cache_name, $cache_contents, 1); $this->assertEquals($cache_contents, $this->cache->fetch($cache_name));