Refactor shared implementation methods into a trait, check keys in pool
Some checks failed
Gitea - aviat/banker/pipeline/head There was a failure building this commit
Some checks failed
Gitea - aviat/banker/pipeline/head There was a failure building this commit
This commit is contained in:
parent
f939e46c85
commit
dc53bf5ccc
@ -9,7 +9,7 @@
|
|||||||
<bootstrap />
|
<bootstrap />
|
||||||
|
|
||||||
<!-- A phpDox project to process, you can have multiple projects in one config file -->
|
<!-- A phpDox project to process, you can have multiple projects in one config file -->
|
||||||
<project name="Cache" source="../src" workdir="phpdox/xml">
|
<project name="Banker" source="../src" workdir="phpdox/xml">
|
||||||
<!-- @name - The name of the project -->
|
<!-- @name - The name of the project -->
|
||||||
<!-- @source - The source directory of the application to process -->
|
<!-- @source - The source directory of the application to process -->
|
||||||
<!-- @workdir - The directory to store the xml data files in -->
|
<!-- @workdir - The directory to store the xml data files in -->
|
||||||
@ -35,7 +35,7 @@
|
|||||||
-->
|
-->
|
||||||
|
|
||||||
<!-- Additional configuration for the collecting process (parsing of php code, generation of xml data) -->
|
<!-- Additional configuration for the collecting process (parsing of php code, generation of xml data) -->
|
||||||
<collector publiconly="false" backend="parser" encoding="auto">
|
<collector publiconly="true" backend="parser" encoding="auto">
|
||||||
<!-- @publiconly - Flag to disable/enable processing of non public methods and members -->
|
<!-- @publiconly - Flag to disable/enable processing of non public methods and members -->
|
||||||
<!-- @backend - The collector backend to use, currently only shipping with 'parser' -->
|
<!-- @backend - The collector backend to use, currently only shipping with 'parser' -->
|
||||||
<!-- @encoding - Charset encoding of source files (overwrite default 'auto' if detection fails) -->
|
<!-- @encoding - Charset encoding of source files (overwrite default 'auto' if detection fails) -->
|
||||||
@ -78,15 +78,15 @@
|
|||||||
</source>
|
</source>
|
||||||
|
|
||||||
<!-- git vcs information -->
|
<!-- git vcs information -->
|
||||||
<source type="git">
|
<!-- <source type="git">
|
||||||
<git binary="/usr/bin/git" />
|
<git binary="/usr/bin/git" />
|
||||||
<history enabled="true" limit="15" cache="${phpDox.project.workdir}/gitlog.xml" />
|
<history enabled="true" limit="15" cache="${phpDox.project.workdir}/gitlog.xml" />
|
||||||
</source>
|
</source> -->
|
||||||
|
|
||||||
<!-- PHP Code Sniffer findings -->
|
<!-- PHP Code Sniffer findings -->
|
||||||
<source type="checkstyle">
|
<!-- <source type="checkstyle">
|
||||||
<file name="phpcs.xml" />
|
<file name="phpcs.xml" />
|
||||||
</source>
|
</source> -->
|
||||||
|
|
||||||
<!-- PHPMessDetector -->
|
<!-- PHPMessDetector -->
|
||||||
<!--
|
<!--
|
||||||
@ -96,16 +96,9 @@
|
|||||||
-->
|
-->
|
||||||
|
|
||||||
<!-- PHPUnit Coverage XML -->
|
<!-- PHPUnit Coverage XML -->
|
||||||
<source type="phpunit">
|
<!-- <source type="phpunit">
|
||||||
<coverage path="coverage/clover.xml" />
|
|
||||||
<!-- <coverage path="clover.xml" />-->
|
|
||||||
<!-- @path - the directory where the xml code coverage report can be found -->
|
|
||||||
<!--<filter directory="${phpDox.project.source}" />-->
|
|
||||||
<!-- @directory - path of the phpunit config whitelist filter directory -->
|
|
||||||
</source>
|
|
||||||
<source type="phpunit">
|
|
||||||
<filter directory="${phpDox.project.source}" />
|
<filter directory="${phpDox.project.source}" />
|
||||||
</source>
|
</source> -->
|
||||||
|
|
||||||
</enrich>
|
</enrich>
|
||||||
|
|
||||||
|
52
src/Pool.php
52
src/Pool.php
@ -15,7 +15,6 @@
|
|||||||
*/
|
*/
|
||||||
namespace Aviat\Banker;
|
namespace Aviat\Banker;
|
||||||
|
|
||||||
use Aviat\Banker\Driver\DriverInterface;
|
|
||||||
use Aviat\Banker\Exception\InvalidArgumentException;
|
use Aviat\Banker\Exception\InvalidArgumentException;
|
||||||
use Psr\Cache\{CacheItemInterface, CacheItemPoolInterface};
|
use Psr\Cache\{CacheItemInterface, CacheItemPoolInterface};
|
||||||
use Psr\Log\{LoggerAwareInterface, LoggerInterface};
|
use Psr\Log\{LoggerAwareInterface, LoggerInterface};
|
||||||
@ -26,16 +25,9 @@ use function is_string;
|
|||||||
* The main cache manager
|
* The main cache manager
|
||||||
*/
|
*/
|
||||||
final class Pool implements CacheItemPoolInterface, LoggerAwareInterface {
|
final class Pool implements CacheItemPoolInterface, LoggerAwareInterface {
|
||||||
|
use _Driver;
|
||||||
use LoggerTrait;
|
use LoggerTrait;
|
||||||
|
|
||||||
/**
|
|
||||||
* Driver class for handling the chosen caching backend
|
|
||||||
*
|
|
||||||
* @var DriverInterface
|
|
||||||
*/
|
|
||||||
protected DriverInterface $driver;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Cache Items to be saved
|
* Cache Items to be saved
|
||||||
*
|
*
|
||||||
@ -77,10 +69,7 @@ final class Pool implements CacheItemPoolInterface, LoggerAwareInterface {
|
|||||||
*/
|
*/
|
||||||
public function getItem($key): CacheItemInterface
|
public function getItem($key): CacheItemInterface
|
||||||
{
|
{
|
||||||
if ( ! is_string($key))
|
$this->validateKey($key);
|
||||||
{
|
|
||||||
throw new InvalidArgumentException();
|
|
||||||
}
|
|
||||||
|
|
||||||
// If a deferred item exists, return that
|
// If a deferred item exists, return that
|
||||||
if (array_key_exists($key, $this->deferred))
|
if (array_key_exists($key, $this->deferred))
|
||||||
@ -109,6 +98,8 @@ final class Pool implements CacheItemPoolInterface, LoggerAwareInterface {
|
|||||||
*/
|
*/
|
||||||
public function getItems(array $keys = [])
|
public function getItems(array $keys = [])
|
||||||
{
|
{
|
||||||
|
$this->validateKeys($keys);
|
||||||
|
|
||||||
if (empty($keys))
|
if (empty($keys))
|
||||||
{
|
{
|
||||||
return new ItemCollection([]);
|
return new ItemCollection([]);
|
||||||
@ -150,10 +141,7 @@ final class Pool implements CacheItemPoolInterface, LoggerAwareInterface {
|
|||||||
*/
|
*/
|
||||||
public function hasItem($key): bool
|
public function hasItem($key): bool
|
||||||
{
|
{
|
||||||
if ( ! is_string($key))
|
$this->validateKey($key);
|
||||||
{
|
|
||||||
throw new InvalidArgumentException();
|
|
||||||
}
|
|
||||||
|
|
||||||
// See if there are any deferred items
|
// See if there are any deferred items
|
||||||
if (array_key_exists($key, $this->deferred))
|
if (array_key_exists($key, $this->deferred))
|
||||||
@ -190,10 +178,7 @@ final class Pool implements CacheItemPoolInterface, LoggerAwareInterface {
|
|||||||
*/
|
*/
|
||||||
public function deleteItem($key): bool
|
public function deleteItem($key): bool
|
||||||
{
|
{
|
||||||
if ( ! is_string($key))
|
$this->validateKey($key);
|
||||||
{
|
|
||||||
throw new InvalidArgumentException();
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( ! $this->hasItem($key))
|
if ( ! $this->hasItem($key))
|
||||||
{
|
{
|
||||||
@ -218,13 +203,7 @@ final class Pool implements CacheItemPoolInterface, LoggerAwareInterface {
|
|||||||
*/
|
*/
|
||||||
public function deleteItems(array $keys): bool
|
public function deleteItems(array $keys): bool
|
||||||
{
|
{
|
||||||
foreach ($keys as $key)
|
$this->validateKeys($keys);
|
||||||
{
|
|
||||||
if ( ! is_string($key))
|
|
||||||
{
|
|
||||||
throw new InvalidArgumentException();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this->driver->deleteMultiple($keys);
|
return $this->driver->deleteMultiple($keys);
|
||||||
}
|
}
|
||||||
@ -287,21 +266,4 @@ final class Pool implements CacheItemPoolInterface, LoggerAwareInterface {
|
|||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Instantiate the appropriate cache backend based on the config
|
|
||||||
*
|
|
||||||
* @param array $driverConfig
|
|
||||||
* @return DriverInterface
|
|
||||||
*/
|
|
||||||
protected function loadDriver(array $driverConfig = []): DriverInterface
|
|
||||||
{
|
|
||||||
$driver = ucfirst(strtolower($driverConfig['driver'] ?? 'null'));
|
|
||||||
$class = __NAMESPACE__ . "\\Driver\\${driver}Driver";
|
|
||||||
|
|
||||||
$driverConfig['connection'] = $driverConfig['connection'] ?? [];
|
|
||||||
$driverConfig['options'] = $driverConfig['options'] ?? [];
|
|
||||||
|
|
||||||
return new $class($driverConfig['connection'], $driverConfig['options']);
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -15,17 +15,17 @@
|
|||||||
*/
|
*/
|
||||||
namespace Aviat\Banker;
|
namespace Aviat\Banker;
|
||||||
|
|
||||||
use Aviat\Banker\Driver\DriverInterface;
|
|
||||||
use Aviat\Banker\Exception\InvalidArgumentException;
|
|
||||||
use Psr\Log\LoggerAwareInterface;
|
use Psr\Log\LoggerAwareInterface;
|
||||||
use Psr\SimpleCache;
|
use Psr\SimpleCache;
|
||||||
use Psr\Log\LoggerInterface;
|
use Psr\Log\LoggerInterface;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implements PSR-16 (SimpleCache)
|
||||||
|
*/
|
||||||
class Teller implements SimpleCache\CacheInterface, LoggerAwareInterface {
|
class Teller implements SimpleCache\CacheInterface, LoggerAwareInterface {
|
||||||
|
use _Driver;
|
||||||
use LoggerTrait;
|
use LoggerTrait;
|
||||||
|
|
||||||
private DriverInterface $driver;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set up the cache backend
|
* Set up the cache backend
|
||||||
*
|
*
|
||||||
@ -194,57 +194,4 @@ class Teller implements SimpleCache\CacheInterface, LoggerAwareInterface {
|
|||||||
|
|
||||||
return $this->driver->exists($key);
|
return $this->driver->exists($key);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $keys
|
|
||||||
* @param bool $hash
|
|
||||||
* @throws InvalidArgumentException
|
|
||||||
*/
|
|
||||||
private function validateKeys($keys, bool $hash = FALSE): void
|
|
||||||
{
|
|
||||||
// Check type of keys
|
|
||||||
if ( ! is_iterable($keys))
|
|
||||||
{
|
|
||||||
throw new InvalidArgumentException('Keys must be an array or a traversable object');
|
|
||||||
}
|
|
||||||
|
|
||||||
$keys = ($hash) ? array_keys((array)$keys) : (array)$keys;
|
|
||||||
|
|
||||||
// Check each key
|
|
||||||
array_walk($keys, fn($key) => $this->validateKey($key));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $key
|
|
||||||
* @throws InvalidArgumentException
|
|
||||||
*/
|
|
||||||
private function validateKey($key): void
|
|
||||||
{
|
|
||||||
if ( ! is_string($key))
|
|
||||||
{
|
|
||||||
throw new InvalidArgumentException('Cache key must be a string.');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (is_string($key) && preg_match("`[{}()/@:\\\]`", $key) === 1)
|
|
||||||
{
|
|
||||||
throw new InvalidArgumentException('Invalid characters in cache key');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Instantiate the appropriate cache backend based on the config
|
|
||||||
*
|
|
||||||
* @param array $driverConfig
|
|
||||||
* @return DriverInterface
|
|
||||||
*/
|
|
||||||
protected function loadDriver(array $driverConfig = []): DriverInterface
|
|
||||||
{
|
|
||||||
$driver = ucfirst(strtolower($driverConfig['driver'] ?? 'null'));
|
|
||||||
$class = __NAMESPACE__ . "\\Driver\\${driver}Driver";
|
|
||||||
|
|
||||||
$driverConfig['connection'] = $driverConfig['connection'] ?? [];
|
|
||||||
$driverConfig['options'] = $driverConfig['options'] ?? [];
|
|
||||||
|
|
||||||
return new $class($driverConfig['connection'], $driverConfig['options']);
|
|
||||||
}
|
|
||||||
}
|
}
|
84
src/_Driver.php
Normal file
84
src/_Driver.php
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
<?php declare(strict_types=1);
|
||||||
|
/**
|
||||||
|
* Banker
|
||||||
|
*
|
||||||
|
* A Caching library implementing psr/cache (PSR 6) and psr/simple-cache (PSR 16)
|
||||||
|
*
|
||||||
|
* PHP version 7.4
|
||||||
|
*
|
||||||
|
* @package Banker
|
||||||
|
* @author Timothy J. Warren <tim@timshomepage.net>
|
||||||
|
* @copyright 2016 - 2020 Timothy J. Warren
|
||||||
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
||||||
|
* @version 3.0.0
|
||||||
|
* @link https://git.timshomepage.net/timw4mail/banker
|
||||||
|
*/
|
||||||
|
namespace Aviat\Banker;
|
||||||
|
|
||||||
|
use Aviat\Banker\Driver\DriverInterface;
|
||||||
|
use Aviat\Banker\Exception\InvalidArgumentException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Private trait for shared driver-related functionality
|
||||||
|
*/
|
||||||
|
trait _Driver {
|
||||||
|
/**
|
||||||
|
* Driver class for handling the chosen caching backend
|
||||||
|
*
|
||||||
|
* @var DriverInterface
|
||||||
|
*/
|
||||||
|
private DriverInterface $driver;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Instantiate the appropriate cache backend based on the config
|
||||||
|
*
|
||||||
|
* @param array $driverConfig
|
||||||
|
* @return DriverInterface
|
||||||
|
*/
|
||||||
|
protected function loadDriver(array $driverConfig = []): DriverInterface
|
||||||
|
{
|
||||||
|
$driver = ucfirst(strtolower($driverConfig['driver'] ?? 'null'));
|
||||||
|
$class = __NAMESPACE__ . "\\Driver\\${driver}Driver";
|
||||||
|
|
||||||
|
$driverConfig['connection'] = $driverConfig['connection'] ?? [];
|
||||||
|
$driverConfig['options'] = $driverConfig['options'] ?? [];
|
||||||
|
|
||||||
|
return new $class($driverConfig['connection'], $driverConfig['options']);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $keys
|
||||||
|
* @param bool $hash
|
||||||
|
* @throws InvalidArgumentException
|
||||||
|
*/
|
||||||
|
private function validateKeys($keys, bool $hash = FALSE): void
|
||||||
|
{
|
||||||
|
// Check type of keys
|
||||||
|
if ( ! is_iterable($keys))
|
||||||
|
{
|
||||||
|
throw new InvalidArgumentException('Keys must be an array or a traversable object');
|
||||||
|
}
|
||||||
|
|
||||||
|
$keys = ($hash) ? array_keys((array)$keys) : (array)$keys;
|
||||||
|
|
||||||
|
// Check each key
|
||||||
|
array_walk($keys, fn($key) => $this->validateKey($key));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $key
|
||||||
|
* @throws InvalidArgumentException
|
||||||
|
*/
|
||||||
|
private function validateKey($key): void
|
||||||
|
{
|
||||||
|
if ( ! is_string($key))
|
||||||
|
{
|
||||||
|
throw new InvalidArgumentException('Cache key must be a string.');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_string($key) && preg_match("`[{}()/@:\\\]`", $key) === 1)
|
||||||
|
{
|
||||||
|
throw new InvalidArgumentException('Invalid characters in cache key');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user