1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Behat Gherkin. |
5
|
|
|
* (c) Konstantin Kudryashov <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Behat\Gherkin\Cache; |
12
|
|
|
|
13
|
|
|
use Behat\Gherkin\Exception\CacheException; |
14
|
|
|
use Behat\Gherkin\Node\FeatureNode; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* File cache. |
18
|
|
|
* Caches feature into a file. |
19
|
|
|
* |
20
|
|
|
* @author Konstantin Kudryashov <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class FileCache implements CacheInterface |
23
|
|
|
{ |
24
|
|
|
private $path; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Initializes file cache. |
28
|
|
|
* |
29
|
|
|
* @param string $path Path to the folder where to store caches. |
30
|
|
|
*/ |
31
|
5 |
|
public function __construct($path) |
32
|
|
|
{ |
33
|
5 |
|
$this->path = rtrim($path, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.'412'; |
34
|
|
|
|
35
|
5 |
|
if (!is_dir($this->path)) { |
36
|
1 |
|
mkdir($this->path, 0777, true); |
37
|
1 |
|
} |
38
|
5 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Checks that cache for feature exists and is fresh. |
42
|
|
|
* |
43
|
|
|
* @param string $path Feature path |
44
|
|
|
* @param integer $timestamp The last time feature was updated |
45
|
|
|
* |
46
|
|
|
* @return Boolean |
47
|
|
|
*/ |
48
|
3 |
|
public function isFresh($path, $timestamp) |
49
|
|
|
{ |
50
|
3 |
|
$cachePath = $this->getCachePathFor($path); |
51
|
|
|
|
52
|
3 |
|
if (!file_exists($cachePath)) { |
53
|
1 |
|
return false; |
54
|
|
|
} |
55
|
|
|
|
56
|
2 |
|
return filemtime($cachePath) > $timestamp; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Reads feature cache from path. |
61
|
|
|
* |
62
|
|
|
* @param string $path Feature path |
63
|
|
|
* |
64
|
|
|
* @return FeatureNode |
65
|
|
|
* |
66
|
|
|
* @throws CacheException |
67
|
|
|
*/ |
68
|
2 |
|
public function read($path) |
69
|
|
|
{ |
70
|
2 |
|
$cachePath = $this->getCachePathFor($path); |
71
|
2 |
|
$feature = unserialize(file_get_contents($cachePath)); |
72
|
|
|
|
73
|
2 |
|
if (!$feature instanceof FeatureNode) { |
74
|
1 |
|
throw new CacheException(sprintf('Can not load cache for a feature "%s" from "%s".', $path, $cachePath )); |
75
|
|
|
} |
76
|
|
|
|
77
|
1 |
|
return $feature; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Caches feature node. |
82
|
|
|
* |
83
|
|
|
* @param string $path Feature path |
84
|
|
|
* @param FeatureNode $feature Feature instance |
85
|
|
|
*/ |
86
|
3 |
|
public function write($path, FeatureNode $feature) |
87
|
|
|
{ |
88
|
3 |
|
file_put_contents($this->getCachePathFor($path), serialize($feature)); |
89
|
3 |
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Returns feature cache file path from features path. |
93
|
|
|
* |
94
|
|
|
* @param string $path Feature path |
95
|
|
|
* |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
5 |
|
protected function getCachePathFor($path) |
99
|
|
|
{ |
100
|
5 |
|
return $this->path.'/'.md5($path).'.feature.cache'; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|