From bf6655a534a6775d30cafa67bd801276bda1d98d Mon Sep 17 00:00:00 2001 From: polo Date: Tue, 13 Aug 2024 23:45:21 +0200 Subject: =?UTF-8?q?VERSION=200.2=20doctrine=20ORM=20et=20entit=C3=A9s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../orm/src/Cache/Region/FileLockRegion.php | 194 +++++++++++++++++++++ 1 file changed, 194 insertions(+) create mode 100644 vendor/doctrine/orm/src/Cache/Region/FileLockRegion.php (limited to 'vendor/doctrine/orm/src/Cache/Region/FileLockRegion.php') diff --git a/vendor/doctrine/orm/src/Cache/Region/FileLockRegion.php b/vendor/doctrine/orm/src/Cache/Region/FileLockRegion.php new file mode 100644 index 0000000..bedd6a6 --- /dev/null +++ b/vendor/doctrine/orm/src/Cache/Region/FileLockRegion.php @@ -0,0 +1,194 @@ +getLockFileName($key); + + if (! is_file($filename)) { + return false; + } + + $time = $this->getLockTime($filename); + $content = $this->getLockContent($filename); + + if ($content === false || $time === false) { + @unlink($filename); + + return false; + } + + if ($lock && $content === $lock->value) { + return false; + } + + // outdated lock + if ($time + $this->lockLifetime <= time()) { + @unlink($filename); + + return false; + } + + return true; + } + + private function getLockFileName(CacheKey $key): string + { + return $this->directory . DIRECTORY_SEPARATOR . $key->hash . '.' . self::LOCK_EXTENSION; + } + + private function getLockContent(string $filename): string|false + { + return @file_get_contents($filename); + } + + private function getLockTime(string $filename): int|false + { + return @fileatime($filename); + } + + public function getName(): string + { + return $this->region->getName(); + } + + public function contains(CacheKey $key): bool + { + if ($this->isLocked($key)) { + return false; + } + + return $this->region->contains($key); + } + + public function get(CacheKey $key): CacheEntry|null + { + if ($this->isLocked($key)) { + return null; + } + + return $this->region->get($key); + } + + public function getMultiple(CollectionCacheEntry $collection): array|null + { + if (array_filter(array_map($this->isLocked(...), $collection->identifiers))) { + return null; + } + + return $this->region->getMultiple($collection); + } + + public function put(CacheKey $key, CacheEntry $entry, Lock|null $lock = null): bool + { + if ($this->isLocked($key, $lock)) { + return false; + } + + return $this->region->put($key, $entry); + } + + public function evict(CacheKey $key): bool + { + if ($this->isLocked($key)) { + @unlink($this->getLockFileName($key)); + } + + return $this->region->evict($key); + } + + public function evictAll(): bool + { + // The check below is necessary because on some platforms glob returns false + // when nothing matched (even though no errors occurred) + $filenames = glob(sprintf('%s/*.%s', $this->directory, self::LOCK_EXTENSION)) ?: []; + + foreach ($filenames as $filename) { + @unlink($filename); + } + + return $this->region->evictAll(); + } + + public function lock(CacheKey $key): Lock|null + { + if ($this->isLocked($key)) { + return null; + } + + $lock = Lock::createLockRead(); + $filename = $this->getLockFileName($key); + + if (@file_put_contents($filename, $lock->value, LOCK_EX) === false) { + return null; + } + + chmod($filename, 0664); + + return $lock; + } + + public function unlock(CacheKey $key, Lock $lock): bool + { + if ($this->isLocked($key, $lock)) { + return false; + } + + return @unlink($this->getLockFileName($key)); + } +} -- cgit v1.2.3