Skip to content

Commit 501a7e6

Browse files
committed
Added dedicated fallback configuration
1 parent f313197 commit 501a7e6

File tree

5 files changed

+297
-18
lines changed

5 files changed

+297
-18
lines changed

lib/Phpfastcache/CacheManager.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,10 @@ public static function getInstance($driver = 'auto', $config = null, $instanceId
130130
} catch (PhpfastcacheDriverCheckException $e) {
131131
if ($config->getFallback()) {
132132
try {
133-
134133
$fallback = $config->getFallback();
135134
$config->setFallback('');
136135
trigger_error(sprintf('The "%s" driver is unavailable at the moment, the fallback driver "%s" has been used instead.', $driver, $fallback), E_USER_WARNING);
137-
return self::getInstance($fallback, $config);
136+
return self::getInstance($fallback, $config->getFallbackConfig());
138137
} catch (PhpfastcacheInvalidArgumentException $e) {
139138
throw new PhpfastcacheInvalidConfigurationException('Invalid fallback driver configuration', 0, $e);
140139
}

lib/Phpfastcache/Config/ConfigurationOption.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace Phpfastcache\Config;
1010

11+
use Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException;
1112
use Phpfastcache\Exceptions\PhpfastcacheInvalidConfigurationException;
1213
use Phpfastcache\Util\ArrayObject;
1314

@@ -54,6 +55,11 @@ class ConfigurationOption extends ArrayObject
5455
*/
5556
protected $fallback = '';
5657

58+
/**
59+
* @var \Phpfastcache\Config\ConfigurationOption
60+
*/
61+
protected $fallbackConfig;
62+
5763
/**
5864
* @var int
5965
*/
@@ -304,6 +310,31 @@ public function setFallback(string $fallback): self
304310
return $this;
305311
}
306312

313+
/**
314+
* @return \Phpfastcache\Config\ConfigurationOption|null
315+
*/
316+
public function getFallbackConfig()
317+
{
318+
return $this->fallbackConfig;
319+
}
320+
321+
/**
322+
* @param \Phpfastcache\Config\ConfigurationOption|null $fallbackConfig
323+
* @return ConfigurationOption
324+
*/
325+
public function setFallbackConfig($fallbackConfig): self
326+
{
327+
if($fallbackConfig !== null && !($fallbackConfig instanceof self)){
328+
throw new PhpfastcacheInvalidArgumentException(sprintf(
329+
'Invalid argument "%s" for %s',
330+
gettype($fallbackConfig) === 'object' ? get_class($fallbackConfig) : gettype($fallbackConfig),
331+
__METHOD__
332+
));
333+
}
334+
$this->fallbackConfig = $fallbackConfig;
335+
return $this;
336+
}
337+
307338
/**
308339
* @return int
309340
*/

lib/Phpfastcache/Helper/TestHelper.php

Lines changed: 85 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class TestHelper
3535
*/
3636
public function __construct($testName)
3737
{
38-
$this->printText('[PhpFastCache CORE v' . Api::getPhpFastCacheVersion() . Api::getPhpFastCacheGitHeadHash() . ']', true);
38+
$this->printText('[PhpFastCache CORE v' . Api::getPhpFastCacheVersion() . Api::getPhpFastCacheGitHeadHash() . ']', true);
3939
$this->printText('[PhpFastCache API v' . Api::getVersion() . ']', true);
4040
$this->printText('[PHP v' . PHP_VERSION . ']', true);
4141
$this->printText("[Begin Test: '{$testName}']");
@@ -45,7 +45,8 @@ public function __construct($testName)
4545
* Catch all uncaught exception
4646
* to our own exception handler
4747
*/
48-
set_exception_handler(array($this, 'exceptionHandler'));
48+
set_exception_handler([$this, 'exceptionHandler']);
49+
set_error_handler([$this, 'errorHandler']);
4950
}
5051

5152
/**
@@ -88,6 +89,28 @@ public function printPassText($string): self
8889
return $this;
8990
}
9091

92+
/**
93+
* @param string $string
94+
* @return $this
95+
*/
96+
public function printInfoText($string): self
97+
{
98+
$this->printText("[INFO] {$string}");
99+
100+
return $this;
101+
}
102+
103+
/**
104+
* @param string $string
105+
* @return $this
106+
*/
107+
public function printDebugText($string): self
108+
{
109+
$this->printText("[DEBUG] {$string}");
110+
111+
return $this;
112+
}
113+
91114
/**
92115
* @param string $string
93116
* @return $this
@@ -183,10 +206,11 @@ public function accessInaccessibleMember($obj, $prop)
183206
/**
184207
* @param \Throwable $exception
185208
*/
186-
public function exceptionHandler(\Throwable $exception) {
187-
if($exception instanceof PhpfastcacheDriverCheckException){
209+
public function exceptionHandler(\Throwable $exception)
210+
{
211+
if ($exception instanceof PhpfastcacheDriverCheckException) {
188212
$this->printSkipText('A driver could not be initialized due to missing requirement: ' . $exception->getMessage());
189-
}else{
213+
} else {
190214
$this->printFailText(sprintf(
191215
'Uncaught exception "%s" in "%s" line %d with message: "%s"',
192216
\get_class($exception),
@@ -197,4 +221,60 @@ public function exceptionHandler(\Throwable $exception) {
197221
}
198222
$this->terminateTest();
199223
}
224+
225+
/**
226+
* @param int $errno
227+
* @param string $errstr
228+
* @param string $errfile
229+
* @param int $errline
230+
*/
231+
public function errorHandler(int $errno, string $errstr, string $errfile, int $errline)
232+
{
233+
$errorType = '';
234+
235+
switch ($errno) {
236+
case E_PARSE:
237+
case E_ERROR:
238+
case E_CORE_ERROR:
239+
case E_COMPILE_ERROR:
240+
case E_USER_ERROR:
241+
$errorType = '[FATAL ERROR]';
242+
break;
243+
case E_WARNING:
244+
case E_USER_WARNING:
245+
case E_COMPILE_WARNING:
246+
case E_RECOVERABLE_ERROR:
247+
$errorType = '[WARNING]';
248+
break;
249+
case E_NOTICE:
250+
case E_USER_NOTICE:
251+
$errorType = '[NOTICE]';
252+
break;
253+
case E_STRICT:
254+
$errorType = '[STRICT]';
255+
break;
256+
case E_DEPRECATED:
257+
case E_USER_DEPRECATED:
258+
$errorType = '[DEPRECATED]';
259+
break;
260+
default :
261+
break;
262+
}
263+
264+
if ($errorType === '[FATAL ERROR]') {
265+
$this->printFailText(sprintf(
266+
"A critical error has been caught: \"%s\" in %s line %d",
267+
"$errorType $errstr",
268+
$errfile,
269+
$errline
270+
));
271+
} else {
272+
$this->printDebugText(sprintf(
273+
"A non-critical error has been caught: \"%s\" in %s line %d",
274+
"$errorType $errstr",
275+
$errfile,
276+
$errline
277+
));
278+
}
279+
}
200280
}

tests/CustomNamespaces.test.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
require_once __DIR__ . '/../vendor/autoload.php';
1414
$testHelper = new TestHelper('Custom namespaces');
1515

16-
$testDir = __DIR__ . '/../lib/Phpfastcache/CustomDriversPath/Files2/';
16+
$testDir = __DIR__ . '/../lib/Phpfastcache/DriverTest/Files2/';
1717

1818
if (@!mkdir($testDir, 0777, true) && !is_dir($testDir))
1919
{
20-
$testHelper->printFailText('Cannot create CustomDriversPath directory');
20+
$testHelper->printFailText('Cannot create DriverTest directory');
2121
$testHelper->terminateTest();
2222
}
2323

@@ -39,12 +39,12 @@
3939
*
4040
*/
4141
42-
namespace Phpfastcache\CustomDriversPath\Files2;
42+
namespace Phpfastcache\DriverTest\Files2;
4343
use Phpfastcache\Drivers\Files\Driver as FilesDriver;
4444
4545
/**
4646
* Class Driver
47-
* @package Phpfastcache\CustomDriversPath\Files2
47+
* @package Phpfastcache\DriverTest\Files2
4848
*/
4949
class Driver extends FilesDriver
5050
{
@@ -70,12 +70,12 @@ class Driver extends FilesDriver
7070
*
7171
*/
7272
73-
namespace Phpfastcache\CustomDriversPath\Files2;
73+
namespace Phpfastcache\DriverTest\Files2;
7474
use Phpfastcache\Drivers\Files\Item as FilesItem;
7575
7676
/**
7777
* Class Item
78-
* @package Phpfastcache\CustomDriversPath\Files2
78+
* @package Phpfastcache\DriverTest\Files2
7979
*/
8080
class Item extends FilesItem
8181
{
@@ -101,12 +101,12 @@ class Item extends FilesItem
101101
*
102102
*/
103103
104-
namespace Phpfastcache\CustomDriversPath\Files2;
104+
namespace Phpfastcache\DriverTest\Files2;
105105
use Phpfastcache\Drivers\Files\Config as FilesConfig;
106106
107107
/**
108108
* Class Config
109-
* @package Phpfastcache\CustomDriversPath\Files2
109+
* @package Phpfastcache\DriverTest\Files2
110110
*/
111111
class Config extends FilesConfig
112112
{
@@ -136,16 +136,16 @@ class Config extends FilesConfig
136136
chmod("{$testDir}Driver.php", 0644);
137137
chmod("{$testDir}Item.php", 0644);
138138

139-
if(!class_exists(Phpfastcache\CustomDriversPath\Files2\Item::class)
140-
|| !class_exists(Phpfastcache\CustomDriversPath\Files2\Driver::class)
139+
if(!class_exists(Phpfastcache\DriverTest\Files2\Item::class)
140+
|| !class_exists(Phpfastcache\DriverTest\Files2\Driver::class)
141141
){
142142
$testHelper->printFailText('The php classes of driver "Files2" does not exists');
143143
$testHelper->terminateTest();
144144
}else{
145145
$testHelper->printPassText('The php classes of driver "Files2" were found');
146146
}
147147

148-
CacheManager::setNamespacePath(Phpfastcache\CustomDriversPath::class);
148+
CacheManager::setNamespacePath(Phpfastcache\DriverTest::class);
149149
$cacheInstance = CacheManager::getInstance('Files2');
150150
$cacheKey = 'cacheKey';
151151
$RandomCacheValue = str_shuffle(uniqid('pfc', true));

0 commit comments

Comments
 (0)