Skip to content

Commit 3a20116

Browse files
committed
Added Arangodb support + updated ConfigurationOption signature
1 parent 287beb8 commit 3a20116

File tree

17 files changed

+698
-253
lines changed

17 files changed

+698
-253
lines changed

.scrutinizer.yml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,7 @@
1010
#
1111

1212
before_commands:
13-
- "composer install --no-dev --prefer-source"
14-
- "composer require predis/predis \"~1.1.0\""
15-
- "composer require mongodb/mongodb \"^1.9\""
16-
- "composer require phpfastcache/phpssdb ~1.0.0\""
17-
- "composer require doctrine/couchdb dev-master"
13+
- "./bin/ci/scripts/install_dependencies.sh"
1814
build:
1915
nodes:
2016
analysis:

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
- __Helpers__
2222
- Deprecated `\Phpfastcache\Helper\CacheConditionalHelper`, use `\Phpfastcache\CacheContract` instead
2323
- The `\Phpfastcache\CacheContract` class is now also callable directly without calling `get()` method
24-
- __Options__
24+
- __Config/Options__
25+
- Updated `ConfigurationOption` which is no longer an `ArrayObject` class, therefore array-syntax is no longer available.
2526
- Renamed configuration entry `htaccess` to `autoHtaccessCreationEnabled` for files-based drivers.
2627
- Renamed `IOConfigurationOptionTrait::getHtaccess()` to `IOConfigurationOptionTrait::isAutoHtaccessCreationEnabled()`
2728
- Renamed `IOConfigurationOptionTrait::setHtaccess()` to `IOConfigurationOptionTrait::setAutoHtaccessCreationEnabled()`

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
[![Code Climate](https://codeclimate.com/github/PHPSocialNetwork/phpfastcache/badges/gpa.svg)](https://codeclimate.com/github/PHPSocialNetwork/phpfastcache) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/PHPSocialNetwork/phpfastcache/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/PHPSocialNetwork/phpfastcache/?branch=master) [![Build Status](https://travis-ci.com/PHPSocialNetwork/phpfastcache.svg?branch=master)](https://travis-ci.com/PHPSocialNetwork/phpfastcache) [![Semver compliant](https://img.shields.io/badge/Semver-2.0.0-yellow.svg?maxAge=86400)](https://semver.org/spec/v2.0.0.html) [![Patreon](https://img.shields.io/badge/Support%20us%20on-Patreon-f96854.svg?maxAge=86400)](https://www.patreon.com/geolim4)
33

44
#### :warning: Please note that the V9 is mostly a PHP 8 type aware update of Phpfastcache with some significant changes !
5-
> As the V9 is **relatively** not compatible with previous versions, please read carefully the [migration guide](./docs/migration/MigratingFromV7ToV8.md) to ensure you the smoothest migration possible.
5+
> As the V9 is **relatively** not compatible with previous versions, please read carefully the [migration guide](./docs/migration/MigratingFromV8ToV9.md) to ensure you the smoothest migration possible.
66
One of the biggest change is the configuration system which is now an object that replace the primitive array that we used to implement back then.
77
Also, please note that the V9 requires at least PHP 8 or higher to works properly.
88

@@ -30,7 +30,7 @@ The simplicity of abstraction: One class for many backend cache. You don't need
3030
\* Driver descriptions available in DOCS/DRIVERS.md
3131

3232
---------------------------
33-
Because caching does not means weaken your code
33+
Because caching does not mean weaken your code
3434
---------------------------
3535
Phpfastcache has been developed over the years with 3 main goals:
3636

bin/ci/scripts/install_dependencies.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
composer self-update
44
composer install
5-
composer require doctrine/couchdb:dev-master phpfastcache/phpssdb:~1.1.0 predis/predis:~1.1.0 mongodb/mongodb:^1.9
5+
composer require doctrine/couchdb:dev-master phpfastcache/phpssdb:~1.1.0 predis/predis:~1.1.0 mongodb/mongodb:^1.9 triagens/arangodb:^3.8

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@
5555
"predis/predis": "^1.1",
5656
"mongodb/mongodb": "^1.9",
5757
"phpfastcache/phpssdb": "~1.1.0",
58-
"doctrine/couchdb": "^dev-master#9eeb9e5"
58+
"doctrine/couchdb": "^dev-master#9eeb9e5",
59+
"triagens/arangodb": "^3.8"
5960
},
6061
"conflict": {
6162
"doctrine/couchdb": "<dev-master#9eeb9e5"

docs/migration/MigratingFromV8ToV9.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ 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+
### Updated `ConfigurationOption` which is no longer an `ArrayObject` class
11+
You can no longer use the following array-compatible syntax: `$config['yourKey'] = 'value'`\
12+
Use the object-notation syntax instead: `$config->setYourKey('value')`
13+
14+
However, this syntax is STILL valid through the configuration constructor\
15+
For the default config object: `new ConfigurationOption(['yourKey' => 'yourValue'])`\
16+
Or for specific config objects: `new \Phpfastcache\Drivers\Files\Config(['yourKey' => 'yourValue'])`\
17+
Finally, the config name you try to set MUST be recognized or an exception will be thrown.
18+
1019
### Deprecated `\Phpfastcache\Helper\CacheConditionalHelper`
1120
Use `\Phpfastcache\CacheContract` instead. See [Wiki](https://github.com/PHPSocialNetwork/phpfastcache/wiki/%5BV9%CB%96%5D-Cache-contract).
1221

lib/Phpfastcache/CacheManager.php

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,10 @@ public static function getInstance(string $driver, ?ConfigurationOptionInterface
147147

148148
if (\class_exists($driverClass)) {
149149
$configClass = $driverClass::getConfigClass();
150-
self::$instances[$instanceId] = new $driverClass(new $configClass($config->toArray()), $instanceId);
150+
if ($configClass !== $config::class) {
151+
$config = new $configClass($config->toArray());
152+
}
153+
self::$instances[$instanceId] = new $driverClass($config, $instanceId);
151154
self::$instances[$instanceId]->setEventManager(EventManager::getInstance());
152155
} else {
153156
throw new PhpfastcacheDriverNotFoundException(sprintf('The driver "%s" does not exists', $driver));
@@ -158,25 +161,18 @@ public static function getInstance(string $driver, ?ConfigurationOptionInterface
158161
}
159162

160163
/**
161-
* @param ConfigurationOptionInterface|null $config
164+
* @param ConfigurationOption|null $config
162165
* @return ConfigurationOption
163-
* @throws PhpfastcacheInvalidArgumentException
164166
*/
165-
protected static function validateConfig(?ConfigurationOptionInterface $config): ConfigurationOption
167+
protected static function validateConfig(?ConfigurationOption $config): ConfigurationOption
166168
{
167-
if ($config === null) {
168-
$config = self::getDefaultConfig();
169-
} elseif (!($config instanceof ConfigurationOption)) {
170-
throw new PhpfastcacheInvalidArgumentException(\sprintf('Unsupported config type: %s', \gettype($config)));
171-
}
172-
173-
return $config;
169+
return $config ?? self::getDefaultConfig();
174170
}
175171

176172
/**
177-
* @return ConfigurationOptionInterface
173+
* @return ConfigurationOption
178174
*/
179-
public static function getDefaultConfig(): ConfigurationOptionInterface
175+
public static function getDefaultConfig(): ConfigurationOption
180176
{
181177
return self::$config ?? self::$config = new ConfigurationOption();
182178
}
@@ -282,6 +278,9 @@ static function (ExtendedCacheItemPoolInterface $cachePool) use ($cachePoolInsta
282278
*/
283279
public static function setDefaultConfig(ConfigurationOption $config): void
284280
{
281+
if(is_subclass_of($config, ConfigurationOption::class)){
282+
throw new PhpfastcacheInvalidArgumentException('Default configuration cannot be a child class of ConfigurationOption::class');
283+
}
285284
self::$config = $config;
286285
}
287286

lib/Phpfastcache/Config/ConfigurationOption.php

Lines changed: 17 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,8 @@
1717

1818
use Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException;
1919
use Phpfastcache\Exceptions\PhpfastcacheInvalidConfigurationException;
20-
use Phpfastcache\Util\ArrayObject;
21-
use ReflectionException;
22-
use ReflectionMethod;
23-
use ReflectionNamedType;
24-
use ReflectionParameter;
25-
use TypeError;
26-
27-
class ConfigurationOption extends ArrayObject implements ConfigurationOptionInterface
20+
21+
class ConfigurationOption implements ConfigurationOptionInterface
2822
{
2923
protected bool $itemDetailedDate = false;
3024

@@ -53,36 +47,22 @@ class ConfigurationOption extends ArrayObject implements ConfigurationOptionInte
5347
protected ?object $superGlobalAccessor = null;
5448

5549
/**
56-
* @param $args
57-
* ArrayObject constructor.
5850
* @throws PhpfastcacheInvalidConfigurationException
59-
* @throws ReflectionException
6051
*/
61-
public function __construct(...$args)
52+
public function __construct(array $parameters = [])
6253
{
63-
parent::__construct(...$args);
64-
$array =& $this->getArray();
65-
66-
/**
67-
* Detect unwanted keys and throw an exception.
68-
* No more kidding now, it's 21th century.
69-
*/
70-
if (\array_diff_key($array, \get_object_vars($this))) {
71-
throw new PhpfastcacheInvalidConfigurationException(
72-
sprintf(
73-
'Invalid option(s) for the config %s: %s',
74-
static::class,
75-
\implode(', ', \array_keys(\array_diff_key($array, \get_object_vars($this))))
76-
)
77-
);
78-
}
79-
80-
foreach (\get_object_vars($this) as $property => $value) {
54+
foreach ($parameters as $configKey => $configVal) {
8155
try {
82-
if (\array_key_exists($property, $array)) {
83-
$this->$property = &$array[$property];
56+
if (\property_exists($this, $configKey)) {
57+
$this->{'set' . \ucfirst($configKey)}($configVal);
8458
} else {
85-
$array[$property] = &$this->$property;
59+
throw new PhpfastcacheInvalidConfigurationException(
60+
sprintf(
61+
'Invalid option for the config %s: %s',
62+
$this::class,
63+
$configKey
64+
)
65+
);
8666
}
8767
} catch (\TypeError $e) {
8868
throw new PhpfastcacheInvalidConfigurationException(
@@ -93,40 +73,11 @@ public function __construct(...$args)
9373
);
9474
}
9575
}
76+
}
9677

97-
foreach (\get_class_methods($this) as $method) {
98-
if (str_starts_with($method, 'set')) {
99-
$value = null;
100-
try {
101-
/**
102-
* We use property instead of getter
103-
* because of is/get conditions and
104-
* to allow us to retrieve the value
105-
* in catch statement bloc
106-
*/
107-
$value = $this->{\lcfirst(\substr($method, 3))};
108-
$this->{$method}($value);
109-
} catch (TypeError) {
110-
$typeHintGot = \get_debug_type($value);
111-
$reflectionMethod = new ReflectionMethod($this, $method);
112-
$parameter = $reflectionMethod->getParameters()[0] ?? null;
113-
$typeHintExpected = 'Unknown type';
114-
115-
if ($parameter instanceof ReflectionParameter && $parameter->getType() instanceof ReflectionNamedType) {
116-
$typeHintExpected = ($parameter->getType()->getName() === 'object' ? $parameter->getType() : $parameter->getType()->getName());
117-
}
118-
119-
throw new PhpfastcacheInvalidConfigurationException(
120-
\sprintf(
121-
'Invalid type hint found for "%s", expected "%s" got "%s"',
122-
\lcfirst(\substr($method, 3)),
123-
$typeHintExpected,
124-
$typeHintGot
125-
)
126-
);
127-
}
128-
}
129-
}
78+
public function toArray(): array
79+
{
80+
return get_object_vars($this);
13081
}
13182

13283
/**

lib/Phpfastcache/Config/ConfigurationOptionInterface.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,14 @@
2020
interface ConfigurationOptionInterface
2121
{
2222
/**
23-
* @param $args
24-
* ArrayObject constructor.
23+
* ConfigurationOptionInterface constructor.
2524
*/
26-
public function __construct(...$args);
25+
public function __construct(array $parameters = []);
26+
27+
/**
28+
* @return array
29+
*/
30+
public function toArray(): array;
2731

2832
/**
2933
* @param string $optionName

lib/Phpfastcache/Core/Pool/CacheItemPoolTrait.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public function getItem(string $key): ExtendedCacheItemInterface
146146
}
147147
$driverData = $this->driverUnwrapData($driverArray);
148148

149-
if ($this->getConfig()['preventCacheSlams']) {
149+
if ($this->getConfig()->isPreventCacheSlams()) {
150150
while ($driverData instanceof ItemBatch) {
151151
if ($driverData->getItemDate()->getTimestamp() + $this->getConfig()->getCacheSlamsTimeout() < \time()) {
152152
/**
@@ -208,7 +208,7 @@ public function getItem(string $key): ExtendedCacheItemInterface
208208
* Reset the Item
209209
*/
210210
$item->set(null)
211-
->expiresAfter(abs((int)$this->getConfig()['defaultTtl']))
211+
->expiresAfter(abs((int)$this->getConfig()->getDefaultTtl()))
212212
->setHit(false)
213213
->setTags([]);
214214
if ($this->getConfig()->isItemDetailedDate()) {
@@ -224,7 +224,7 @@ public function getItem(string $key): ExtendedCacheItemInterface
224224
$item->setHit(true);
225225
}
226226
} else {
227-
$item->expiresAfter(abs((int)$this->getConfig()['defaultTtl']));
227+
$item->expiresAfter(abs($this->getConfig()->getDefaultTtl()));
228228
}
229229
}
230230
} else {

0 commit comments

Comments
 (0)