Skip to content

Commit 4e6f3f7

Browse files
committed
Finished ConfigurationOption object implementation
1 parent 2aa6c87 commit 4e6f3f7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+1370
-319
lines changed

docs/migration/MigratingFromV6ToV7.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,14 @@ CacheManager::getInstanceById($instanceId);
3939
Some configuration names were still using "snake case" format:
4040
- default_chmod
4141
- compress_data
42+
- sasl_user
4243
- ...
4344

4445
#### :alarm_clock: Now:
4546
These configuration names are now "Camelized":
4647
- defaultChmod
4748
- compressData
49+
- saslUser
4850
- ...
4951

5052
As of the V7 there's no "snake case" names left.

src/autoload.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
}
5656
});
5757

58-
if ((!\defined('PFC_IGNORE_COMPOSER_WARNING') || !PFC_IGNORE_COMPOSER_WARNING) && class_exists('Composer\Autoload\ClassLoader')) {
58+
if ((!\defined('PFC_IGNORE_COMPOSER_WARNING') || !PFC_IGNORE_COMPOSER_WARNING) && \class_exists('Composer\Autoload\ClassLoader')) {
5959
trigger_error('Your project already makes use of Composer. You SHOULD use the composer dependency "phpfastcache/phpfastcache" instead of hard-autoloading.',
6060
E_USER_WARNING);
6161
}

src/phpFastCache/CacheManager.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public static function getInstance($driver = 'auto', $config = null, $instanceId
120120
$badPracticeOmeter[ $driver ] = 1;
121121
$driverClass = self::getNamespacePath() . $driver . '\Driver';
122122
try {
123-
if (class_exists($driverClass)) {
123+
if (\class_exists($driverClass)) {
124124
$configClass = $driverClass::getConfigClass();
125125
self::$instances[ $instance ] = new $driverClass(new $configClass($config->toArray()), $instance);
126126
self::$instances[ $instance ]->setEventManager(EventManager::getInstance());
@@ -130,8 +130,10 @@ public static function getInstance($driver = 'auto', $config = null, $instanceId
130130
} catch (phpFastCacheDriverCheckException $e) {
131131
if ($config->getFallback()) {
132132
try {
133+
133134
$fallback = $config->getFallback();
134135
$config->setFallback('');
136+
trigger_error(sprintf('The "%s" driver is unavailable at the moment, the fallback driver "%s" has been used instead.', $driver, $fallback), E_USER_WARNING);
135137
return self::getInstance($fallback, $config);
136138
} catch (phpFastCacheInvalidArgumentException $e) {
137139
throw new phpFastCacheInvalidConfigurationException('Invalid fallback driver configuration', 0, $e);

src/phpFastCache/Config/ConfigurationOption.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class ConfigurationOption extends ArrayObject
3737
/**
3838
* @var string|Callable
3939
*/
40-
protected $defaultKeyHashFunction = '';
40+
protected $defaultKeyHashFunction = 'md5';
4141

4242
/**
4343
* @var int
@@ -52,7 +52,7 @@ class ConfigurationOption extends ArrayObject
5252
/**
5353
* @var string
5454
*/
55-
protected $fallback;
55+
protected $fallback = '';
5656

5757
/**
5858
* @var int
@@ -107,6 +107,34 @@ public function __construct(...$args)
107107
$array[ $property ] = &$this->$property;
108108
}
109109
}
110+
111+
foreach (get_class_methods($this) as $method) {
112+
if(strpos($method, 'set') === 0){
113+
$value = null;
114+
try{
115+
/**
116+
* We use property instead of getter
117+
* because of is/get conditions and
118+
* to allow us to retrieve the value
119+
* in catch statement bloc
120+
*/
121+
$value = $this->{lcfirst(substr($method, 3))};
122+
$this->{$method}($value);
123+
}catch(\TypeError $e){
124+
$typeHintGot = \is_object($value) ? \get_class($value) : \gettype($value);
125+
$reflectionMethod = new \ReflectionMethod($this, $method);
126+
$parameter = $reflectionMethod->getParameters()[0] ?? null;
127+
$typeHintExpected = ($parameter instanceof \ReflectionParameter ? ($parameter->getType() === 'object' ? $parameter->getClass() : $parameter->getType()) : 'Unknown type');
128+
129+
throw new phpFastCacheInvalidConfigurationException(sprintf(
130+
'Invalid type hint found for "%s", expected "%s" got "%s"',
131+
lcfirst(substr($method, 3)),
132+
$typeHintExpected,
133+
$typeHintGot
134+
));
135+
}
136+
}
137+
}
110138
}
111139

112140
/**

src/phpFastCache/Core/Pool/CacheItemPoolTrait.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@
2323
use phpFastCache\Exceptions\{
2424
phpFastCacheInvalidArgumentException, phpFastCacheCoreException, phpFastCacheLogicException
2525
};
26+
use phpFastCache\Config\ConfigurationOption;
2627
use Psr\Cache\CacheItemInterface;
2728

2829
/**
2930
* Trait StandardPsr6StructureTrait
3031
* @package phpFastCache\Core
31-
* @property array $config The config array
32+
* @property ConfigurationOption $config The config array
3233
*/
3334
trait CacheItemPoolTrait
3435
{

src/phpFastCache/Core/Pool/DriverBaseTrait.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,13 @@ public function __construct(ConfigurationOption $config, $instanceId)
7070

7171
if (!$this->driverCheck()) {
7272
throw new phpFastCacheDriverCheckException(sprintf(self::DRIVER_CHECK_FAILURE, $this->getDriverName()));
73-
} else {
74-
$this->driverConnect();
7573
}
74+
75+
$this->driverConnect();
7676
}
7777

7878
/**
79-
* @param ConfigurationOption $config_name
79+
* @param ConfigurationOption $config
8080
*/
8181
public function setConfig(ConfigurationOption $config)
8282
{
@@ -150,7 +150,7 @@ protected function isPHPModule(): bool
150150
*/
151151
protected function driverExists($class): bool
152152
{
153-
return class_exists("\\phpFastCache\\Drivers\\{$class}");
153+
return \class_exists("\\phpFastCache\\Drivers\\{$class}");
154154
}
155155

156156

@@ -166,7 +166,7 @@ public function driverPreWrap(ExtendedCacheItemInterface $item): array
166166
self::DRIVER_EDATE_WRAPPER_INDEX => $item->getExpirationDate(),
167167
];
168168

169-
if ($this->config[ 'itemDetailedDate' ]) {
169+
if ($this->config->getOption('itemDetailedDate')) {
170170
$wrap[ self::DRIVER_MDATE_WRAPPER_INDEX ] = new \DateTime();
171171
/**
172172
* If the creation date exists
@@ -387,7 +387,7 @@ public static function getValidOptions(): array
387387
public static function getConfigClass(): string
388388
{
389389
$localConfigClass = substr(static::class, 0, strrpos(static::class, '\\')) . '\Config';
390-
if(class_exists($localConfigClass) && is_a($localConfigClass, ConfigurationOption::class, true)){
390+
if(\class_exists($localConfigClass) && is_a($localConfigClass, ConfigurationOption::class, true)){
391391
return $localConfigClass;
392392
}
393393
return ConfigurationOption::class;

src/phpFastCache/Core/Pool/IO/IOHelperTrait.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function getPath($readonly = false): string
5252
* Calculate the security key
5353
*/
5454
{
55-
$securityKey = \array_key_exists('securityKey', $this->config) ? $this->config[ 'securityKey' ] : '';
55+
$securityKey = \array_key_exists('securityKey', $this->config) ? $this->config->getOption('securityKey') : '';
5656
if (!$securityKey || $securityKey === 'auto') {
5757
if (isset($_SERVER[ 'HTTP_HOST' ])) {
5858
$securityKey = preg_replace('/^www./', '', \strtolower(\str_replace(':', '_', $_SERVER[ 'HTTP_HOST' ])));
@@ -74,10 +74,10 @@ public function getPath($readonly = false): string
7474
*/
7575
$tmp_dir = rtrim($tmp_dir, '/') . DIRECTORY_SEPARATOR;
7676

77-
if (empty($this->config[ 'path' ]) || !\is_string($this->config[ 'path' ])) {
77+
if (empty($this->config->getOption('path')) || !\is_string($this->config->getOption('path'))) {
7878
$path = $tmp_dir;
7979
} else {
80-
$path = rtrim($this->config[ 'path' ], '/') . DIRECTORY_SEPARATOR;
80+
$path = rtrim($this->config->getOption('path'), '/') . DIRECTORY_SEPARATOR;
8181
}
8282

8383
$path_suffix = $securityKey . DIRECTORY_SEPARATOR . $this->getDriverName();
@@ -92,7 +92,7 @@ public function getPath($readonly = false): string
9292
* return the temp dir
9393
*/
9494
if ($readonly === true) {
95-
if ($this->config[ 'autoTmpFallback' ] && (!@\file_exists($full_path) || !@\is_writable($full_path))) {
95+
if ($this->config->getOption('autoTmpFallback') && (!@\file_exists($full_path) || !@\is_writable($full_path))) {
9696
return $full_path_tmp;
9797
}
9898
return $full_path;
@@ -101,7 +101,7 @@ public function getPath($readonly = false): string
101101
if (!@\file_exists($full_path)) {
102102
@mkdir($full_path, $this->getDefaultChmod(), true);
103103
} else if (!@\is_writable($full_path)) {
104-
if (!@chmod($full_path, $this->getDefaultChmod()) && $this->config[ 'autoTmpFallback' ]) {
104+
if (!@chmod($full_path, $this->getDefaultChmod()) && $this->config->getOption('autoTmpFallback')) {
105105
/**
106106
* Switch back to tmp dir
107107
* again if the path is not writable
@@ -123,7 +123,7 @@ public function getPath($readonly = false): string
123123
}
124124

125125
$this->tmp[ $full_path_hash ] = $full_path;
126-
$this->htaccessGen($full_path, \array_key_exists('htaccess', $this->config) ? $this->config[ 'htaccess' ] : false);
126+
$this->htaccessGen($full_path, \array_key_exists('htaccess', $this->config) ? $this->config->getOption('htaccess') : false);
127127
}
128128
}
129129

@@ -160,7 +160,7 @@ protected function getFilePath($keyword, $skip = false): string
160160
}
161161
}
162162

163-
return $path . '/' . $filename . '.' . $this->config[ 'cacheFileExtension' ];
163+
return $path . '/' . $filename . '.' . $this->config->getOption('cacheFileExtension');
164164
}
165165

166166

@@ -178,10 +178,10 @@ protected function encodeFilename($keyword): string
178178
*/
179179
protected function getDefaultChmod(): int
180180
{
181-
if (!isset($this->config[ 'default_chmod' ]) || $this->config[ 'default_chmod' ] == '' || is_null($this->config[ 'default_chmod' ])) {
181+
if ($this->config->getOption('default_chmod') !== null || $this->config->getOption('default_chmod') == '' || is_null($this->config->getOption('default_chmod'))) {
182182
return 0777;
183183
} else {
184-
return $this->config[ 'default_chmod' ];
184+
return $this->config->getOption('default_chmod');
185185
}
186186
}
187187

src/phpFastCache/Drivers/Apc/Driver.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
/**
2626
* Class Driver
2727
* @package phpFastCache\Drivers
28+
* @property Config $config Config object
2829
*/
2930
class Driver implements ExtendedCacheItemPoolInterface
3031
{
@@ -136,7 +137,7 @@ public function getStats(): DriverStatistic
136137

137138
return (new DriverStatistic())
138139
->setData(\implode(', ', \array_keys($this->itemInstances)))
139-
->setInfo(sprintf("The APC cache is up since %s, and have %d item(s) in cache.\n For more information see RawData.", $date->format(DATE_RFC2822),
140+
->setInfo(\sprintf("The APC cache is up since %s, and have %d item(s) in cache.\n For more information see RawData.", $date->format(DATE_RFC2822),
140141
$stats[ 'num_entries' ]))
141142
->setRawData($stats)
142143
->setSize($stats[ 'mem_size' ]);

src/phpFastCache/Drivers/Apcu/Driver.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
/**
2626
* Class Driver
2727
* @package phpFastCache\Drivers
28+
* @property Config $config Config object
2829
*/
2930
class Driver implements ExtendedCacheItemPoolInterface
3031
{
@@ -35,7 +36,7 @@ class Driver implements ExtendedCacheItemPoolInterface
3536
*/
3637
public function driverCheck(): bool
3738
{
38-
return extension_loaded('apcu') && ini_get('apc.enabled');
39+
return \extension_loaded('apcu') && ini_get('apc.enabled');
3940
}
4041

4142
/**
@@ -120,7 +121,7 @@ public function getStats(): DriverStatistic
120121

121122
return (new DriverStatistic())
122123
->setData(\implode(', ', \array_keys($this->itemInstances)))
123-
->setInfo(sprintf("The APCU cache is up since %s, and have %d item(s) in cache.\n For more information see RawData.", $date->format(DATE_RFC2822),
124+
->setInfo(\sprintf("The APCU cache is up since %s, and have %d item(s) in cache.\n For more information see RawData.", $date->format(DATE_RFC2822),
124125
$stats[ 'num_entries' ]))
125126
->setRawData($stats)
126127
->setSize($stats[ 'mem_size' ]);

0 commit comments

Comments
 (0)