Skip to content

Commit

Permalink
Move path logic to path trait
Browse files Browse the repository at this point in the history
  • Loading branch information
m1 committed Dec 19, 2015
1 parent aea469b commit 8a54340
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 111 deletions.
51 changes: 7 additions & 44 deletions src/Cache/CacheProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

namespace M1\Vars\Cache;

use M1\Vars\Traits\PathTrait;
use M1\Vars\Vars;

/**
Expand All @@ -27,6 +28,11 @@
*/
class CacheProvider
{
/**
* Used for path functions and variables
*/
use PathTrait;

/**
* Has the cache been attempted
*
Expand Down Expand Up @@ -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
*
Expand All @@ -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));
Expand Down Expand Up @@ -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
*
Expand Down
2 changes: 1 addition & 1 deletion src/Provider/Silex/VarsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'])) {
Expand Down
2 changes: 1 addition & 1 deletion src/Resource/FileResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
55 changes: 55 additions & 0 deletions src/Traits/PathTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* Created by PhpStorm.
* User: miles
* Date: 19/12/15
* Time: 11:32
*/

namespace M1\Vars\Traits;

trait PathTrait
{
/**
* The base path for the Vars config and cache folders
*
* @var string $path
*/
public $path;

/**
* Get the path
*
* @return string The path
*/
public function getPath()
{
return $this->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;
}
}
68 changes: 16 additions & 52 deletions src/Vars.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -204,7 +204,7 @@ private function loadFromCache()
$this->cache->load();

$passed_keys = array(
'base_path',
'path',
'content',
'extensions',
'loaders',
Expand All @@ -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
*
Expand Down
24 changes: 11 additions & 13 deletions tests/VarsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -580,15 +580,15 @@ 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;

$vars = new Vars(
$resource,
array(
'base_path' => $base_path,
'path' => $path,
'cache' => $cache_provide,
'cache_path' => $cache_path,
'cache_expire' => $cache_expire,
Expand All @@ -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());
Expand All @@ -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()
Expand Down Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -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);
Expand Down Expand Up @@ -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,
)
);
Expand Down

0 comments on commit 8a54340

Please sign in to comment.