diff --git a/src/Cache/CacheProvider.php b/src/Cache/CacheProvider.php index a9ab637..d6784c4 100644 --- a/src/Cache/CacheProvider.php +++ b/src/Cache/CacheProvider.php @@ -18,6 +18,7 @@ namespace M1\Vars\Cache; +use M1\Vars\Traits\PathTrait; use M1\Vars\Vars; /** @@ -27,6 +28,11 @@ */ class CacheProvider { + /** + * Used for path functions and variables + */ + use PathTrait; + /** * Has the cache been attempted * @@ -62,13 +68,6 @@ class CacheProvider */ private $name; - /** - * The specific path for the cache folder - * - * @var string $path - */ - private $path; - /** * Is the cache turned on * @@ -92,7 +91,7 @@ class CacheProvider public function __construct($resource, $options) { $this->setProvide($options['cache']); - $this->setPath($options['cache_path']); + $this->setPath($options['cache_path'], true); $this->expire = $options['cache_expire']; $this->name = md5(serialize($resource)); @@ -173,42 +172,6 @@ public function getAttempted() return $this->attempted; } - /** - * Returns the cache path - * - * @return string The cache path - */ - public function getPath() - { - return $this->path; - } - - /** - * Sets the cache path - * - * @param string $path The cache path to set - * - * @throws \InvalidArgumentException If the cache path does not exist or is not writable - * - * @return \M1\Vars\Cache\CacheProvider - */ - public function setPath($path) - { - if (is_null($path)) { - return; - } - - if (!is_dir($path) || !is_writable($path)) { - throw new \InvalidArgumentException(sprintf( - "'%s' cache path does not exist or is not writable", - $path - )); - } - - $this->path = realpath($path); - return $this; - } - /** * Returns how long the cache lasts for * diff --git a/src/Provider/Silex/VarsServiceProvider.php b/src/Provider/Silex/VarsServiceProvider.php index 9d6dc42..78676f0 100644 --- a/src/Provider/Silex/VarsServiceProvider.php +++ b/src/Provider/Silex/VarsServiceProvider.php @@ -83,7 +83,7 @@ private function createOptions($app) $options = array(); if (isset($app['vars.path'])) { - $options['base_path'] = $app['vars.path']; + $options['path'] = $app['vars.path']; } if (isset($app['vars.options'])) { diff --git a/src/Resource/FileResource.php b/src/Resource/FileResource.php index d57ffe4..b3858bf 100644 --- a/src/Resource/FileResource.php +++ b/src/Resource/FileResource.php @@ -95,7 +95,7 @@ private function makePaths($file) { $file = realpath($file); - $base_path = $this->provider->vars->getBasePath(); + $base_path = $this->provider->vars->getPath(); $filesystem = new Filesystem(); $abs_path = $filesystem->makePathRelative( diff --git a/src/Traits/PathTrait.php b/src/Traits/PathTrait.php new file mode 100644 index 0000000..6446d42 --- /dev/null +++ b/src/Traits/PathTrait.php @@ -0,0 +1,55 @@ +path; + } + + /** + * Set the Vars base path + * + * @param string $path The path to set + * + * @throws \InvalidArgumentException If the path does not exist or is not writable + * + * @return \M1\Vars\Vars + */ + public function setPath($path, $check_writeable = false) + { + if (is_null($path)) { + return; + } + + if (!is_dir($path) || ($check_writeable && !is_writable($path))) { + throw new \InvalidArgumentException(sprintf( + "'%s' base path does not exist or is not writable", + $path + )); + } + + $this->path = realpath($path); + return $this; + } +} \ No newline at end of file diff --git a/src/Vars.php b/src/Vars.php index 4f94d68..b0ff735 100644 --- a/src/Vars.php +++ b/src/Vars.php @@ -23,6 +23,7 @@ use M1\Vars\Resource\AbstractResource; use M1\Vars\Resource\ResourceProvider; use M1\Vars\Resource\VariableResource; +use M1\Vars\Traits\PathTrait; use M1\Vars\Traits\TransformerTrait; /** @@ -33,16 +34,14 @@ class Vars extends AbstractResource { /** - * Used for to* functions + * Used for path functions and variables */ - use TransformerTrait; + use PathTrait; /** - * The base path for the Vars config and cache folders - * - * @var string $base_path + * Used for to* functions */ - private $base_path; + use TransformerTrait; /** * The cache object if the cache is wanted, else false @@ -57,7 +56,7 @@ class Vars extends AbstractResource * @var array $default_options */ private $default_options = array( - 'base_path' => null, + 'path' => null, 'cache' => true, 'cache_path' => null, 'cache_expire' => 300, // 5 minutes @@ -157,10 +156,11 @@ private function makeCache($options, $resource) */ private function makePaths($options) { - $this->setBasePath($options['base_path']); + $this->setPath($options['path']); + + if (is_null($options['cache_path']) && !is_null($options['path'])) { - if (is_null($options['cache_path']) && !is_null($options['base_path'])) { - $this->cache->setPath($options['base_path']); + $this->cache->setPath($options['path']); $this->paths_loaded = true; } } @@ -204,7 +204,7 @@ private function loadFromCache() $this->cache->load(); $passed_keys = array( - 'base_path', + 'path', 'content', 'extensions', 'loaders', @@ -231,58 +231,22 @@ private function loadFromCache() public function pathsLoadedCheck($resource) { if (!$this->paths_loaded) { - $base_path = $this->getBasePath(); + $path = $this->getPath(); - if (!$base_path) { + if (!$path) { $file = pathinfo(realpath($resource)); - $base_path = $file['dirname']; - $this->setBasePath($base_path); + $path = $file['dirname']; + $this->setPath($path); } if ($this->cache->getProvide() && !$this->cache->getPath()) { - $this->cache->setPath($base_path); + $this->cache->setPath($path); } $this->paths_loaded = true; } } - /** - * Get the Vars base path - * - * @return string The Vars base path - */ - public function getBasePath() - { - return $this->base_path; - } - - /** - * Set the Vars base path - * - * @param string $base_path The base path to set - * - * @throws \InvalidArgumentException If the base path does not exist or is not writable - * - * @return \M1\Vars\Vars - */ - public function setBasePath($base_path) - { - if (is_null($base_path)) { - return; - } - - if (!is_dir($base_path)) { - throw new \InvalidArgumentException(sprintf( - "'%s' base path does not exist or is not writable", - $base_path - )); - } - - $this->base_path = realpath($base_path); - return $this; - } - /** * Adds a resource to $this->resources * diff --git a/tests/VarsTest.php b/tests/VarsTest.php index 165da00..78ca00e 100644 --- a/tests/VarsTest.php +++ b/tests/VarsTest.php @@ -580,7 +580,7 @@ public function testSetOptions() { $resource = __DIR__ . '/mocks/basic/test_pass_1.ini'; $cache_name = sprintf('%s.php', md5(serialize($resource))); - $base_path = __DIR__ . '/mocks/cache'; + $path = __DIR__ . '/mocks/cache'; $cache_provide = true; $cache_path = __DIR__ . '/mocks/cache/output'; $cache_expire = 1000; @@ -588,7 +588,7 @@ public function testSetOptions() $vars = new Vars( $resource, array( - 'base_path' => $base_path, + 'path' => $path, 'cache' => $cache_provide, 'cache_path' => $cache_path, 'cache_expire' => $cache_expire, @@ -597,8 +597,6 @@ public function testSetOptions() $cache = $vars->getCache(); - $this->assertEquals($base_path, $vars->getBasePath()); - $this->assertInstanceOf('\M1\Vars\Cache\CacheProvider', $cache); $this->assertEquals($cache_provide, $cache->getProvide()); $this->assertEquals($cache_path, $cache->getPath()); @@ -609,24 +607,24 @@ public function testSetOptions() public function testSetBasePath() { - $base_path = __DIR__ . '/mocks/cache'; + $path = __DIR__ . '/mocks/cache'; $resource = __DIR__ . '/mocks/basic/test_pass_1.ini'; $cache_name = sprintf('%s.php', md5(serialize($resource))); $vars = new Vars( $resource, array( - 'base_path' => $base_path, + 'path' => $path, ) ); $cache = $vars->getCache(); $this->assertInstanceOf('\M1\Vars\Cache\CacheProvider', $cache); - $this->assertEquals($base_path, $vars->getBasePath()); - $this->assertEquals($base_path, $cache->getPath()); + $this->assertEquals($path, $vars->getPath()); + $this->assertEquals($path, $cache->getPath()); - unlink(sprintf('%s/%s', $base_path, $cache_name)); + unlink(sprintf('%s/%s', $path, $cache_name)); } public function testVariablesSet() @@ -1021,7 +1019,7 @@ public function testOptionsSilexServiceProvider() { $resource = __DIR__ . '/mocks/basic/test_pass_1.yml'; $cache_name = sprintf('%s.php', md5(serialize($resource))); - $base_path = __DIR__ . '/mocks/cache'; + $path = __DIR__ . '/mocks/cache'; $cache_provide = true; $cache_path = __DIR__ . '/mocks/cache/output'; $cache_expire = 1000; @@ -1031,7 +1029,7 @@ public function testOptionsSilexServiceProvider() $app->register( new \M1\Vars\Provider\Silex\VarsServiceProvider($resource), array( - 'vars.path' => $base_path, + 'vars.path' => $path, 'vars.options' => array( 'cache' => $cache_provide, 'cache_path' => $cache_path, @@ -1041,7 +1039,7 @@ public function testOptionsSilexServiceProvider() ); $this->assertEquals($this->basic_array, $app['vars']->getContent()); - $this->assertEquals($base_path, $app['vars']->getBasePath()); + $this->assertEquals($path, $app['vars']->getPath()); $cache = $app['vars']->getCache(); $this->assertInstanceOf('\M1\Vars\Cache\CacheProvider', $cache); @@ -1303,7 +1301,7 @@ public function testNoneExistentBasePath() $vars = new Vars( __DIR__ . '/mocks/importing/dir_fail_1.yml', array( - 'base_path' => __DIR__ . '/mocks/FOLDER_NON_EXISTENT', + 'path' => __DIR__ . '/mocks/FOLDER_NON_EXISTENT', 'cache' => false, ) );