Нет описания

MetadataCache.php 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace MailPoet\Doctrine;
  3. if (!defined('ABSPATH')) exit;
  4. use MailPoetVendor\Doctrine\Common\Cache\CacheProvider;
  5. use MailPoetVendor\Doctrine\ORM\Mapping\ClassMetadata as DoctrineClassMetadata;
  6. use MailPoetVendor\Symfony\Component\Validator\Mapping\ClassMetadata as ValidatorClassMetadata;
  7. use ReflectionClass;
  8. use ReflectionException;
  9. // Simple filesystem-based cache storage for Doctrine Metadata.
  10. //
  11. // Needed because Doctrine's FilesystemCache doesn't work read-only (when metadata dumped)
  12. // and it calls realpath() that could fail on some hostings due to filesystem permissions.
  13. class MetadataCache extends CacheProvider {
  14. /** @var bool */
  15. private $isDevMode;
  16. /** @var string */
  17. private $directory;
  18. public function __construct($dir, $isReadOnly) {
  19. $this->isDevMode = defined('WP_DEBUG') && WP_DEBUG && !$isReadOnly;
  20. $this->directory = rtrim($dir, '/\\');
  21. if (!file_exists($this->directory)) {
  22. mkdir($this->directory);
  23. }
  24. }
  25. protected function doFetch($id) {
  26. if (!$this->doContains($id)) {
  27. return false;
  28. }
  29. return unserialize((string)file_get_contents($this->getFilename($id)));
  30. }
  31. protected function doContains($id) {
  32. $filename = $this->getFilename($id);
  33. $fileExists = file_exists($filename);
  34. // in dev mode invalidate cache if source file has changed
  35. if ($fileExists && $this->isDevMode) {
  36. $classMetadata = unserialize((string)file_get_contents($filename));
  37. assert($classMetadata instanceof DoctrineClassMetadata || $classMetadata instanceof ValidatorClassMetadata);
  38. if (!class_exists($classMetadata->name) && !interface_exists($classMetadata->name)) {
  39. return false;
  40. }
  41. try {
  42. $reflection = new ReflectionClass($classMetadata->name);
  43. } catch (ReflectionException $e) {
  44. return false;
  45. }
  46. clearstatcache();
  47. return filemtime((string)$filename) >= filemtime((string)$reflection->getFileName());
  48. }
  49. return $fileExists;
  50. }
  51. protected function doSave($id, $data, $lifeTime = 0) {
  52. $filename = $this->getFilename($id);
  53. $result = @file_put_contents($filename, serialize($data));
  54. if ($result === false) {
  55. throw new \RuntimeException("Error while writing to '$filename'");
  56. }
  57. return true;
  58. }
  59. protected function doDelete($id) {
  60. @unlink($this->getFilename($id));
  61. return true;
  62. }
  63. protected function doFlush() {
  64. $directoryContent = glob($this->directory . DIRECTORY_SEPARATOR . '*');
  65. if ($directoryContent === false) {
  66. return false;
  67. }
  68. foreach ($directoryContent as $filename) {
  69. if (is_file($filename)) {
  70. @unlink($filename);
  71. }
  72. }
  73. return true;
  74. }
  75. protected function doGetStats() {
  76. return null;
  77. }
  78. private function getFilename($id) {
  79. return $this->directory . DIRECTORY_SEPARATOR . md5($id);
  80. }
  81. }