Skip to content

Commit

Permalink
feat(cache): add PHP Array cache driver #199
Browse files Browse the repository at this point in the history
  • Loading branch information
Awilum committed Jul 29, 2021
1 parent a712499 commit bcf9afd
Show file tree
Hide file tree
Showing 3 changed files with 307 additions and 0 deletions.
111 changes: 111 additions & 0 deletions src/flextype/Foundation/Cache/Drivers/Phparray/Config.php
@@ -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 changes: 143 additions & 0 deletions src/flextype/Foundation/Cache/Drivers/Phparray/Driver.php
@@ -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 changes: 53 additions & 0 deletions src/flextype/Foundation/Cache/Drivers/Phparray/Item.php
@@ -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');
}
}

0 comments on commit bcf9afd

Please sign in to comment.