Security update for permissions_by_term
[yaffs-website] / vendor / behat / gherkin / src / Behat / Gherkin / Cache / FileCache.php
1 <?php
2
3 /*
4 * This file is part of the Behat Gherkin.
5 * (c) Konstantin Kudryashov <ever.zet@gmail.com>
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 <ever.zet@gmail.com>
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     public function __construct($path)
35     {
36         $this->path = rtrim($path, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.'v'.Gherkin::VERSION;
37
38         if (!is_dir($this->path)) {
39             @mkdir($this->path, 0777, true);
40         }
41
42         if (!is_writeable($this->path)) {
43             throw new CacheException(sprintf('Cache path "%s" is not writeable. Check your filesystem permissions or disable Gherkin file cache.', $this->path));
44         }
45     }
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     public function isFresh($path, $timestamp)
56     {
57         $cachePath = $this->getCachePathFor($path);
58
59         if (!file_exists($cachePath)) {
60             return false;
61         }
62
63         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     public function read($path)
76     {
77         $cachePath = $this->getCachePathFor($path);
78         $feature = unserialize(file_get_contents($cachePath));
79
80         if (!$feature instanceof FeatureNode) {
81             throw new CacheException(sprintf('Can not load cache for a feature "%s" from "%s".', $path, $cachePath ));
82         }
83
84         return $feature;
85     }
86
87     /**
88      * Caches feature node.
89      *
90      * @param string      $path    Feature path
91      * @param FeatureNode $feature Feature instance
92      */
93     public function write($path, FeatureNode $feature)
94     {
95         file_put_contents($this->getCachePathFor($path), serialize($feature));
96     }
97
98     /**
99      * Returns feature cache file path from features path.
100      *
101      * @param string $path Feature path
102      *
103      * @return string
104      */
105     protected function getCachePathFor($path)
106     {
107         return $this->path.'/'.md5($path).'.feature.cache';
108     }
109 }