Skip to content

Commit 0a233b1

Browse files
committed
- Removed the old magic methods from the Cache manager
- Added LockableConfiguration interface
1 parent e6f9d38 commit 0a233b1

31 files changed

Lines changed: 507 additions & 381 deletions

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ install:
4747
- ./bin/ci/scripts/install_dependencies.sh
4848

4949
script:
50-
- ./vendor/bin/phpcs lib/ --report=summary
50+
- ./vendor/bin/phpcs lib/ --report=summary
5151
- ./vendor/bin/phpmd lib/ ansi phpmd.xml
5252
- ./vendor/bin/phpstan analyse lib/ -l 2 -c phpstan.neon 2>&1
5353
- php -f ./bin/ci/run_tests.php

CHANGELOG.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
## 9.0.0
2-
##### 30 august 2021
3-
- __Estimated release date__: _End of 2021 (december)_
2+
##### 1 november 2021
3+
- __Migration guide__
4+
- Read the [migration guide](./docs/migration/MigratingFromV8ToV9.md) to upgrade from V8 to V9
45
- __API__
56
- Upgraded Phpfastcache API `4.0.0` ([see changes](CHANGELOG_API.md))
67
- Renamed `Api::getPhpFastCacheVersion()` to `Api::getPhpfastcacheVersion()`
78
- Renamed `Api::getPhpFastCacheChangelog()` to `Api::getPhpfastcacheChangelog()`
89
- Renamed `Api::getPhpFastCacheGitHeadHash()` to `Api::getPhpfastcacheGitHeadHash()`
910
- __Global__
11+
- Removed magics methods from CacheManager `CacheManager::DriverName()`, use `CacheManager::getInstance('DriverName')` instead
1012
- Slightly increased performances on some critical points of the library
1113
- Removed "BadPracticeOMeter" notice in CacheManager
1214
- Removed many code duplicate (like in `\Phpfastcache\Driver\[DRIVER_NAME]\Item` classes)
@@ -22,10 +24,11 @@
2224
- Deprecated `\Phpfastcache\Helper\CacheConditionalHelper`, use `\Phpfastcache\CacheContract` instead
2325
- The `\Phpfastcache\CacheContract` class is now also callable directly without calling `get()` method
2426
- __Config/Options__
27+
- Configuration object will now be locked once the cache pool instance is running.
2528
- Updated `ConfigurationOption` which is no longer an `ArrayObject` class, therefore array-syntax is no longer available.
26-
- Renamed configuration entry `htaccess` to `autoHtaccessCreationEnabled` for files-based drivers.
27-
- Renamed `IOConfigurationOptionTrait::getHtaccess()` to `IOConfigurationOptionTrait::isAutoHtaccessCreationEnabled()`
28-
- Renamed `IOConfigurationOptionTrait::setHtaccess()` to `IOConfigurationOptionTrait::setAutoHtaccessCreationEnabled()`
29+
- Removed configuration entry `htaccess` for files-based drivers.
30+
- Removed `IOConfigurationOptionTrait::getHtaccess()`
31+
- Removed `IOConfigurationOptionTrait::setHtaccess()`
2932
- __Tests__
3033
- Added PHPMD, PHPCS and PHPSTAN coverages to increase quality of the project
3134
- Updated tests to work with new core/drivers changes

composer.json

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,6 @@
1818
"homepage": "https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors"
1919
}
2020
],
21-
"funding": [
22-
{
23-
"type": "patreon",
24-
"url": "https://www.patreon.com/geolim4"
25-
}
26-
],
2721
"require": {
2822
"php": "^8.0",
2923
"psr/cache": "^3.0",
@@ -66,5 +60,16 @@
6660
"Phpfastcache\\": "lib/Phpfastcache/",
6761
"Phpfastcache\\Tests\\": "tests/lib/"
6862
}
69-
}
63+
},
64+
"support": {
65+
"issues": "https://github.com/PHPSocialNetwork/phpfastcache/issues",
66+
"wiki": "https://github.com/PHPSocialNetwork/phpfastcache/wiki",
67+
"source": "https://github.com/PHPSocialNetwork/phpfastcache"
68+
},
69+
"funding": [
70+
{
71+
"type": "patreon",
72+
"url": "https://www.patreon.com/geolim4"
73+
}
74+
]
7075
}

composer.lock

Lines changed: 11 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/migration/MigratingFromV8ToV9.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ Once released, the php version 8.1 will be unit-tested
77
### Embedded autoload has been removed (and therefore, embedded dependencies)
88
Use [Composer](https://getcomposer.org/doc/03-cli.md#require) to include Phpfastcache in your project
99

10+
### Removed magics methods from CacheManager `CacheManager::DriverName()`
11+
Use `CacheManager::getInstance('DriverName')` instead.\
12+
This decision was made to make the cache manager more consistent by removing the "old legacy code".
13+
1014
### Updated `ConfigurationOption` which is no longer an `ArrayObject` class
1115
You can no longer use the following array-compatible syntax: `$config['yourKey'] = 'value'`\
1216
Use the object-notation syntax instead: `$config->setYourKey('value')`
@@ -38,10 +42,13 @@ See [EVENTS.md](./../EVENTS.md) file for more information
3842
They have not been replaced.
3943
However, the `Devrandom` driver with configurable factor chance and data length has been added
4044

41-
### Renamed configuration entry `htaccess` to `autoHtaccessCreationEnabled` for files-based drivers.
42-
- The variable has been renamed to better comply with its supposed role and avoid phpmd exception.
43-
- Renamed `IOConfigurationOptionTrait::getHtaccess()` to `IOConfigurationOptionTrait::isAutoHtaccessCreationEnabled()`
44-
- Renamed `IOConfigurationOptionTrait::setHtaccess()` to `IOConfigurationOptionTrait::setAutoHtaccessCreationEnabled()`
45+
### Removed configuration entry `htaccess` for files-based drivers.
46+
- We consider that it's no longer the task of Phpfastcache to handle server configuration
47+
- Removed `IOConfigurationOptionTrait::getHtaccess()`
48+
- Removed `IOConfigurationOptionTrait::setHtaccess()`
49+
50+
### Configuration object will now be locked once the cache pool instance is running
51+
If you try to set a configuration value after the cache pool instance is being built, an exception will be thrown.
4552

4653
### Removed `Cookie` driver because of its potential dangerosity
4754
However, you can always implement it by yourself if you want to by putting it back from previous versions using `\Phpfastcache\CacheManager::addCustomDriver()` method

lib/Phpfastcache/CacheManager.php

Lines changed: 16 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -27,38 +27,13 @@
2727
use Phpfastcache\Exceptions\PhpfastcacheUnsupportedOperationException;
2828
use Phpfastcache\Util\ClassNamespaceResolverTrait;
2929

30-
/**
31-
* Class CacheManager
32-
* @package phpFastCache
33-
*
34-
* @method static ExtendedCacheItemPoolInterface Apcu() Apcu($config = []) Return a driver "Apcu" instance
35-
* @method static ExtendedCacheItemPoolInterface Cassandra() Cassandra($config = []) Return a driver "Cassandra" instance
36-
* @method static ExtendedCacheItemPoolInterface Cookie() Cookie($config = []) Return a driver "Cookie" instance
37-
* @method static ExtendedCacheItemPoolInterface Couchbase() Couchbase($config = []) Return a driver "Couchbase" instance
38-
* @method static ExtendedCacheItemPoolInterface Couchdb() Couchdb($config = []) Return a driver "Couchdb" instance
39-
* @method static ExtendedCacheItemPoolInterface Devnull() Devnull($config = []) Return a driver "Devnull" instance
40-
* @method static ExtendedCacheItemPoolInterface Files() Files($config = []) Return a driver "files" instance
41-
* @method static ExtendedCacheItemPoolInterface Leveldb() Leveldb($config = []) Return a driver "Leveldb" instance
42-
* @method static ExtendedCacheItemPoolInterface Memcache() Memcache($config = []) Return a driver "Memcache" instance
43-
* @method static ExtendedCacheItemPoolInterface Memcached() Memcached($config = []) Return a driver "Memcached" instance
44-
* @method static ExtendedCacheItemPoolInterface Memstatic() Memstatic($config = []) Return a driver "Memstatic" instance
45-
* @method static ExtendedCacheItemPoolInterface Mongodb() Mongodb($config = []) Return a driver "Mongodb" instance
46-
* @method static ExtendedCacheItemPoolInterface Predis() Predis($config = []) Return a driver "Predis" instance
47-
* @method static ExtendedCacheItemPoolInterface Redis() Redis($config = []) Return a driver "Pedis" instance
48-
* @method static ExtendedCacheItemPoolInterface Sqlite() Sqlite($config = []) Return a driver "Sqlite" instance
49-
* @method static ExtendedCacheItemPoolInterface Ssdb() Ssdb($config = []) Return a driver "Ssdb" instance
50-
* @method static ExtendedCacheItemPoolInterface Wincache() Wincache($config = []) Return a driver "Wincache" instance
51-
* @method static ExtendedCacheItemPoolInterface Zenddisk() Zenddisk($config = []) Return a driver "Zend disk cache" instance
52-
* @method static ExtendedCacheItemPoolInterface Zendshm() Zendshm($config = []) Return a driver "Zend memory cache" instance
53-
*
54-
*/
5530
class CacheManager
5631
{
5732
public const CORE_DRIVER_NAMESPACE = 'Phpfastcache\Drivers\\';
5833

5934
use ClassNamespaceResolverTrait;
6035

61-
protected static ConfigurationOption $config;
36+
protected static ConfigurationOptionInterface $config;
6237

6338
protected static string $namespacePath;
6439

@@ -109,22 +84,6 @@ public static function getInstances(): array
10984
return self::$instances;
11085
}
11186

112-
/**
113-
* @param string $name
114-
* @param array $arguments
115-
* @return ExtendedCacheItemPoolInterface
116-
* @throws PhpfastcacheDriverCheckException
117-
* @throws PhpfastcacheDriverException
118-
* @throws PhpfastcacheDriverNotFoundException
119-
* @throws PhpfastcacheInvalidArgumentException
120-
*/
121-
public static function __callStatic(string $name, array $arguments): ExtendedCacheItemPoolInterface
122-
{
123-
$options = (\array_key_exists(0, $arguments) ? $arguments[0] : []);
124-
125-
return self::getInstance($name, $options);
126-
}
127-
12887
/**
12988
* @param string $driver
13089
* @param ConfigurationOptionInterface|null $config
@@ -133,13 +92,12 @@ public static function __callStatic(string $name, array $arguments): ExtendedCac
13392
* @throws PhpfastcacheDriverCheckException
13493
* @throws PhpfastcacheDriverException
13594
* @throws PhpfastcacheDriverNotFoundException
136-
* @throws PhpfastcacheInvalidArgumentException
95+
* @throws PhpfastcacheLogicException
13796
*/
13897
public static function getInstance(string $driver, ?ConfigurationOptionInterface $config = null, ?string $instanceId = null): ExtendedCacheItemPoolInterface
13998
{
14099
$config = self::validateConfig($config);
141100
$driver = self::standardizeDriverName($driver);
142-
143101
$instanceId = $instanceId ?: md5($driver . \serialize(\array_filter($config->toArray(), static fn ($val) => !\is_callable($val))));
144102

145103
if (!isset(self::$instances[$instanceId])) {
@@ -161,18 +119,22 @@ public static function getInstance(string $driver, ?ConfigurationOptionInterface
161119
}
162120

163121
/**
164-
* @param ConfigurationOption|null $config
165-
* @return ConfigurationOption
122+
* @param ConfigurationOptionInterface|null $config
123+
* @return ConfigurationOptionInterface
124+
* @throws PhpfastcacheLogicException
166125
*/
167-
protected static function validateConfig(?ConfigurationOption $config): ConfigurationOption
126+
protected static function validateConfig(?ConfigurationOptionInterface $config): ConfigurationOptionInterface
168127
{
128+
if ($config instanceof ConfigurationOptionInterface && $config->isLocked()) {
129+
throw new PhpfastcacheLogicException('You provided an already locked configuration, cannot continue.');
130+
}
169131
return $config ?? self::getDefaultConfig();
170132
}
171133

172134
/**
173-
* @return ConfigurationOption
135+
* @return ConfigurationOptionInterface
174136
*/
175-
public static function getDefaultConfig(): ConfigurationOption
137+
public static function getDefaultConfig(): ConfigurationOptionInterface
176138
{
177139
return self::$config ?? self::$config = new ConfigurationOption();
178140
}
@@ -246,7 +208,8 @@ public static function clearInstances(): bool
246208
self::$instances = [];
247209

248210
\gc_collect_cycles();
249-
return !\count(self::$instances);
211+
212+
return true;
250213
}
251214

252215
/**
@@ -274,9 +237,10 @@ static function (ExtendedCacheItemPoolInterface $cachePool) use ($cachePoolInsta
274237
}
275238

276239
/**
277-
* @param ConfigurationOption $config
240+
* @param ConfigurationOptionInterface $config
241+
* @throws PhpfastcacheInvalidArgumentException
278242
*/
279-
public static function setDefaultConfig(ConfigurationOption $config): void
243+
public static function setDefaultConfig(ConfigurationOptionInterface $config): void
280244
{
281245
if (is_subclass_of($config, ConfigurationOption::class)) {
282246
throw new PhpfastcacheInvalidArgumentException('Default configuration cannot be a child class of ConfigurationOption::class');
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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) <contact@geolim4.com>
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+
}

0 commit comments

Comments
 (0)