Mercurial > hg > isophonics-drupal-site
comparison vendor/symfony/http-kernel/HttpCache/Store.php @ 17:129ea1e6d783
Update, including to Drupal core 8.6.10
author | Chris Cannam |
---|---|
date | Thu, 28 Feb 2019 13:21:36 +0000 |
parents | 1fec387a4317 |
children |
comparison
equal
deleted
inserted
replaced
16:c2387f117808 | 17:129ea1e6d783 |
---|---|
38 $this->root = $root; | 38 $this->root = $root; |
39 if (!file_exists($this->root) && !@mkdir($this->root, 0777, true) && !is_dir($this->root)) { | 39 if (!file_exists($this->root) && !@mkdir($this->root, 0777, true) && !is_dir($this->root)) { |
40 throw new \RuntimeException(sprintf('Unable to create the store directory (%s).', $this->root)); | 40 throw new \RuntimeException(sprintf('Unable to create the store directory (%s).', $this->root)); |
41 } | 41 } |
42 $this->keyCache = new \SplObjectStorage(); | 42 $this->keyCache = new \SplObjectStorage(); |
43 $this->locks = array(); | 43 $this->locks = []; |
44 } | 44 } |
45 | 45 |
46 /** | 46 /** |
47 * Cleanups storage. | 47 * Cleanups storage. |
48 */ | 48 */ |
52 foreach ($this->locks as $lock) { | 52 foreach ($this->locks as $lock) { |
53 flock($lock, LOCK_UN); | 53 flock($lock, LOCK_UN); |
54 fclose($lock); | 54 fclose($lock); |
55 } | 55 } |
56 | 56 |
57 $this->locks = array(); | 57 $this->locks = []; |
58 } | 58 } |
59 | 59 |
60 /** | 60 /** |
61 * Tries to lock the cache for a given Request, without blocking. | 61 * Tries to lock the cache for a given Request, without blocking. |
62 * | 62 * |
66 { | 66 { |
67 $key = $this->getCacheKey($request); | 67 $key = $this->getCacheKey($request); |
68 | 68 |
69 if (!isset($this->locks[$key])) { | 69 if (!isset($this->locks[$key])) { |
70 $path = $this->getPath($key); | 70 $path = $this->getPath($key); |
71 if (!file_exists(dirname($path)) && false === @mkdir(dirname($path), 0777, true) && !is_dir(dirname($path))) { | 71 if (!file_exists(\dirname($path)) && false === @mkdir(\dirname($path), 0777, true) && !is_dir(\dirname($path))) { |
72 return $path; | 72 return $path; |
73 } | 73 } |
74 $h = fopen($path, 'cb'); | 74 $h = fopen($path, 'cb'); |
75 if (!flock($h, LOCK_EX | LOCK_NB)) { | 75 if (!flock($h, LOCK_EX | LOCK_NB)) { |
76 fclose($h); | 76 fclose($h); |
185 } | 185 } |
186 | 186 |
187 $response->headers->set('X-Content-Digest', $digest); | 187 $response->headers->set('X-Content-Digest', $digest); |
188 | 188 |
189 if (!$response->headers->has('Transfer-Encoding')) { | 189 if (!$response->headers->has('Transfer-Encoding')) { |
190 $response->headers->set('Content-Length', strlen($response->getContent())); | 190 $response->headers->set('Content-Length', \strlen($response->getContent())); |
191 } | 191 } |
192 } | 192 } |
193 | 193 |
194 // read existing cache entries, remove non-varying, and add this one to the list | 194 // read existing cache entries, remove non-varying, and add this one to the list |
195 $entries = array(); | 195 $entries = []; |
196 $vary = $response->headers->get('vary'); | 196 $vary = $response->headers->get('vary'); |
197 foreach ($this->getMetadata($key) as $entry) { | 197 foreach ($this->getMetadata($key) as $entry) { |
198 if (!isset($entry[1]['vary'][0])) { | 198 if (!isset($entry[1]['vary'][0])) { |
199 $entry[1]['vary'] = array(''); | 199 $entry[1]['vary'] = ['']; |
200 } | 200 } |
201 | 201 |
202 if ($entry[1]['vary'][0] != $vary || !$this->requestsMatch($vary, $entry[0], $storedEnv)) { | 202 if ($entry[1]['vary'][0] != $vary || !$this->requestsMatch($vary, $entry[0], $storedEnv)) { |
203 $entries[] = $entry; | 203 $entries[] = $entry; |
204 } | 204 } |
205 } | 205 } |
206 | 206 |
207 $headers = $this->persistResponse($response); | 207 $headers = $this->persistResponse($response); |
208 unset($headers['age']); | 208 unset($headers['age']); |
209 | 209 |
210 array_unshift($entries, array($storedEnv, $headers)); | 210 array_unshift($entries, [$storedEnv, $headers]); |
211 | 211 |
212 if (false === $this->save($key, serialize($entries))) { | 212 if (false === $this->save($key, serialize($entries))) { |
213 throw new \RuntimeException('Unable to store the metadata.'); | 213 throw new \RuntimeException('Unable to store the metadata.'); |
214 } | 214 } |
215 | 215 |
234 public function invalidate(Request $request) | 234 public function invalidate(Request $request) |
235 { | 235 { |
236 $modified = false; | 236 $modified = false; |
237 $key = $this->getCacheKey($request); | 237 $key = $this->getCacheKey($request); |
238 | 238 |
239 $entries = array(); | 239 $entries = []; |
240 foreach ($this->getMetadata($key) as $entry) { | 240 foreach ($this->getMetadata($key) as $entry) { |
241 $response = $this->restoreResponse($entry[1]); | 241 $response = $this->restoreResponse($entry[1]); |
242 if ($response->isFresh()) { | 242 if ($response->isFresh()) { |
243 $response->expire(); | 243 $response->expire(); |
244 $modified = true; | 244 $modified = true; |
245 $entries[] = array($entry[0], $this->persistResponse($response)); | 245 $entries[] = [$entry[0], $this->persistResponse($response)]; |
246 } else { | 246 } else { |
247 $entries[] = $entry; | 247 $entries[] = $entry; |
248 } | 248 } |
249 } | 249 } |
250 | 250 |
291 * @return array An array of data associated with the key | 291 * @return array An array of data associated with the key |
292 */ | 292 */ |
293 private function getMetadata($key) | 293 private function getMetadata($key) |
294 { | 294 { |
295 if (!$entries = $this->load($key)) { | 295 if (!$entries = $this->load($key)) { |
296 return array(); | 296 return []; |
297 } | 297 } |
298 | 298 |
299 return unserialize($entries); | 299 return unserialize($entries); |
300 } | 300 } |
301 | 301 |
373 if (isset($this->locks[$key])) { | 373 if (isset($this->locks[$key])) { |
374 $fp = $this->locks[$key]; | 374 $fp = $this->locks[$key]; |
375 @ftruncate($fp, 0); | 375 @ftruncate($fp, 0); |
376 @fseek($fp, 0); | 376 @fseek($fp, 0); |
377 $len = @fwrite($fp, $data); | 377 $len = @fwrite($fp, $data); |
378 if (strlen($data) !== $len) { | 378 if (\strlen($data) !== $len) { |
379 @ftruncate($fp, 0); | 379 @ftruncate($fp, 0); |
380 | 380 |
381 return false; | 381 return false; |
382 } | 382 } |
383 } else { | 383 } else { |
384 if (!file_exists(dirname($path)) && false === @mkdir(dirname($path), 0777, true) && !is_dir(dirname($path))) { | 384 if (!file_exists(\dirname($path)) && false === @mkdir(\dirname($path), 0777, true) && !is_dir(\dirname($path))) { |
385 return false; | 385 return false; |
386 } | 386 } |
387 | 387 |
388 $tmpFile = tempnam(dirname($path), basename($path)); | 388 $tmpFile = tempnam(\dirname($path), basename($path)); |
389 if (false === $fp = @fopen($tmpFile, 'wb')) { | 389 if (false === $fp = @fopen($tmpFile, 'wb')) { |
390 @unlink($tmpFile); | 390 @unlink($tmpFile); |
391 | 391 |
392 return false; | 392 return false; |
393 } | 393 } |
410 @chmod($path, 0666 & ~umask()); | 410 @chmod($path, 0666 & ~umask()); |
411 } | 411 } |
412 | 412 |
413 public function getPath($key) | 413 public function getPath($key) |
414 { | 414 { |
415 return $this->root.DIRECTORY_SEPARATOR.substr($key, 0, 2).DIRECTORY_SEPARATOR.substr($key, 2, 2).DIRECTORY_SEPARATOR.substr($key, 4, 2).DIRECTORY_SEPARATOR.substr($key, 6); | 415 return $this->root.\DIRECTORY_SEPARATOR.substr($key, 0, 2).\DIRECTORY_SEPARATOR.substr($key, 2, 2).\DIRECTORY_SEPARATOR.substr($key, 4, 2).\DIRECTORY_SEPARATOR.substr($key, 6); |
416 } | 416 } |
417 | 417 |
418 /** | 418 /** |
419 * Generates a cache key for the given Request. | 419 * Generates a cache key for the given Request. |
420 * | 420 * |
462 * @return array An array of HTTP headers | 462 * @return array An array of HTTP headers |
463 */ | 463 */ |
464 private function persistResponse(Response $response) | 464 private function persistResponse(Response $response) |
465 { | 465 { |
466 $headers = $response->headers->all(); | 466 $headers = $response->headers->all(); |
467 $headers['X-Status'] = array($response->getStatusCode()); | 467 $headers['X-Status'] = [$response->getStatusCode()]; |
468 | 468 |
469 return $headers; | 469 return $headers; |
470 } | 470 } |
471 | 471 |
472 /** | 472 /** |
481 { | 481 { |
482 $status = $headers['X-Status'][0]; | 482 $status = $headers['X-Status'][0]; |
483 unset($headers['X-Status']); | 483 unset($headers['X-Status']); |
484 | 484 |
485 if (null !== $body) { | 485 if (null !== $body) { |
486 $headers['X-Body-File'] = array($body); | 486 $headers['X-Body-File'] = [$body]; |
487 } | 487 } |
488 | 488 |
489 return new Response($body, $status, $headers); | 489 return new Response($body, $status, $headers); |
490 } | 490 } |
491 } | 491 } |