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