From ec6a047799e70c8c55d162bf4b1dc5207cbb8bea Mon Sep 17 00:00:00 2001 From: m1 Date: Mon, 21 Dec 2015 17:35:29 +0000 Subject: [PATCH] Update cache option so folder is created --- README.md | 6 +++++- src/Cache/CacheProvider.php | 11 ++++++++--- tests/VarsTest.php | 18 +++++++++--------- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 71f848a..4a26d31 100644 --- a/README.md +++ b/README.md @@ -418,10 +418,14 @@ getenv('test_key_1.test_key_2'); // value Vars automatically caches the resources for 5 minutes, you can turn this off by setting the `cache` option to `false`. -The `cache_path` if not set is set to what the `base_path` is set to. +The `cache_path` if not set is set to what the `base_path` is set to. The `cache_path` must be writeable. + +To invalidate the cache, simply just remove the folder inside your `cache_path` called `vars`, eg: `rm -rf /var/www/application/app/cache/vars` The cache file is a .php file due to the extra speedup of opcache. +If you're using the Silex provider, then the cache will not be used and set if you're in debug mode. + #### Loaders The loaders are what enable Vars to read the different file types (defaults are Ini, Json, Php, Toml, Xml and Yaml). diff --git a/src/Cache/CacheProvider.php b/src/Cache/CacheProvider.php index d6784c4..ecf939e 100644 --- a/src/Cache/CacheProvider.php +++ b/src/Cache/CacheProvider.php @@ -105,7 +105,7 @@ public function __construct($resource, $options) public function checkCache() { if ($this->provide && $this->path && !$this->getAttempted()) { - $file = sprintf('%s/%s.php', $this->path, $this->name); + $file = sprintf('%s/vars/%s.php', $this->path, $this->name); $this->attempted = true; if (is_file($file) && @@ -122,7 +122,7 @@ public function checkCache() */ public function load() { - $cached_file = sprintf('%s/%s.php', $this->path, $this->name); + $cached_file = sprintf('%s/vars/%s.php', $this->path, $this->name); $this->loaded_vars = unserialize(file_get_contents($cached_file)); } @@ -134,7 +134,12 @@ public function load() public function makeCache(Vars $vars) { if ($this->provide) { - $cache_file = sprintf('%s/%s.php', $this->path, $this->name); + $cache_folder = sprintf("%s/vars", $this->path); + if (!file_exists($cache_folder)) { + mkdir($cache_folder, 0777, true); + } + + $cache_file = sprintf('%s/%s.php', $cache_folder, $this->name); file_put_contents($cache_file, serialize($vars)); } } diff --git a/tests/VarsTest.php b/tests/VarsTest.php index 78ca00e..226eccf 100644 --- a/tests/VarsTest.php +++ b/tests/VarsTest.php @@ -602,7 +602,7 @@ public function testSetOptions() $this->assertEquals($cache_path, $cache->getPath()); $this->assertEquals($cache_expire, $cache->getExpire()); - unlink(sprintf('%s/%s', $cache_path, $cache_name)); + unlink(sprintf('%s/vars/%s', $cache_path, $cache_name)); } public function testSetBasePath() @@ -624,7 +624,7 @@ public function testSetBasePath() $this->assertEquals($path, $vars->getPath()); $this->assertEquals($path, $cache->getPath()); - unlink(sprintf('%s/%s', $path, $cache_name)); + unlink(sprintf('%s/vars/%s', $path, $cache_name)); } public function testVariablesSet() @@ -754,7 +754,7 @@ public function testBasicCache() $this->assertEquals($output, $vars->getContent()); $this->assertEquals($cache_time, $cache->getTime()); - unlink(sprintf('%s/%s', $cache_path, $cache_name)); + unlink(sprintf('%s/vars/%s', $cache_path, $cache_name)); } public function testCacheCheckInResourceProvider() @@ -796,7 +796,7 @@ public function testCacheCheckInResourceProvider() $this->assertEquals($output, $vars->getContent()); $this->assertEquals($cache_time, $cache->getTime()); - unlink(sprintf('%s/%s', $cache_path, $cache_name)); + unlink(sprintf('%s/vars/%s', $cache_path, $cache_name)); } public function testCacheIsCreated() @@ -813,9 +813,9 @@ public function testCacheIsCreated() ) ); - $this->assertTrue(is_file(sprintf('%s/%s', $cache_path, $cache_name))); + $this->assertTrue(is_file(sprintf('%s/vars/%s', $cache_path, $cache_name))); - unlink(sprintf('%s/%s', $cache_path, $cache_name)); + unlink(sprintf('%s/vars/%s', $cache_path, $cache_name)); } public function testCachePathIsSet() @@ -830,10 +830,10 @@ public function testCachePathIsSet() ) ); - $this->assertTrue(is_file(sprintf('%s/%s', $cache_path, $cache_name))); + $this->assertTrue(is_file(sprintf('%s/vars/%s', $cache_path, $cache_name))); $this->assertEquals($cache_path, $vars->getCache()->getPath()); - unlink(sprintf('%s/%s', $cache_path, $cache_name)); + unlink(sprintf('%s/vars/%s', $cache_path, $cache_name)); } public function testGetResourceContent() { @@ -1048,7 +1048,7 @@ public function testOptionsSilexServiceProvider() $this->assertEquals($cache_path, $cache->getPath()); $this->assertEquals($cache_expire, $cache->getExpire()); - unlink(sprintf('%s/%s', $cache_path, $cache_name)); + unlink(sprintf('%s/vars/%s', $cache_path, $cache_name)); } public function testDebugSilexServiceProvider()