1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* This file is part of Phpfastcache. |
5
|
|
|
* |
6
|
|
|
* @license MIT License (MIT) |
7
|
|
|
* |
8
|
|
|
* For full copyright and license information, please see the docs/CREDITS.txt and LICENCE files. |
9
|
|
|
* |
10
|
|
|
* @author Georges.L (Geolim4) <[email protected]> |
11
|
|
|
* @author Contributors https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
declare(strict_types=1); |
15
|
|
|
|
16
|
|
|
namespace Phpfastcache\Config; |
17
|
|
|
|
18
|
|
|
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface; |
19
|
|
|
use Phpfastcache\Exceptions\PhpfastcacheLogicException; |
20
|
|
|
|
21
|
|
|
abstract class AbstractConfigurationOption implements LockableConfigurationInterface |
22
|
|
|
{ |
23
|
|
|
private bool $lockedObject = false; |
24
|
|
|
|
25
|
|
|
private ExtendedCacheItemPoolInterface $locker; |
26
|
|
|
|
27
|
|
|
public function lock(ExtendedCacheItemPoolInterface $poolInstance): static |
28
|
|
|
{ |
29
|
|
|
$this->lockedObject = true; |
30
|
|
|
$this->locker = $poolInstance; |
31
|
|
|
|
32
|
|
|
return $this; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function lockedBy(): ExtendedCacheItemPoolInterface |
36
|
|
|
{ |
37
|
|
|
return $this->locker; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function isLocked(): bool |
41
|
|
|
{ |
42
|
|
|
return $this->lockedObject; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @throws PhpfastcacheLogicException |
47
|
|
|
*/ |
48
|
|
|
protected function enforceLockedProperty(string $method): void |
49
|
|
|
{ |
50
|
|
|
if ($this->lockedObject === true) { |
51
|
|
|
$dbt = \debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS, 2); |
52
|
|
|
$cause = $dbt[\array_key_last($dbt)] ?? null; |
53
|
|
|
if ($cause) { |
54
|
|
|
$moreInfo = \sprintf('Caused line %d in %s', $cause['line'], $cause['file']); |
55
|
|
|
} |
56
|
|
|
throw new PhpfastcacheLogicException( |
57
|
|
|
\sprintf( |
58
|
|
|
'You can no longer change the configuration "%s" as the cache pool instance is now running. %s', |
59
|
|
|
\lcfirst(\substr($method, 3)), |
60
|
|
|
$moreInfo ?? '' |
61
|
|
|
) |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|