Annotations
3 years ago
EntityTraits
2 years ago
EventListeners
2 months ago
Middlewares
4 weeks ago
Types
2 months ago
Validator
2 months ago
WPDB
2 months ago
ArrayCache.php
2 months ago
CacheOnlyMappingDriver.php
3 years ago
ConfigurationFactory.php
3 years ago
ConnectionFactory.php
1 year ago
EntityManagerFactory.php
2 years ago
MetadataCache.php
2 months ago
PSRArrayCache.php
2 months ago
PSRCacheInvalidArgumentException.php
3 years ago
PSRCacheItem.php
2 months ago
PSRMetadataCache.php
3 years ago
ProxyClassNameResolver.php
3 years ago
Repository.php
8 months ago
TablePrefixMetadataFactory.php
6 months ago
index.php
3 years ago
MetadataCache.php
94 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Doctrine; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoetVendor\Doctrine\Common\Cache\CacheProvider; |
| 9 | use ReflectionClass; |
| 10 | |
| 11 | // Simple filesystem-based cache storage for Doctrine Metadata. |
| 12 | // |
| 13 | // Needed because Doctrine's FilesystemCache doesn't work read-only (when metadata dumped) |
| 14 | // and it calls realpath() that could fail on some hostings due to filesystem permissions. |
| 15 | class MetadataCache extends CacheProvider { |
| 16 | /** @var bool */ |
| 17 | private $isDevMode; |
| 18 | |
| 19 | /** @var string */ |
| 20 | private $directory; |
| 21 | |
| 22 | public function __construct( |
| 23 | $dir, |
| 24 | $isReadOnly |
| 25 | ) { |
| 26 | $this->isDevMode = defined('WP_DEBUG') && WP_DEBUG && !$isReadOnly; |
| 27 | $this->directory = rtrim($dir, '/\\'); |
| 28 | if (!file_exists($this->directory)) { |
| 29 | mkdir($this->directory); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | protected function doFetch($id) { |
| 34 | if (!$this->doContains($id)) { |
| 35 | return false; |
| 36 | } |
| 37 | return unserialize((string)file_get_contents($this->getFilename($id))); |
| 38 | } |
| 39 | |
| 40 | protected function doContains($id) { |
| 41 | $filename = $this->getFilename($id); |
| 42 | $fileExists = file_exists($filename); |
| 43 | |
| 44 | // in dev mode invalidate cache if source file has changed |
| 45 | if ($fileExists && $this->isDevMode) { |
| 46 | /** @var \stdClass $classMetadata */ |
| 47 | $classMetadata = unserialize((string)file_get_contents($filename)); |
| 48 | if (!isset($classMetadata->name) || (!class_exists($classMetadata->name) && !interface_exists($classMetadata->name))) { |
| 49 | return false; |
| 50 | } |
| 51 | $reflection = new ReflectionClass($classMetadata->name); |
| 52 | clearstatcache(); |
| 53 | return filemtime((string)$filename) >= filemtime((string)$reflection->getFileName()); |
| 54 | } |
| 55 | |
| 56 | return $fileExists; |
| 57 | } |
| 58 | |
| 59 | protected function doSave($id, $data, $lifeTime = 0) { |
| 60 | $filename = $this->getFilename($id); |
| 61 | $result = @file_put_contents($filename, serialize($data)); |
| 62 | if ($result === false) { |
| 63 | throw new \RuntimeException("Error while writing to '$filename'"); |
| 64 | } |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | protected function doDelete($id) { |
| 69 | @unlink($this->getFilename($id)); |
| 70 | return true; |
| 71 | } |
| 72 | |
| 73 | protected function doFlush() { |
| 74 | $directoryContent = glob($this->directory . DIRECTORY_SEPARATOR . '*'); |
| 75 | if ($directoryContent === false) { |
| 76 | return false; |
| 77 | } |
| 78 | foreach ($directoryContent as $filename) { |
| 79 | if (is_file($filename)) { |
| 80 | @unlink($filename); |
| 81 | } |
| 82 | } |
| 83 | return true; |
| 84 | } |
| 85 | |
| 86 | protected function doGetStats() { |
| 87 | return null; |
| 88 | } |
| 89 | |
| 90 | private function getFilename($id) { |
| 91 | return $this->directory . DIRECTORY_SEPARATOR . md5($id); |
| 92 | } |
| 93 | } |
| 94 |