From bcf9afde0186f64adc1d727618b4624732aebd27 Mon Sep 17 00:00:00 2001 From: Awilum Date: Thu, 29 Jul 2021 15:43:41 +0300 Subject: [PATCH] feat(cache): add PHP Array cache driver #199 --- .../Cache/Drivers/Phparray/Config.php | 111 ++++++++++++++ .../Cache/Drivers/Phparray/Driver.php | 143 ++++++++++++++++++ .../Cache/Drivers/Phparray/Item.php | 53 +++++++ 3 files changed, 307 insertions(+) create mode 100644 src/flextype/Foundation/Cache/Drivers/Phparray/Config.php create mode 100644 src/flextype/Foundation/Cache/Drivers/Phparray/Driver.php create mode 100644 src/flextype/Foundation/Cache/Drivers/Phparray/Item.php diff --git a/src/flextype/Foundation/Cache/Drivers/Phparray/Config.php b/src/flextype/Foundation/Cache/Drivers/Phparray/Config.php new file mode 100644 index 000000000..73014dfbf --- /dev/null +++ b/src/flextype/Foundation/Cache/Drivers/Phparray/Config.php @@ -0,0 +1,111 @@ +securityKey; + } + + /** + * @param string $securityKey + * @return Config + */ + public function setSecurityKey(string $securityKey): self + { + $this->securityKey = $securityKey; + + return $this; + } + + /** + * @return bool + */ + public function getHtaccess(): bool + { + return $this->htaccess; + } + + /** + * @param bool $htaccess + * @return Config + */ + public function setHtaccess(bool $htaccess): self + { + $this->htaccess = $htaccess; + + return $this; + } + + /** + * @return bool + */ + public function isSecureFileManipulation(): bool + { + return $this->secureFileManipulation; + } + + /** + * @param bool $secureFileManipulation + * @return self + */ + public function setSecureFileManipulation(bool $secureFileManipulation): self + { + $this->secureFileManipulation = $secureFileManipulation; + return $this; + } + + + /** + * @return string + */ + public function getCacheFileExtension(): string + { + return $this->cacheFileExtension; + } + + /** + * @param string $cacheFileExtension + * @return self + * @throws PhpfastcacheInvalidConfigurationException + */ + public function setCacheFileExtension(string $cacheFileExtension): self + { + $this->cacheFileExtension = 'php'; + return $this; + } +} \ No newline at end of file diff --git a/src/flextype/Foundation/Cache/Drivers/Phparray/Driver.php b/src/flextype/Foundation/Cache/Drivers/Phparray/Driver.php new file mode 100644 index 000000000..6e5989337 --- /dev/null +++ b/src/flextype/Foundation/Cache/Drivers/Phparray/Driver.php @@ -0,0 +1,143 @@ +__parentConstruct($config, $instanceId); + } + + /** + * @return bool + */ + public function driverCheck(): bool + { + return is_writable($this->getPath()) || mkdir($concurrentDirectory = $this->getPath(), $this->getDefaultChmod(), true) || is_dir($concurrentDirectory); + } + + /** + * @return bool + */ + protected function driverConnect(): bool + { + return true; + } + + /** + * @param CacheItemInterface $item + * @return null|array + */ + protected function driverRead(CacheItemInterface $item) + { + $file_path = $this->getFilePath($item->getKey(), true); + + try{ + $content = include $file_path; + }catch (PhpfastcacheIOException $e){ + return null; + } + + return $content; + } + + /** + * @param CacheItemInterface $item + * @return bool + * @throws PhpfastcacheInvalidArgumentException + */ + protected function driverWrite(CacheItemInterface $item): bool + { + /** + * Check for Cross-Driver type confusion + */ + if ($item instanceof Item) { + $file_path = $this->getFilePath($item->getKey()); + $data = $this->driverPreWrap($item); + + /** + * Force write + */ + try { + return $this->writefile($file_path, "getConfig()->isSecureFileManipulation()); + } catch (Exception $e) { + return false; + } + } + + throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); + } + + /** + * @param CacheItemInterface $item + * @return bool + * @throws PhpfastcacheInvalidArgumentException + */ + protected function driverDelete(CacheItemInterface $item): bool + { + /** + * Check for Cross-Driver type confusion + */ + if ($item instanceof Item) { + $file_path = $this->getFilePath($item->getKey(), true); + if (\file_exists($file_path) && @\unlink($file_path)) { + \clearstatcache(true, $file_path); + $dir = \dirname($file_path); + if (!(new FilesystemIterator($dir))->valid()) { + \rmdir($dir); + } + return true; + } + + return false; + } + + throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); + } + + /** + * @return bool + * @throws \Phpfastcache\Exceptions\PhpfastcacheIOException + */ + protected function driverClear(): bool + { + return Directory::rrmdir($this->getPath(true)); + } +} diff --git a/src/flextype/Foundation/Cache/Drivers/Phparray/Item.php b/src/flextype/Foundation/Cache/Drivers/Phparray/Item.php new file mode 100644 index 000000000..d2399727a --- /dev/null +++ b/src/flextype/Foundation/Cache/Drivers/Phparray/Item.php @@ -0,0 +1,53 @@ +__BaseConstruct($driver, $key); + } + + /** + * @param ExtendedCacheItemPoolInterface $driver + * @return static + * @throws PhpfastcacheInvalidArgumentException + */ + public function setDriver(ExtendedCacheItemPoolInterface $driver) + { + if ($driver instanceof PhparrayDriver) { + $this->driver = $driver; + + return $this; + } + + throw new PhpfastcacheInvalidArgumentException('Invalid driver instance'); + } +} \ No newline at end of file