PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 4.7.0
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v4.7.0
4.7.0 4.6.1 4.6.0 4.5.5 4.5.4 4.5.3 4.5.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.5 3.2.6 3.2.7 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.7.0 3.7.1 All 111 releases
wp-optimize / vendor / mrclay / minify / lib / Minify / Cache / File.php

File.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 4.7.0, at vendor/mrclay/minify/lib/Minify/Cache/File.php

184 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class Minify_Cache_File
4 * @package Minify
5 */
6
7 use Psr\Log\LoggerInterface;
8
9 class Minify_Cache_File implements Minify_CacheInterface
10 {
11
12 /**
13 * @var string
14 */
15 private $path;
16
17 /**
18 * @var bool
19 */
20 private $locking;
21
22 /**
23 * @var LoggerInterface
24 */
25 private $logger;
26
27 /**
28 * @param string $path
29 * @param bool $fileLocking
30 * @param LoggerInterface $logger
31 */
32 public function __construct($path = '', $fileLocking = false, LoggerInterface $logger = null)
33 {
34 if (! $path) {
35 $path = sys_get_temp_dir();
36 }
37 $this->locking = $fileLocking;
38 $this->path = $path;
39
40 if (!$logger) {
41 $logger = new \Monolog\Logger('minify');
42 }
43 $this->logger = $logger;
44 }
45
46 /**
47 * Write data to cache.
48 *
49 * @param string $id cache id (e.g. a filename)
50 *
51 * @param string $data
52 *
53 * @return bool success
54 */
55 public function store($id, $data)
56 {
57 $flag = $this->locking ? LOCK_EX : null;
58 $file = $this->path . '/' . $id;
59
60 if (! @file_put_contents($file, $data, $flag)) {
61 $this->logger->warning("Minify_Cache_File: Write failed to '$file'");
62 }
63
64 // write control
65 if ($data !== $this->fetch($id)) {
66 @unlink($file);
67 $this->logger->warning("Minify_Cache_File: Post-write read failed for '$file'");
68
69 return false;
70 }
71
72 return true;
73 }
74
75 /**
76 * Get the size of a cache entry
77 *
78 * @param string $id cache id (e.g. a filename)
79 *
80 * @return int size in bytes
81 */
82 public function getSize($id)
83 {
84 return filesize($this->path . '/' . $id);
85 }
86
87 /**
88 * Does a valid cache entry exist?
89 *
90 * @param string $id cache id (e.g. a filename)
91 *
92 * @param int $srcMtime mtime of the original source file(s)
93 *
94 * @return bool exists
95 */
96 public function isValid($id, $srcMtime)
97 {
98 $file = $this->path . '/' . $id;
99
100 return (is_file($file) && (filemtime($file) >= $srcMtime));
101 }
102
103 /**
104 * Send the cached content to output
105 *
106 * @param string $id cache id (e.g. a filename)
107 */
108 public function display($id)
109 {
110 if (!$this->locking) {
111 readfile($this->path . '/' . $id);
112
113 return;
114 }
115
116 $fp = fopen($this->path . '/' . $id, 'rb');
117 flock($fp, LOCK_SH);
118 fpassthru($fp);
119 flock($fp, LOCK_UN);
120 fclose($fp);
121 }
122
123 /**
124 * Fetch the cached content
125 *
126 * @param string $id cache id (e.g. a filename)
127 *
128 * @return string
129 */
130 public function fetch($id)
131 {
132 if (!$this->locking) {
133 return file_get_contents($this->path . '/' . $id);
134 }
135
136 $fp = fopen($this->path . '/' . $id, 'rb');
137 if (!$fp) {
138 return false;
139 }
140
141 flock($fp, LOCK_SH);
142 $ret = stream_get_contents($fp);
143 flock($fp, LOCK_UN);
144 fclose($fp);
145
146 return $ret;
147 }
148
149 /**
150 * Fetch the cache path used
151 *
152 * @return string
153 */
154 public function getPath()
155 {
156 return $this->path;
157 }
158
159 /**
160 * Get a usable temp directory
161 *
162 * @return string
163 * @deprecated
164 */
165 public static function tmp()
166 {
167 trigger_error(__METHOD__ . ' is deprecated in Minfy 3.0', E_USER_DEPRECATED);
168
169 return sys_get_temp_dir();
170 }
171
172 /**
173 * Send message to the Minify logger
174 * @param string $msg
175 * @return null
176 * @deprecated Use $this->logger
177 */
178 protected function _log($msg)
179 {
180 trigger_error(__METHOD__ . ' is deprecated in Minify 3.0.', E_USER_DEPRECATED);
181 $this->logger->warning($msg);
182 }
183 }
184