Skip to content

Commit 8a54340

Browse files
committed
Move path logic to path trait
1 parent aea469b commit 8a54340

File tree

6 files changed

+91
-111
lines changed

6 files changed

+91
-111
lines changed

src/Cache/CacheProvider.php

Lines changed: 7 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
namespace M1\Vars\Cache;
2020

21+
use M1\Vars\Traits\PathTrait;
2122
use M1\Vars\Vars;
2223

2324
/**
@@ -27,6 +28,11 @@
2728
*/
2829
class CacheProvider
2930
{
31+
/**
32+
* Used for path functions and variables
33+
*/
34+
use PathTrait;
35+
3036
/**
3137
* Has the cache been attempted
3238
*
@@ -62,13 +68,6 @@ class CacheProvider
6268
*/
6369
private $name;
6470

65-
/**
66-
* The specific path for the cache folder
67-
*
68-
* @var string $path
69-
*/
70-
private $path;
71-
7271
/**
7372
* Is the cache turned on
7473
*
@@ -92,7 +91,7 @@ class CacheProvider
9291
public function __construct($resource, $options)
9392
{
9493
$this->setProvide($options['cache']);
95-
$this->setPath($options['cache_path']);
94+
$this->setPath($options['cache_path'], true);
9695

9796
$this->expire = $options['cache_expire'];
9897
$this->name = md5(serialize($resource));
@@ -173,42 +172,6 @@ public function getAttempted()
173172
return $this->attempted;
174173
}
175174

176-
/**
177-
* Returns the cache path
178-
*
179-
* @return string The cache path
180-
*/
181-
public function getPath()
182-
{
183-
return $this->path;
184-
}
185-
186-
/**
187-
* Sets the cache path
188-
*
189-
* @param string $path The cache path to set
190-
*
191-
* @throws \InvalidArgumentException If the cache path does not exist or is not writable
192-
*
193-
* @return \M1\Vars\Cache\CacheProvider
194-
*/
195-
public function setPath($path)
196-
{
197-
if (is_null($path)) {
198-
return;
199-
}
200-
201-
if (!is_dir($path) || !is_writable($path)) {
202-
throw new \InvalidArgumentException(sprintf(
203-
"'%s' cache path does not exist or is not writable",
204-
$path
205-
));
206-
}
207-
208-
$this->path = realpath($path);
209-
return $this;
210-
}
211-
212175
/**
213176
* Returns how long the cache lasts for
214177
*

src/Provider/Silex/VarsServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ private function createOptions($app)
8383
$options = array();
8484

8585
if (isset($app['vars.path'])) {
86-
$options['base_path'] = $app['vars.path'];
86+
$options['path'] = $app['vars.path'];
8787
}
8888

8989
if (isset($app['vars.options'])) {

src/Resource/FileResource.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ private function makePaths($file)
9595
{
9696
$file = realpath($file);
9797

98-
$base_path = $this->provider->vars->getBasePath();
98+
$base_path = $this->provider->vars->getPath();
9999

100100
$filesystem = new Filesystem();
101101
$abs_path = $filesystem->makePathRelative(

src/Traits/PathTrait.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: miles
5+
* Date: 19/12/15
6+
* Time: 11:32
7+
*/
8+
9+
namespace M1\Vars\Traits;
10+
11+
trait PathTrait
12+
{
13+
/**
14+
* The base path for the Vars config and cache folders
15+
*
16+
* @var string $path
17+
*/
18+
public $path;
19+
20+
/**
21+
* Get the path
22+
*
23+
* @return string The path
24+
*/
25+
public function getPath()
26+
{
27+
return $this->path;
28+
}
29+
30+
/**
31+
* Set the Vars base path
32+
*
33+
* @param string $path The path to set
34+
*
35+
* @throws \InvalidArgumentException If the path does not exist or is not writable
36+
*
37+
* @return \M1\Vars\Vars
38+
*/
39+
public function setPath($path, $check_writeable = false)
40+
{
41+
if (is_null($path)) {
42+
return;
43+
}
44+
45+
if (!is_dir($path) || ($check_writeable && !is_writable($path))) {
46+
throw new \InvalidArgumentException(sprintf(
47+
"'%s' base path does not exist or is not writable",
48+
$path
49+
));
50+
}
51+
52+
$this->path = realpath($path);
53+
return $this;
54+
}
55+
}

src/Vars.php

Lines changed: 16 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use M1\Vars\Resource\AbstractResource;
2424
use M1\Vars\Resource\ResourceProvider;
2525
use M1\Vars\Resource\VariableResource;
26+
use M1\Vars\Traits\PathTrait;
2627
use M1\Vars\Traits\TransformerTrait;
2728

2829
/**
@@ -33,16 +34,14 @@
3334
class Vars extends AbstractResource
3435
{
3536
/**
36-
* Used for to* functions
37+
* Used for path functions and variables
3738
*/
38-
use TransformerTrait;
39+
use PathTrait;
3940

4041
/**
41-
* The base path for the Vars config and cache folders
42-
*
43-
* @var string $base_path
42+
* Used for to* functions
4443
*/
45-
private $base_path;
44+
use TransformerTrait;
4645

4746
/**
4847
* The cache object if the cache is wanted, else false
@@ -57,7 +56,7 @@ class Vars extends AbstractResource
5756
* @var array $default_options
5857
*/
5958
private $default_options = array(
60-
'base_path' => null,
59+
'path' => null,
6160
'cache' => true,
6261
'cache_path' => null,
6362
'cache_expire' => 300, // 5 minutes
@@ -157,10 +156,11 @@ private function makeCache($options, $resource)
157156
*/
158157
private function makePaths($options)
159158
{
160-
$this->setBasePath($options['base_path']);
159+
$this->setPath($options['path']);
160+
161+
if (is_null($options['cache_path']) && !is_null($options['path'])) {
161162

162-
if (is_null($options['cache_path']) && !is_null($options['base_path'])) {
163-
$this->cache->setPath($options['base_path']);
163+
$this->cache->setPath($options['path']);
164164
$this->paths_loaded = true;
165165
}
166166
}
@@ -204,7 +204,7 @@ private function loadFromCache()
204204
$this->cache->load();
205205

206206
$passed_keys = array(
207-
'base_path',
207+
'path',
208208
'content',
209209
'extensions',
210210
'loaders',
@@ -231,58 +231,22 @@ private function loadFromCache()
231231
public function pathsLoadedCheck($resource)
232232
{
233233
if (!$this->paths_loaded) {
234-
$base_path = $this->getBasePath();
234+
$path = $this->getPath();
235235

236-
if (!$base_path) {
236+
if (!$path) {
237237
$file = pathinfo(realpath($resource));
238-
$base_path = $file['dirname'];
239-
$this->setBasePath($base_path);
238+
$path = $file['dirname'];
239+
$this->setPath($path);
240240
}
241241

242242
if ($this->cache->getProvide() && !$this->cache->getPath()) {
243-
$this->cache->setPath($base_path);
243+
$this->cache->setPath($path);
244244
}
245245

246246
$this->paths_loaded = true;
247247
}
248248
}
249249

250-
/**
251-
* Get the Vars base path
252-
*
253-
* @return string The Vars base path
254-
*/
255-
public function getBasePath()
256-
{
257-
return $this->base_path;
258-
}
259-
260-
/**
261-
* Set the Vars base path
262-
*
263-
* @param string $base_path The base path to set
264-
*
265-
* @throws \InvalidArgumentException If the base path does not exist or is not writable
266-
*
267-
* @return \M1\Vars\Vars
268-
*/
269-
public function setBasePath($base_path)
270-
{
271-
if (is_null($base_path)) {
272-
return;
273-
}
274-
275-
if (!is_dir($base_path)) {
276-
throw new \InvalidArgumentException(sprintf(
277-
"'%s' base path does not exist or is not writable",
278-
$base_path
279-
));
280-
}
281-
282-
$this->base_path = realpath($base_path);
283-
return $this;
284-
}
285-
286250
/**
287251
* Adds a resource to $this->resources
288252
*

tests/VarsTest.php

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -580,15 +580,15 @@ public function testSetOptions()
580580
{
581581
$resource = __DIR__ . '/mocks/basic/test_pass_1.ini';
582582
$cache_name = sprintf('%s.php', md5(serialize($resource)));
583-
$base_path = __DIR__ . '/mocks/cache';
583+
$path = __DIR__ . '/mocks/cache';
584584
$cache_provide = true;
585585
$cache_path = __DIR__ . '/mocks/cache/output';
586586
$cache_expire = 1000;
587587

588588
$vars = new Vars(
589589
$resource,
590590
array(
591-
'base_path' => $base_path,
591+
'path' => $path,
592592
'cache' => $cache_provide,
593593
'cache_path' => $cache_path,
594594
'cache_expire' => $cache_expire,
@@ -597,8 +597,6 @@ public function testSetOptions()
597597

598598
$cache = $vars->getCache();
599599

600-
$this->assertEquals($base_path, $vars->getBasePath());
601-
602600
$this->assertInstanceOf('\M1\Vars\Cache\CacheProvider', $cache);
603601
$this->assertEquals($cache_provide, $cache->getProvide());
604602
$this->assertEquals($cache_path, $cache->getPath());
@@ -609,24 +607,24 @@ public function testSetOptions()
609607

610608
public function testSetBasePath()
611609
{
612-
$base_path = __DIR__ . '/mocks/cache';
610+
$path = __DIR__ . '/mocks/cache';
613611
$resource = __DIR__ . '/mocks/basic/test_pass_1.ini';
614612
$cache_name = sprintf('%s.php', md5(serialize($resource)));
615613

616614
$vars = new Vars(
617615
$resource,
618616
array(
619-
'base_path' => $base_path,
617+
'path' => $path,
620618
)
621619
);
622620
$cache = $vars->getCache();
623621

624622
$this->assertInstanceOf('\M1\Vars\Cache\CacheProvider', $cache);
625623

626-
$this->assertEquals($base_path, $vars->getBasePath());
627-
$this->assertEquals($base_path, $cache->getPath());
624+
$this->assertEquals($path, $vars->getPath());
625+
$this->assertEquals($path, $cache->getPath());
628626

629-
unlink(sprintf('%s/%s', $base_path, $cache_name));
627+
unlink(sprintf('%s/%s', $path, $cache_name));
630628
}
631629

632630
public function testVariablesSet()
@@ -1021,7 +1019,7 @@ public function testOptionsSilexServiceProvider()
10211019
{
10221020
$resource = __DIR__ . '/mocks/basic/test_pass_1.yml';
10231021
$cache_name = sprintf('%s.php', md5(serialize($resource)));
1024-
$base_path = __DIR__ . '/mocks/cache';
1022+
$path = __DIR__ . '/mocks/cache';
10251023
$cache_provide = true;
10261024
$cache_path = __DIR__ . '/mocks/cache/output';
10271025
$cache_expire = 1000;
@@ -1031,7 +1029,7 @@ public function testOptionsSilexServiceProvider()
10311029
$app->register(
10321030
new \M1\Vars\Provider\Silex\VarsServiceProvider($resource),
10331031
array(
1034-
'vars.path' => $base_path,
1032+
'vars.path' => $path,
10351033
'vars.options' => array(
10361034
'cache' => $cache_provide,
10371035
'cache_path' => $cache_path,
@@ -1041,7 +1039,7 @@ public function testOptionsSilexServiceProvider()
10411039
);
10421040

10431041
$this->assertEquals($this->basic_array, $app['vars']->getContent());
1044-
$this->assertEquals($base_path, $app['vars']->getBasePath());
1042+
$this->assertEquals($path, $app['vars']->getPath());
10451043

10461044
$cache = $app['vars']->getCache();
10471045
$this->assertInstanceOf('\M1\Vars\Cache\CacheProvider', $cache);
@@ -1303,7 +1301,7 @@ public function testNoneExistentBasePath()
13031301
$vars = new Vars(
13041302
__DIR__ . '/mocks/importing/dir_fail_1.yml',
13051303
array(
1306-
'base_path' => __DIR__ . '/mocks/FOLDER_NON_EXISTENT',
1304+
'path' => __DIR__ . '/mocks/FOLDER_NON_EXISTENT',
13071305
'cache' => false,
13081306
)
13091307
);

0 commit comments

Comments
 (0)