Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / vendor / drush / drush / src / Cache / FileCache.php
1 <?php
2
3 /**
4  * @file
5  * Definition of Drush\Cache\FileCache.
6  */
7
8 namespace Drush\Cache;
9
10 use Drush\Drush;
11 use Symfony\Component\Filesystem\Filesystem;
12 use Symfony\Component\Finder\Finder;
13 use Webmozart\PathUtil\Path;
14
15 /**
16  * Default cache implementation.
17  *
18  * This cache implementation uses plain text files
19  * containing serialized php to store cached data. Each cache bin corresponds
20  * to a directory by the same name.
21  *
22  * @deprecated
23  */
24 class FileCache implements CacheInterface
25 {
26     const EXTENSION = '.cache';
27     protected $bin;
28
29     public function __construct($bin)
30     {
31         $this->bin = $bin;
32         $this->directory = $this->cacheDirectory();
33     }
34
35     /**
36      * Returns the cache directory for the given bin.
37      *
38      * @param string $bin
39      */
40     public function cacheDirectory($bin = null)
41     {
42         $bin = $bin ? $bin : $this->bin;
43         return Path::join(Drush::config()->cache(), $bin);
44     }
45
46     public function get($cid)
47     {
48         $cids = [$cid];
49         $cache = $this->getMultiple($cids);
50         return reset($cache);
51     }
52
53     public function getMultiple(&$cids)
54     {
55         try {
56             $cache = [];
57             foreach ($cids as $cid) {
58                 $filename = $this->getFilePath($cid);
59                 if (!file_exists($filename)) {
60                     return [];
61                 }
62
63                 $item = $this->readFile($filename);
64                 if ($item) {
65                     $cache[$cid] = $item;
66                 }
67             }
68             $cids = array_diff($cids, array_keys($cache));
69             return $cache;
70         } catch (\Exception $e) {
71             return [];
72         }
73     }
74
75     /**
76      * Returns the contents of the given filename unserialized.
77      *
78      * @param string $filename
79      *   Absolute path to filename to read contents from.
80      */
81     public function readFile($filename)
82     {
83         $item = file_get_contents($filename);
84         return $item ? unserialize($item) : false;
85     }
86
87     public function set($cid, $data, $expire = DRUSH_CACHE_PERMANENT)
88     {
89         $created = time();
90
91         $cache = new \stdClass;
92         $cache->cid = $cid;
93         $cache->data = is_object($data) ? clone $data : $data;
94         $cache->created = $created;
95         if ($expire == DRUSH_CACHE_TEMPORARY) {
96             $cache->expire = $created + 2591999;
97         } // Expire time is in seconds if less than 30 days, otherwise is a timestamp.
98         elseif ($expire != DRUSH_CACHE_PERMANENT && $expire < 2592000) {
99             $cache->expire = $created + $expire;
100         } else {
101             $cache->expire = $expire;
102         }
103
104         // Ensure the cache directory still exists, in case a backend process
105         // cleared the cache after the cache was initialized.
106         $fs = new Filesystem();
107         $fs->mkdir($this->directory);
108
109         $filename = $this->getFilePath($cid);
110         return $this->writeFile($filename, $cache);
111     }
112
113     /**
114      * Serializes data and write it to the given filename.
115      *
116      * @param string $filename
117      *   Absolute path to filename to write cache data.
118      * @param $cache
119      *   Cache data to serialize and write to $filename.
120      */
121     public function writeFile($filename, $cache)
122     {
123         return file_put_contents($filename, serialize($cache));
124     }
125
126     public function clear($cid = null, $wildcard = false)
127     {
128         $fs = new Filesystem();
129         $bin_dir = $this->cacheDirectory();
130         $files = [];
131         if (empty($cid)) {
132             $fs->remove($bin_dir);
133         } else {
134             if ($wildcard) {
135                 if ($cid == '*') {
136                     $fs->remove($bin_dir);
137                 } else {
138                     $files = Finder::create()
139                       ->files()
140                       ->name($cid)
141                       ->in($bin_dir);
142                 }
143             } else {
144                 $files[] = $this->getFilePath($cid);
145             }
146
147             $fs->remove($files);
148         }
149     }
150
151     public function isEmpty()
152     {
153         $files = Finder::create()
154           ->files()
155           ->name()
156           ->exclude()
157           ->depth(0)
158           ->in($this->directory);
159         return empty($files);
160     }
161
162     /**
163      * Converts a cache id to a full path.
164      *
165      * @param $cid
166      *   The cache ID of the data to retrieve.
167      *
168      * @return
169      *   The full path to the cache file.
170      */
171     protected function getFilePath($cid)
172     {
173         return $this->directory . '/' . str_replace([':', '\\', '/'], '.', $cid) . self::EXTENSION;
174     }
175 }