Skip to content

Commit 629ebbe

Browse files
committed
Improved "Auto" driver context with tests, new interface method and additional checks
1 parent c2ec86b commit 629ebbe

File tree

10 files changed

+106
-19
lines changed

10 files changed

+106
-19
lines changed

CHANGELOG_API.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 2.0.2
2+
- Added ExtendedCacheItemPoolInterface::isUsableInAutoContext() to check if the driver is allowed to be used in 'Auto' context.
3+
14
## 2.0.1
25
- Implemented additional atomic methods:
36
- Added ExtendedCacheItemInterface::isNull() to test if the data is null or not despite the hit/miss status.

lib/Phpfastcache/Api.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
*/
2525
class Api
2626
{
27-
protected static $version = '2.0.1';
27+
protected static $version = '2.0.2';
2828

2929
/**
3030
* Api constructor.

lib/Phpfastcache/CacheManager.php

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ class CacheManager
9292
*/
9393
protected static $driverCustoms = [];
9494

95+
/**
96+
* @var array
97+
*/
98+
protected static $badPracticeOmeter = [];
99+
95100
/**
96101
* @param string $driver
97102
* @param array|ConfigurationOption $config
@@ -105,14 +110,8 @@ class CacheManager
105110
* @throws PhpfastcacheInvalidArgumentException
106111
* @throws PhpfastcacheDriverException
107112
*/
108-
public static function getInstance($driver = 'auto', $config = null, $instanceId = null): ExtendedCacheItemPoolInterface
113+
public static function getInstance(string $driver = 'auto', $config = null, string $instanceId = null): ExtendedCacheItemPoolInterface
109114
{
110-
static $badPracticeOmeter = [];
111-
112-
if ($instanceId !== null && !\is_string($instanceId)) {
113-
throw new PhpfastcacheInvalidArgumentException('The Instance ID must be a string');
114-
}
115-
116115
if (\is_array($config)) {
117116
$config = new ConfigurationOption($config);
118117
trigger_error(
@@ -134,7 +133,7 @@ public static function getInstance($driver = 'auto', $config = null, $instanceId
134133
$instance = $instanceId ?: md5($driver . \serialize($config->toArray()));
135134

136135
if (!isset(self::$instances[ $instance ])) {
137-
$badPracticeOmeter[ $driver ] = 1;
136+
self::$badPracticeOmeter[ $driver ] = 1;
138137
$driverClass = self::getDriverClass($driver);
139138

140139
if(!is_a($driverClass, ExtendedCacheItemPoolInterface::class, true)){
@@ -166,12 +165,12 @@ public static function getInstance($driver = 'auto', $config = null, $instanceId
166165
throw new PhpfastcacheDriverCheckException($e->getMessage(), $e->getCode(), $e);
167166
}
168167
}
169-
} else if ($badPracticeOmeter[ $driver ] >= 2) {
168+
} else if (self::$badPracticeOmeter[ $driver ] >= 2) {
170169
trigger_error('[' . $driver . '] Calling many times CacheManager::getInstance() for already instanced drivers is a bad practice and have a significant impact on performances.
171170
See https://github.com/PHPSocialNetwork/phpfastcache/wiki/[V5]-Why-calling-getInstance%28%29-each-time-is-a-bad-practice-%3F');
172171
}
173172

174-
$badPracticeOmeter[ $driver ]++;
173+
self::$badPracticeOmeter[ $driver ]++;
175174

176175
return self::$instances[ $instance ];
177176
}
@@ -184,7 +183,7 @@ public static function getInstance($driver = 'auto', $config = null, $instanceId
184183
* @throws PhpfastcacheInvalidArgumentException
185184
* @throws PhpfastcacheInstanceNotFoundException
186185
*/
187-
public static function getInstanceById($instanceId): ExtendedCacheItemPoolInterface
186+
public static function getInstanceById(string $instanceId): ExtendedCacheItemPoolInterface
188187
{
189188
if ($instanceId !== null && !\is_string($instanceId)) {
190189
throw new PhpfastcacheInvalidArgumentException('The Instance ID must be a string');
@@ -238,12 +237,15 @@ public static function getAutoClass(ConfigurationOption $config): string
238237

239238
if ($autoDriver === null) {
240239
foreach (self::getDriverList() as $driver) {
241-
try {
242-
self::getInstance($driver, $config);
243-
$autoDriver = $driver;
244-
break;
245-
} catch (PhpfastcacheDriverCheckException $e) {
246-
continue;
240+
/** @var ExtendedCacheItemPoolInterface $driver */
241+
if((self::CORE_DRIVER_NAMESPACE . $driver . '\Driver')::isUsableInAutoContext()){
242+
try {
243+
self::getInstance($driver, $config);
244+
$autoDriver = $driver;
245+
break;
246+
} catch (PhpfastcacheDriverCheckException $e) {
247+
continue;
248+
}
247249
}
248250
}
249251
}
@@ -252,6 +254,8 @@ public static function getAutoClass(ConfigurationOption $config): string
252254
throw new PhpfastcacheLogicException('Unable to find out a valid driver automatically');
253255
}
254256

257+
self::$badPracticeOmeter[ $autoDriver ]--;
258+
255259
return $autoDriver;
256260
}
257261

lib/Phpfastcache/Core/Pool/ExtendedCacheItemPoolInterface.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,4 +530,12 @@ public function setEventManager(EventManager $em): self;
530530
* @return bool
531531
*/
532532
public function saveMultiple(...$items): bool;
533+
534+
/**
535+
* Defines if the driver is allowed
536+
* to be used in "Auto" driver.
537+
*
538+
* @return bool
539+
*/
540+
public static function isUsableInAutoContext(): bool;
533541
}

lib/Phpfastcache/Core/Pool/ExtendedCacheItemPoolTrait.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,9 @@ public function saveMultiple(...$items): bool
478478
$this->save($item);
479479
}
480480
return true;
481-
} else if (\is_array($items)) {
481+
}
482+
483+
if (\is_array($items)) {
482484
foreach ($items as $item) {
483485
$this->save($item);
484486
}
@@ -487,6 +489,14 @@ public function saveMultiple(...$items): bool
487489
return false;
488490
}
489491

492+
/**
493+
* @inheritdoc
494+
*/
495+
public static function isUsableInAutoContext(): bool
496+
{
497+
return true;
498+
}
499+
490500
/**
491501
* @return string
492502
*/

lib/Phpfastcache/Drivers/Cookie/Driver.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,14 @@ protected function driverClear(): bool
150150
return $return;
151151
}
152152

153+
/**
154+
* @inheritdoc
155+
*/
156+
public static function isUsableInAutoContext(): bool
157+
{
158+
return false;
159+
}
160+
153161
/********************
154162
*
155163
* PSR-6 Extended Methods

lib/Phpfastcache/Drivers/Devfalse/Driver.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,14 @@ protected function driverConnect(): bool
105105
return true;
106106
}
107107

108+
/**
109+
* @inheritdoc
110+
*/
111+
public static function isUsableInAutoContext(): bool
112+
{
113+
return false;
114+
}
115+
108116
/********************
109117
*
110118
* PSR-6 Extended Methods

lib/Phpfastcache/Drivers/Devnull/Driver.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,14 @@ protected function driverConnect(): bool
9999
return true;
100100
}
101101

102+
/**
103+
* @inheritdoc
104+
*/
105+
public static function isUsableInAutoContext(): bool
106+
{
107+
return false;
108+
}
109+
102110
/********************
103111
*
104112
* PSR-6 Extended Methods

lib/Phpfastcache/Drivers/Devtrue/Driver.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,14 @@ protected function driverConnect(): bool
103103
return false;
104104
}
105105

106+
/**
107+
* @inheritdoc
108+
*/
109+
public static function isUsableInAutoContext(): bool
110+
{
111+
return false;
112+
}
113+
106114
/********************
107115
*
108116
* PSR-6 Extended Methods

tests/AutoDriver.test.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/**
4+
* @author Khoa Bui (khoaofgod) <[email protected]> http://www.phpfastcache.com
5+
* @author Georges.L (Geolim4) <[email protected]>
6+
*/
7+
8+
use Phpfastcache\CacheManager;
9+
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface;
10+
use Phpfastcache\Exceptions\PhpfastcacheRootException;
11+
use Phpfastcache\Helper\TestHelper;
12+
13+
chdir(__DIR__);
14+
require_once __DIR__ . '/../vendor/autoload.php';
15+
$testHelper = new TestHelper('"Auto" driver');
16+
17+
try{
18+
$driverInstance = CacheManager::getInstance('Auto');
19+
if($driverInstance instanceof ExtendedCacheItemPoolInterface){
20+
$testHelper->printPassText(sprintf('Found "%s" driver in "Auto" context', get_class($driverInstance)));
21+
}else{
22+
$testHelper->printFailText('No driver found in "Auto" context');
23+
}
24+
25+
}catch (PhpfastcacheRootException $e){
26+
$testHelper->printFailText('Got an exception while trying to find a driver in "Auto" context: ' . $e->getMessage());
27+
}
28+
29+
30+
$testHelper->terminateTest();

0 commit comments

Comments
 (0)