mirror of
https://github.com/flextype/flextype.git
synced 2025-08-08 06:06:45 +02:00
feat(cache): add PHP Array cache driver #199
This commit is contained in:
111
src/flextype/Foundation/Cache/Drivers/Phparray/Config.php
Normal file
111
src/flextype/Foundation/Cache/Drivers/Phparray/Config.php
Normal file
@@ -0,0 +1,111 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Flextype (https://flextype.org)
|
||||
* Founded by Sergey Romanenko and maintained by Flextype Community.
|
||||
*/
|
||||
|
||||
namespace Phpfastcache\Drivers\Phparray;
|
||||
|
||||
use Phpfastcache\Config\ConfigurationOption;
|
||||
|
||||
class Config extends ConfigurationOption
|
||||
{
|
||||
/**
|
||||
* @var boolean
|
||||
*/
|
||||
protected $secureFileManipulation = false;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $htaccess = true;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $securityKey = '';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $cacheFileExtension = 'txt';
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getSecurityKey(): string
|
||||
{
|
||||
return $this->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;
|
||||
}
|
||||
}
|
143
src/flextype/Foundation/Cache/Drivers/Phparray/Driver.php
Normal file
143
src/flextype/Foundation/Cache/Drivers/Phparray/Driver.php
Normal file
@@ -0,0 +1,143 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Flextype (https://flextype.org)
|
||||
* Founded by Sergey Romanenko and maintained by Flextype Community.
|
||||
*/
|
||||
|
||||
namespace Phpfastcache\Drivers\Phparray;
|
||||
|
||||
use Exception;
|
||||
use FilesystemIterator;
|
||||
use Phpfastcache\Cluster\AggregatablePoolInterface;
|
||||
use Phpfastcache\Core\Pool\{DriverBaseTrait, ExtendedCacheItemPoolInterface, IO\IOHelperTrait};
|
||||
use Phpfastcache\Exceptions\{PhpfastcacheInvalidArgumentException, PhpfastcacheIOException};
|
||||
use Phpfastcache\Util\Directory;
|
||||
use Psr\Cache\CacheItemInterface;
|
||||
|
||||
|
||||
/**
|
||||
* Class Driver
|
||||
* @package phpFastCache\Drivers
|
||||
* @property Config $config Config object
|
||||
* @method Config getConfig() Return the config object
|
||||
*
|
||||
* Important NOTE:
|
||||
* We are using getKey instead of getEncodedKey since this backend create filename that are
|
||||
* managed by defaultFileNameHashFunction and not defaultKeyHashFunction
|
||||
*/
|
||||
class Driver implements ExtendedCacheItemPoolInterface, AggregatablePoolInterface
|
||||
{
|
||||
use IOHelperTrait;
|
||||
use DriverBaseTrait {
|
||||
DriverBaseTrait::__construct as private __parentConstruct;
|
||||
}
|
||||
|
||||
/**
|
||||
* Driver constructor.
|
||||
* @param Config $config
|
||||
* @param string $instanceId
|
||||
*/
|
||||
public function __construct(Config $config, string $instanceId)
|
||||
{
|
||||
$this->__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, "<?php\n" . "return " . var_export($data, true) . ";\n", $this->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));
|
||||
}
|
||||
}
|
53
src/flextype/Foundation/Cache/Drivers/Phparray/Item.php
Normal file
53
src/flextype/Foundation/Cache/Drivers/Phparray/Item.php
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Flextype (https://flextype.org)
|
||||
* Founded by Sergey Romanenko and maintained by Flextype Community.
|
||||
*/
|
||||
|
||||
namespace Phpfastcache\Drivers\Phparray;
|
||||
|
||||
use Phpfastcache\Core\Item\{ExtendedCacheItemInterface, ItemBaseTrait};
|
||||
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface;
|
||||
use Phpfastcache\Drivers\Phparray\Driver as PhparrayDriver;
|
||||
use Phpfastcache\Exceptions\{PhpfastcacheInvalidArgumentException};
|
||||
|
||||
/**
|
||||
* Class Item
|
||||
* @package phpFastCache\Drivers\Phparray
|
||||
*/
|
||||
class Item implements ExtendedCacheItemInterface
|
||||
{
|
||||
use ItemBaseTrait {
|
||||
ItemBaseTrait::__construct as __BaseConstruct;
|
||||
}
|
||||
|
||||
/**
|
||||
* Item constructor.
|
||||
* @param Driver $driver
|
||||
* @param $key
|
||||
* @throws PhpfastcacheInvalidArgumentException
|
||||
*/
|
||||
public function __construct(PhparrayDriver $driver, $key)
|
||||
{
|
||||
$this->__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');
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user