Skip to content

Commit c9ef975

Browse files
committed
Improved PhpfastcacheAbstractProxy class
1 parent c119191 commit c9ef975

File tree

5 files changed

+99
-97
lines changed

5 files changed

+99
-97
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- Added `PhpfastcacheEventManagerException` for EventManager-related exceptions
1919
- __Global__
2020
- Removed magics methods from CacheManager `CacheManager::DriverName()`, use `CacheManager::getInstance('DriverName')` instead
21+
- `\Phpfastcache\Proxy\PhpfastcacheAbstractProxy` now implements `\Phpfastcache\Proxy\PhpfastcacheAbstractProxyInterface`
2122
- Slightly increased performances on some critical points of the library
2223
- Removed "BadPracticeOMeter" notice in CacheManager
2324
- Removed many code duplicate (like in `\Phpfastcache\Driver\[DRIVER_NAME]\Item` classes)

lib/Phpfastcache/Proxy/PhpfastcacheAbstractProxy.php

Lines changed: 6 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -15,64 +15,21 @@
1515

1616
namespace Phpfastcache\Proxy;
1717

18-
use BadMethodCallException;
1918
use Phpfastcache\CacheManager;
20-
use Phpfastcache\Config\ConfigurationOption;
21-
use Phpfastcache\Core\Item\ExtendedCacheItemInterface;
19+
use Phpfastcache\Config\ConfigurationOptionInterface;
2220
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface;
23-
use Phpfastcache\Entities\DriverIO;
24-
use Phpfastcache\Entities\DriverStatistic;
2521
use Phpfastcache\Exceptions\PhpfastcacheDriverCheckException;
2622
use Phpfastcache\Exceptions\PhpfastcacheDriverException;
2723
use Phpfastcache\Exceptions\PhpfastcacheDriverNotFoundException;
2824
use Phpfastcache\Exceptions\PhpfastcacheLogicException;
29-
use Psr\Cache\CacheItemInterface;
3025

3126
/**
32-
* Class phpFastCache
33-
*
3427
* Handle methods using annotations for IDE
3528
* because they're handled by __call()
3629
* Check out ExtendedCacheItemInterface to see all
3730
* the drivers methods magically implemented
38-
*
39-
* @method ExtendedCacheItemInterface getItem(string $key) Retrieve an item and returns an empty item if not found
40-
* @method ExtendedCacheItemInterface[] getItems(string[] $keys) Retrieve an item and returns an empty item if not found
41-
* @method bool hasItem(string $key) Tests if an item exists
42-
* @method string getConfigClass()
43-
* @method ConfigurationOption getConfig()
44-
* @method ConfigurationOption getDefaultConfig()
45-
* @method string getDriverName()
46-
* @method string getInstanceId()
47-
* @method bool deleteItem(string $key) Delete an item
48-
* @method bool deleteItems(array $keys) Delete some items
49-
* @method bool save(CacheItemInterface $item) Save an item
50-
* @method bool saveMultiple(CacheItemInterface ...$items) Save multiple items
51-
* @method bool saveDeferred(CacheItemInterface $item) Sets a cache item to be persisted later
52-
* @method bool commit() Persists any deferred cache items
53-
* @method bool clear() Allow you to completely empty the cache and restart from the beginning
54-
* @method DriverStatistic getStats() Returns a DriverStatistic object
55-
* @method string getHelp() Returns help about a driver, if available
56-
* @method ExtendedCacheItemInterface getItemsByTag(string $tagName) Return items by a tag
57-
* @method ExtendedCacheItemInterface[] getItemsByTags(array $tagNames) Return items by some tags
58-
* @method bool deleteItemsByTag(string $tagName) Delete items by a tag
59-
* @method bool deleteItemsByTags(array $tagNames) // Delete items by some tags
60-
* @method void incrementItemsByTag(string $tagName, int $step = 1) // Increment items by a tag
61-
* @method void incrementItemsByTags(array $tagNames, int $step = 1) // Increment items by some tags
62-
* @method void decrementItemsByTag(string $tagName, int $step = 1) // Decrement items by a tag
63-
* @method void decrementItemsByTags(array $tagNames, int $step = 1) // Decrement items by some tags
64-
* @method void appendItemsByTag(string $tagName, mixed $data) // Append items by a tag
65-
* @method void appendItemsByTags(array $tagNames, mixed $data) // Append items by a tags
66-
* @method void prependItemsByTag(string $tagName, mixed $data) // Prepend items by a tag
67-
* @method void prependItemsByTags(array $tagNames, mixed $data) // Prepend items by a tags
68-
* @method string getItemsAsJsonString(array $keys, int $options, int$depth)
69-
* @method ExtendedCacheItemInterface detachItem(CacheItemInterface $item)
70-
* @method ExtendedCacheItemInterface attachItem(CacheItemInterface $item)
71-
* @method ExtendedCacheItemInterface detachAllItems()
72-
* @method bool isAttached()
73-
* @method DriverIO getIO()
7431
*/
75-
abstract class PhpfastcacheAbstractProxy
32+
abstract class PhpfastcacheAbstractProxy implements PhpfastcacheAbstractProxyInterface
7633
{
7734
/**
7835
* @var ExtendedCacheItemPoolInterface
@@ -82,13 +39,13 @@ abstract class PhpfastcacheAbstractProxy
8239
/**
8340
* PhpfastcacheAbstractProxy constructor.
8441
* @param string $driver
85-
* @param null $config
42+
* @param null|ConfigurationOptionInterface $config
8643
* @throws PhpfastcacheDriverCheckException
8744
* @throws PhpfastcacheDriverException
8845
* @throws PhpfastcacheDriverNotFoundException
8946
* @throws PhpfastcacheLogicException
9047
*/
91-
public function __construct(string $driver, $config = null)
48+
public function __construct(string $driver, ?ConfigurationOptionInterface $config = null)
9249
{
9350
$this->instance = CacheManager::getInstance($driver, $config);
9451
}
@@ -97,14 +54,9 @@ public function __construct(string $driver, $config = null)
9754
* @param string $name
9855
* @param array $args
9956
* @return mixed
100-
* @throws BadMethodCallException
10157
*/
102-
public function __call(string $name, array $args)
58+
public function __call(string $name, array $args): mixed
10359
{
104-
if (\method_exists($this->instance, $name)) {
105-
return $this->instance->$name(...$args);
106-
}
107-
108-
throw new BadMethodCallException(\sprintf('Method %s does not exists', $name));
60+
return $this->instance->$name(...$args);
10961
}
11062
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
/**
4+
*
5+
* This file is part of Phpfastcache.
6+
*
7+
* @license MIT License (MIT)
8+
*
9+
* For full copyright and license information, please see the docs/CREDITS.txt and LICENCE files.
10+
*
11+
* @author Georges.L (Geolim4) <contact@geolim4.com>
12+
* @author Contributors https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors
13+
*/
14+
declare(strict_types=1);
15+
16+
namespace Phpfastcache\Proxy;
17+
18+
use Phpfastcache\Config\ConfigurationOption;
19+
use Phpfastcache\Core\Item\ExtendedCacheItemInterface;
20+
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface;
21+
use Phpfastcache\Core\Pool\TaggableCacheItemPoolInterface;
22+
use Phpfastcache\Entities\DriverIO;
23+
use Phpfastcache\Entities\DriverStatistic;
24+
use Psr\Cache\CacheItemInterface;
25+
26+
/**
27+
* @method ExtendedCacheItemInterface getItem(string $key) Retrieve an item and returns an empty item if not found
28+
* @method ExtendedCacheItemInterface[] getItems(string[] $keys) Retrieve an item and returns an empty item if not found
29+
* @method bool hasItem(string $key) Tests if an item exists
30+
* @method static string getConfigClass()
31+
* @method ConfigurationOption getItemClass()
32+
* @method ConfigurationOption getConfig()
33+
* @method ConfigurationOption getDefaultConfig()
34+
* @method string getDriverName()
35+
* @method string getInstanceId()
36+
* @method bool deleteItem(string $key) Delete an item
37+
* @method bool deleteItems(string[] $keys) Delete some items
38+
* @method bool save(CacheItemInterface $item) Save an item
39+
* @method bool saveMultiple(CacheItemInterface ...$items) Save multiple items
40+
* @method bool saveDeferred(CacheItemInterface $item) Sets a cache item to be persisted later
41+
* @method bool commit() Persists any deferred cache items
42+
* @method bool clear() Allow you to completely empty the cache and restart from the beginning
43+
* @method DriverStatistic getStats() Returns a DriverStatistic object
44+
* @method string getHelp() Returns help about a driver, if available
45+
* @method ExtendedCacheItemInterface getItemsByTag(string $tagName, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE) Return items by a tag
46+
* @method ExtendedCacheItemInterface[] getItemsByTags(string[] $tagNames, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE) Return items by some tags
47+
* @method bool deleteItemsByTag(string $tagName, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE) Delete items by a tag
48+
* @method bool deleteItemsByTags(string[] $tagNames, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE) // Delete items by some tags
49+
* @method void incrementItemsByTag(string $tagName, int $step = 1, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE) // Increment items by a tag
50+
* @method void incrementItemsByTags(string[] $tagNames, int $step = 1, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE) // Increment items by some tags
51+
* @method void decrementItemsByTag(string $tagName, int $step = 1, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE) // Decrement items by a tag
52+
* @method void decrementItemsByTags(string[] $tagNames, int $step = 1, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE) // Decrement items by some tags
53+
* @method void appendItemsByTag(string $tagName, mixed $data, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE) // Append items by a tag
54+
* @method void appendItemsByTags(string[] $tagNames, mixed $data, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE) // Append items by a tags
55+
* @method void prependItemsByTag(string $tagName, mixed $data, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE) // Prepend items by a tag
56+
* @method void prependItemsByTags(string[] $tagNames, mixed $data, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE) // Prepend items by a tags
57+
* @method string getItemsAsJsonString(string[] $tagNames, int $option, int $depth, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE)
58+
* @method ExtendedCacheItemInterface detachItem(CacheItemInterface $item)
59+
* @method ExtendedCacheItemInterface attachItem(CacheItemInterface $item)
60+
* @method ExtendedCacheItemInterface detachAllItems()
61+
* @method bool isAttached(CacheItemInterface $item)
62+
* @method DriverIO getIO()
63+
*/
64+
interface PhpfastcacheAbstractProxyInterface
65+
{
66+
public const DRIVER_CHECK_FAILURE = ExtendedCacheItemPoolInterface::DRIVER_CHECK_FAILURE;
67+
public const DRIVER_CONNECT_FAILURE = ExtendedCacheItemPoolInterface::DRIVER_CONNECT_FAILURE;
68+
public const DRIVER_KEY_WRAPPER_INDEX = ExtendedCacheItemPoolInterface::DRIVER_KEY_WRAPPER_INDEX;
69+
public const DRIVER_DATA_WRAPPER_INDEX = ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX;
70+
public const DRIVER_EDATE_WRAPPER_INDEX = ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX;
71+
public const DRIVER_CDATE_WRAPPER_INDEX = ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX;
72+
public const DRIVER_MDATE_WRAPPER_INDEX = ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX;
73+
74+
public const DRIVER_TAGS_KEY_PREFIX = TaggableCacheItemPoolInterface::DRIVER_TAGS_KEY_PREFIX;
75+
public const DRIVER_TAGS_WRAPPER_INDEX = TaggableCacheItemPoolInterface::DRIVER_TAGS_WRAPPER_INDEX;
76+
public const TAG_STRATEGY_ONE = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE;
77+
public const TAG_STRATEGY_ALL = TaggableCacheItemPoolInterface::TAG_STRATEGY_ALL;
78+
public const TAG_STRATEGY_ONLY = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONLY;
79+
80+
public function __call(string $name, array $args): mixed;
81+
}

tests/AbstractProxy.test.php

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -21,37 +21,7 @@
2121
$testHelper = new TestHelper('phpfastcacheAbstractProxy class');
2222
$defaultDriver = (!empty($argv[1]) ? ucfirst($argv[1]) : 'Files');
2323

24+
$myCustomClass = new class($defaultDriver) extends PhpfastcacheAbstractProxy{};
2425

25-
/**
26-
* Dynamic driver-based example
27-
* Class myCustomCacheClass
28-
* @package MyCustom\Project
29-
*/
30-
class CustomMemcachedCacheClass extends PhpfastcacheAbstractProxy
31-
{
32-
public function __construct($driver = '', $config = null)
33-
{
34-
global $defaultDriver;
35-
$driver = $defaultDriver;
36-
parent::__construct($driver, $config);
37-
/**
38-
* That's all !! Your cache class is ready to use
39-
*/
40-
}
41-
}
42-
43-
44-
/**
45-
* Testing memcached as it is declared in .travis.yml
46-
*/
47-
$driverInstance = new CustomMemcachedCacheClass();
48-
49-
if (!is_object($driverInstance->getItem('test'))) {
50-
$testHelper->assertFail('$driverInstance->getItem() returned an invalid var type:' . gettype($driverInstance));
51-
} elseif (!($driverInstance->getItem('test') instanceof ExtendedCacheItemInterface)) {
52-
$testHelper->assertFail('$driverInstance->getItem() returned an invalid class that does not implements ExtendedCacheItemInterface: ' . get_class($driverInstance));
53-
} else {
54-
$testHelper->assertPass('$driverInstance->getItem() returned a valid class that implements ExtendedCacheItemInterface: ' . get_class($driverInstance));
55-
}
56-
26+
$testHelper->runCRUDTests($myCustomClass);
5727
$testHelper->terminateTest();

tests/lib/Helper/TestHelper.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException;
2525
use Phpfastcache\Exceptions\PhpfastcacheIOException;
2626
use Phpfastcache\Exceptions\PhpfastcacheLogicException;
27+
use Phpfastcache\Proxy\PhpfastcacheAbstractProxyInterface;
2728
use Phpfastcache\Util\SapiDetector;
2829
use Psr\Cache\InvalidArgumentException;
2930
use ReflectionClass;
@@ -59,7 +60,7 @@ public function __construct(string $testName)
5960
{
6061
$this->timestamp = microtime(true);
6162
$this->testName = $testName;
62-
$this->climate = new CLImate;
63+
$this->climate = new CLImate();
6364
$this->climate->forceAnsiOn();
6465

6566
/**
@@ -111,7 +112,7 @@ public function printHeaders(): void
111112

112113
$loadedExtensions = get_loaded_extensions();
113114
natcasesort($loadedExtensions);
114-
$this->printText("[<blue>Begin Test:</blue> <magenta>{$this->testName}</magenta>]");
115+
$this->printText("[<blue>Begin Test:</blue> <magenta>$this->testName</magenta>]");
115116
$this->printText('[<blue>PHPFASTCACHE:</blue> CORE <yellow>v' . Api::getPhpfastcacheVersion() . Api::getPhpfastcacheGitHeadHash() . '</yellow> | API <yellow>v' . Api::getVersion() . '</yellow>]');
116117
$this->printText('[<blue>PHP</blue> <yellow>v' . PHP_VERSION . '</yellow> with: <green>' . implode(', ', $loadedExtensions) . '</green>]');
117118
$this->printText('---');
@@ -129,7 +130,7 @@ public function printText(array|string $string, bool $strtoupper = false, string
129130
$string = implode("\n", $string);
130131
}
131132
if ($prefix) {
132-
$string = "[{$prefix}] {$string}";
133+
$string = "[$prefix] $string";
133134
}
134135
if (!$strtoupper) {
135136
$this->climate->out($string);
@@ -235,10 +236,6 @@ public function assertSkip(string $string): self
235236
return $this;
236237
}
237238

238-
239-
/**
240-
* @return void
241-
*/
242239
public function terminateTest(): void
243240
{
244241
$execTime = round(microtime(true) - $this->timestamp, 3);
@@ -256,7 +253,7 @@ public function terminateTest(): void
256253
!$this->numOfPassedTests && $totalCount ? 'red' : 'green',
257254
$this->numOfPassedTests,
258255
ngettext('assertion', 'assertions', $this->numOfPassedTests),
259-
"<cyan>{$totalCount}</cyan>",
256+
"<cyan>$totalCount</cyan>",
260257
ngettext('assertion', 'assertions', $totalCount),
261258
)
262259
);
@@ -366,19 +363,20 @@ public function debugEvents(EventManagerInterface $eventManager): void
366363
{
367364
$eventManager->onEveryEvents(
368365
function (string $eventName) {
369-
$this->printDebugText("Triggered event '{$eventName}'");
366+
$this->printDebugText("Triggered event '$eventName'");
370367
},
371368
'debugCallback'
372369
);
373370
}
374371

375372
/**
376-
* @param ExtendedCacheItemPoolInterface $pool
373+
* @param ExtendedCacheItemPoolInterface|PhpfastcacheAbstractProxyInterface $pool
377374
* @param bool $poolClear
378375
* @throws PhpfastcacheInvalidArgumentException
379376
* @throws InvalidArgumentException
377+
* @throws \Exception
380378
*/
381-
public function runCRUDTests(ExtendedCacheItemPoolInterface $pool, bool $poolClear = true): void
379+
public function runCRUDTests(ExtendedCacheItemPoolInterface|PhpfastcacheAbstractProxyInterface $pool, bool $poolClear = true): void
382380
{
383381
$this->printInfoText('Running CRUD tests on the following backend: ' . get_class($pool));
384382

0 commit comments

Comments
 (0)