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
|
|
|
use Behat\Gherkin\Gherkin; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* File cache. |
19
|
|
|
* Caches feature into a file. |
20
|
|
|
* |
21
|
|
|
* @author Konstantin Kudryashov <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class FileCache implements CacheInterface |
24
|
|
|
{ |
25
|
|
|
private $path; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Initializes file cache. |
29
|
|
|
* |
30
|
|
|
* @param string $path Path to the folder where to store caches. |
31
|
|
|
* |
32
|
|
|
* @throws CacheException |
33
|
|
|
*/ |
34
|
6 |
|
public function __construct($path) |
35
|
|
|
{ |
36
|
6 |
|
$this->path = rtrim($path, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.'v'.Gherkin::VERSION; |
37
|
|
|
|
38
|
6 |
|
if (!is_dir($this->path)) { |
39
|
2 |
|
@mkdir($this->path, 0777, true); |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
6 |
|
if (!is_writeable($this->path)) { |
43
|
1 |
|
throw new CacheException(sprintf('Cache path "%s" is not writeable. Check your filesystem permissions or disable Gherkin file cache.', $this->path)); |
44
|
|
|
} |
45
|
6 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Checks that cache for feature exists and is fresh. |
49
|
|
|
* |
50
|
|
|
* @param string $path Feature path |
51
|
|
|
* @param integer $timestamp The last time feature was updated |
52
|
|
|
* |
53
|
|
|
* @return Boolean |
54
|
|
|
*/ |
55
|
3 |
|
public function isFresh($path, $timestamp) |
56
|
|
|
{ |
57
|
3 |
|
$cachePath = $this->getCachePathFor($path); |
58
|
|
|
|
59
|
3 |
|
if (!file_exists($cachePath)) { |
60
|
1 |
|
return false; |
61
|
|
|
} |
62
|
|
|
|
63
|
2 |
|
return filemtime($cachePath) > $timestamp; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Reads feature cache from path. |
68
|
|
|
* |
69
|
|
|
* @param string $path Feature path |
70
|
|
|
* |
71
|
|
|
* @return FeatureNode |
72
|
|
|
* |
73
|
|
|
* @throws CacheException |
74
|
|
|
*/ |
75
|
2 |
|
public function read($path) |
76
|
|
|
{ |
77
|
2 |
|
$cachePath = $this->getCachePathFor($path); |
78
|
2 |
|
$feature = unserialize(file_get_contents($cachePath)); |
79
|
|
|
|
80
|
2 |
|
if (!$feature instanceof FeatureNode) { |
81
|
1 |
|
throw new CacheException(sprintf('Can not load cache for a feature "%s" from "%s".', $path, $cachePath )); |
82
|
|
|
} |
83
|
|
|
|
84
|
1 |
|
return $feature; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Caches feature node. |
89
|
|
|
* |
90
|
|
|
* @param string $path Feature path |
91
|
|
|
* @param FeatureNode $feature Feature instance |
92
|
|
|
*/ |
93
|
3 |
|
public function write($path, FeatureNode $feature) |
94
|
|
|
{ |
95
|
3 |
|
file_put_contents($this->getCachePathFor($path), serialize($feature)); |
96
|
3 |
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Returns feature cache file path from features path. |
100
|
|
|
* |
101
|
|
|
* @param string $path Feature path |
102
|
|
|
* |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
5 |
|
protected function getCachePathFor($path) |
106
|
|
|
{ |
107
|
5 |
|
return $this->path.'/'.md5($path).'.feature.cache'; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: