Skip to content

Commit

Permalink
SimpleCache collector/decorator - support all versions of interface
Browse files Browse the repository at this point in the history
  • Loading branch information
bkdotcom committed Oct 11, 2024
1 parent 79b0472 commit 98bda7b
Show file tree
Hide file tree
Showing 10 changed files with 620 additions and 102 deletions.
124 changes: 37 additions & 87 deletions src/Debug/Collector/SimpleCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
use BadMethodCallException;
use bdk\Debug;
use bdk\Debug\Collector\SimpleCache\CallInfo;
use bdk\Debug\Collector\SimpleCache\CompatTrait;
use bdk\PubSub\Event;
use Exception;
use Psr\SimpleCache\CacheInterface;
use RuntimeException;
use Traversable;
Expand All @@ -25,6 +27,8 @@
*/
class SimpleCache implements CacheInterface
{
use CompatTrait;

/** @var Debug */
public $debug;

Expand Down Expand Up @@ -58,7 +62,7 @@ public function __construct(CacheInterface $cache, $debug = null)
}
$this->cache = $cache;
$this->debug = $debug;
$this->debug->eventManager->subscribe(Debug::EVENT_OUTPUT, array($this, 'onDebugOutput'), 1);
$this->debug->eventManager->subscribe(Debug::EVENT_OUTPUT, [$this, 'onDebugOutput'], 1);
}

/**
Expand All @@ -83,87 +87,10 @@ public function __call($method, $args)
return $this->profileCall($method, $args, false, $keys);
}

/**
* {@inheritDoc}
*/
public function get($key, $default = null)
{
return $this->profileCall('get', \func_get_args(), false, $key);
}

/**
* {@inheritDoc}
*/
public function set($key, $value, $ttl = null)
{
return $this->profileCall('set', \func_get_args(), true, $key);
}

/**
* {@inheritDoc}
*/
public function delete($key)
{
return $this->profileCall('delete', \func_get_args(), false, $key);
}

/**
* {@inheritDoc}
*/
public function clear()
{
return $this->profileCall('clear', array(), true);
}

/**
* {@inheritDoc}
*/
public function getMultiple($keys, $default = null)
{
$keysDebug = array();
if ($keys instanceof Traversable) {
$keysDebug = \iterator_to_array($keys, false);
} elseif (\is_array($keys)) {
$keysDebug = $keys;
}
return $this->profileCall('getMultiple', \func_get_args(), false, $keysDebug);
}

/**
* {@inheritDoc}
*/
public function setMultiple($values, $ttl = null)
{
$keysDebug = array();
if ($values instanceof Traversable) {
$keysDebug = \array_keys(\iterator_to_array($values));
} elseif (\is_array($values)) {
$keysDebug = \array_keys($values);
}
return $this->profileCall('setMultiple', \func_get_args(), true, $keysDebug);
}

/**
* {@inheritDoc}
*/
public function deleteMultiple($keys)
{
$keysDebug = array();
if ($keys instanceof Traversable) {
$keysDebug = \iterator_to_array($keys, false);
} elseif (\is_array($keys)) {
$keysDebug = $keys;
}
return $this->profileCall('deleteMultiple', \func_get_args(), true, $keysDebug);
}

/**
* {@inheritDoc}
*/
public function has($key)
{
return $this->profileCall('has', \func_get_args(), false, $key);
}
/*
Defined in CompatTrait:
get, set, delete, clear, getMultiple, setMultiple, deleteMultiple, has
*/

/**
* Debug::EVENT_OUTPUT subscriber
Expand Down Expand Up @@ -252,6 +179,28 @@ public function getLoggedActions()
return $this->loggedActions;
}

/**
* Get the keys being get/set/deleted
*
* @param iterable $keysOrValues keys or key=>value pairs
* @param bool $isValues key/values ?
*
* @return array
*/
protected function keysDebug($keysOrValues, $isValues = false)
{
$keysDebug = array();
if ($keysOrValues instanceof Traversable) {
$keysDebug = \iterator_to_array($keysOrValues, $isValues);
} elseif (\is_array($keysOrValues)) {
$keysDebug = $keysOrValues;
}
if ($isValues) {
$keysDebug = \array_keys($keysDebug);
}
return $keysDebug;
}

/**
* Profiles a call to a PDO method
*
Expand All @@ -267,18 +216,19 @@ protected function profileCall($method, array $args = array(), $isSuccessRespons
{
$info = new CallInfo($method, $keyOrKeys);

$exception = null;
$exception = null; // unexpected exception / will be re-thrown
$failureException = null; // method returned false
$result = null;
try {
$result = \call_user_func_array(array($this->cache, $method), $args);
$result = \call_user_func_array([$this->cache, $method], $args);
if ($isSuccessResponse && $result === false) {
$exception = new RuntimeException();
$failureException = new RuntimeException(__CLASS__ . '::' . $method . '() failed');
}
} catch (\Exception $e) {
} catch (Exception $e) {
$exception = $e;
}

$info->end($exception);
$info->end($exception ?: $failureException);
$this->addCallInfo($info);

if ($exception) {
Expand Down
28 changes: 28 additions & 0 deletions src/Debug/Collector/SimpleCache/CompatTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/**
* This file is part of PHPDebugConsole
*
* @package PHPDebugConsole
* @author Brad Kent <bkfake-github@yahoo.com>
* @license http://opensource.org/licenses/MIT MIT
* @copyright 2014-2024 Brad Kent
* @since 3.3
*/

namespace bdk\Debug\Collector\SimpleCache;

$refClass = new \ReflectionClass('Psr\SimpleCache\CacheInterface');
$refMethod = $refClass->getMethod('get');
$refParameters = $refMethod->getParameters();

if (\method_exists($refMethod, 'hasReturnType') && $refMethod->hasReturnType()) {
// psr/simple-cache 3.0
require __DIR__ . '/CompatTrait_3.php';
} elseif (\method_exists($refParameters[0], 'hasType') && $refParameters[0]->hasType()) {
// psr/simple-cache 2.0
require __DIR__ . '/CompatTrait_2.php';
} elseif (\trait_exists(__NAMESPACE__ . '\\CompatTrait', false) === false) {
// psr/simple-cache 1.0
require __DIR__ . '/CompatTrait_1.php';
}
92 changes: 92 additions & 0 deletions src/Debug/Collector/SimpleCache/CompatTrait_1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

/**
* This file is part of PHPDebugConsole
*
* @package PHPDebugConsole
* @author Brad Kent <bkfake-github@yahoo.com>
* @license http://opensource.org/licenses/MIT MIT
* @copyright 2014-2024 Brad Kent
* @since 3.3
*/

namespace bdk\Debug\Collector\SimpleCache;

/*
Wrap in condition.
PHPUnit code coverage scans all files and will conflict
*/
if (\trait_exists(__NAMESPACE__ . '\\CompatTrait', false) === false) {
/**
* Provide method signatures compatible with psr/simple-cache 1.x
*/
trait CompatTrait
{
/**
* {@inheritDoc}
*/
public function get($key, $default = null)
{
return $this->profileCall('get', \func_get_args(), false, $key);
}

/**
* {@inheritDoc}
*/
public function set($key, $value, $ttl = null)
{
return $this->profileCall('set', \func_get_args(), true, $key);
}

/**
* {@inheritDoc}
*/
public function delete($key)
{
return $this->profileCall('delete', \func_get_args(), false, $key);
}

/**
* {@inheritDoc}
*/
public function clear()
{
return $this->profileCall('clear', [], true);
}

/**
* {@inheritDoc}
*/
public function getMultiple($keys, $default = null)
{
$keysDebug = $this->keysDebug($keys);
return $this->profileCall('getMultiple', \func_get_args(), false, $keysDebug);
}

/**
* {@inheritDoc}
*/
public function setMultiple($values, $ttl = null)
{
$keysDebug = $this->keysDebug($values, true);
return $this->profileCall('setMultiple', \func_get_args(), true, $keysDebug);
}

/**
* {@inheritDoc}
*/
public function deleteMultiple($keys)
{
$keysDebug = $this->keysDebug($keys);
return $this->profileCall('deleteMultiple', \func_get_args(), true, $keysDebug);
}

/**
* {@inheritDoc}
*/
public function has($key)
{
return $this->profileCall('has', \func_get_args(), false, $key);
}
}
}
92 changes: 92 additions & 0 deletions src/Debug/Collector/SimpleCache/CompatTrait_2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

/**
* This file is part of PHPDebugConsole
*
* @package PHPDebugConsole
* @author Brad Kent <bkfake-github@yahoo.com>
* @license http://opensource.org/licenses/MIT MIT
* @copyright 2014-2024 Brad Kent
* @since 3.3
*/

namespace bdk\Debug\Collector\SimpleCache;

/*
Wrap in condition.
PHPUnit code coverage scans all files and will conflict
*/
if (\trait_exists(__NAMESPACE__ . '\\CompatTrait', false) === false) {
/**
* Provide method signatures compatible with psr/simple-cache 2.x
*/
trait CompatTrait
{
/**
* {@inheritDoc}
*/
public function get(string $key, mixed $default = null)
{
return $this->profileCall('get', \func_get_args(), false, $key);
}

/**
* {@inheritDoc}
*/
public function set(string $key, mixed $value, null|int|\DateInterval $ttl = null)
{
return $this->profileCall('set', \func_get_args(), true, $key);
}

/**
* {@inheritDoc}
*/
public function delete(string $key)
{
return $this->profileCall('delete', \func_get_args(), false, $key);
}

/**
* {@inheritDoc}
*/
public function clear()
{
return $this->profileCall('clear', [], true);
}

/**
* {@inheritDoc}
*/
public function getMultiple(string $keys, mixed $default = null)
{
$keysDebug = $this->keysDebug($keys);
return $this->profileCall('getMultiple', \func_get_args(), false, $keysDebug);
}

/**
* {@inheritDoc}
*/
public function setMultiple(iterable $values, null|int|\DateInterval $ttl = null)
{
$keysDebug = $this->keysDebug($values, true);
return $this->profileCall('setMultiple', \func_get_args(), true, $keysDebug);
}

/**
* {@inheritDoc}
*/
public function deleteMultiple(iterable $keys)
{
$keysDebug = $this->keysDebug($keys);
return $this->profileCall('deleteMultiple', \func_get_args(), true, $keysDebug);
}

/**
* {@inheritDoc}
*/
public function has(string $key)
{
return $this->profileCall('has', \func_get_args(), false, $key);
}
}
}
Loading

0 comments on commit 98bda7b

Please sign in to comment.