Skip to content
This repository was archived by the owner on Apr 13, 2020. It is now read-only.

Commit 9046fdf

Browse files
committed
Fixed bin & global prefixes & and module check for debug (requires devel)
1 parent 5dd7a92 commit 9046fdf

7 files changed

+118
-46
lines changed

phpfastcache.module

+9-2
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,17 @@ function phpfastcache_help(
1111
RouteMatchInterface $route_match
1212
): TranslatableMarkup {
1313
if ($route_name === 'phpfastcache.admin_settings_form') {
14+
$help = <<<HELP
15+
<a href=":site_url" target="_blank">Phpfastcache</a> is a high-performance backend cache system.
16+
It is intended for use in speeding up dynamic web applications by alleviating database load.
17+
Check out the <a href=":github_url" target="_blank">Github support</a> for the Phpfastcache's library only.
18+
HELP;
19+
1420
return t(
15-
'<a href=":url" target="_blank">PhpFastCache</a> is a high-performance, distributed object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.',
21+
nl2br($help),
1622
[
17-
':url' => 'https://github.com/PHPSocialNetwork/phpfastcache',
23+
':site_url' => 'https://www.phpfastcache.com/',
24+
':github_url' => 'https://github.com/PHPSocialNetwork/phpfastcache',
1825
]
1926
);
2027
}

phpfastcache.services.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
services:
22
phpfastcache_event_subscriber:
33
class: Drupal\phpfastcache\EventSubscriber\PhpfastcacheSubscriber
4-
arguments: ['@cache.backend.phpfastcache']
4+
arguments: ['@cache.backend.phpfastcache', '@module_handler']
55
tags:
66
- {name: event_subscriber}
77
cache.backend.phpfastcache:

src/Cache/PhpfastcacheBackend.php

+14-7
Original file line numberDiff line numberDiff line change
@@ -223,13 +223,18 @@ protected function getDrupalCacheStdObject(): PhpfastcacheStoredObject {
223223
* An ASCII-encoded cache ID that is at most 255 characters long.
224224
* @see DatabaseBackend::normalizeCid()
225225
*/
226-
protected function normalizeCid($cid): string {
226+
protected function normalizeCid(string $cid): string {
227227
static $maxKeyLength = 64;
228228

229229
/**
230-
* Add PhpFastCache Prefix
230+
* Add Bin Prefix
231231
*/
232-
$cid = ($this->settings[ 'phpfastcache_prefix' ] ?: 'd8') . '-' . $cid;
232+
$cid = $this->binPrefix . $cid;
233+
234+
/**
235+
* Add Phpfastcache Prefix
236+
*/
237+
$cid = ($this->settings[ 'phpfastcache_prefix' ] ?: 'D8') . '.' . $cid;
233238

234239
/**
235240
* Nothing to do if the ID is a US ASCII string of 64 characters or less.
@@ -245,21 +250,23 @@ protected function normalizeCid($cid): string {
245250
*/
246251
$hash = Crypt::hashBase64($cid);
247252

253+
$hash = '_._' . trim(substr($hash, 3), '-_.');
254+
248255
if (!$cid_is_ascii) {
249256
return $this->replaceUnsupportedPsr6Characters($hash);
250257
}
251258

252259
return $this->replaceUnsupportedPsr6Characters(
253-
$this->binPrefix . substr($cid, 0, $maxKeyLength - \strlen($hash)) . $hash
260+
substr($cid, 0, $maxKeyLength - \strlen($hash)) . $hash
254261
);
255262
}
256263

257264
/**
258-
* @param $str
265+
* @param string $str
259266
*
260-
* @return mixed
267+
* @return string
261268
*/
262-
protected function replaceUnsupportedPsr6Characters($str) {
269+
protected function replaceUnsupportedPsr6Characters(string $str): string {
263270
return str_replace(
264271
['{', '}', '(', ')', '/', '\\', '@', ':'],
265272
'_',

src/Cache/PhpfastcacheBackendFactory.php

+26-13
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
/**
1111
* Class PhpFastCacheBackendFactory
12-
* @todo Uncamelize class name...
12+
*
1313
*/
1414
class PhpfastcacheBackendFactory implements CacheFactoryInterface {
1515

@@ -51,15 +51,22 @@ class PhpfastcacheBackendFactory implements CacheFactoryInterface {
5151
*/
5252
protected $connection;
5353

54+
/**
55+
* @var \Drupal\Core\Cache\CacheBackendInterface[]
56+
*/
57+
protected $cacheBins = [];
58+
5459
/**
5560
* PhpFastCacheBackendFactory constructor.
5661
*
5762
* @param \Drupal\Core\Database\Connection $connection
63+
*
5864
* @throws \Phpfastcache\Exceptions\PhpfastcacheDriverCheckException
5965
* @throws \Phpfastcache\Exceptions\PhpfastcacheDriverException
6066
* @throws \Phpfastcache\Exceptions\PhpfastcacheDriverNotFoundException
6167
* @throws \Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException
6268
* @throws \Phpfastcache\Exceptions\PhpfastcacheInvalidConfigurationException
69+
* @throws \Drupal\phpfastcache\Exceptions\CacheBackendException
6370
*/
6471
public function __construct(Connection $connection) {
6572
$this->backendClass = PhpfastcacheBackend::class;
@@ -97,15 +104,16 @@ public function __construct(Connection $connection) {
97104
* @throws \Phpfastcache\Exceptions\PhpfastcacheDriverNotFoundException
98105
* @throws \Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException
99106
* @throws \Phpfastcache\Exceptions\PhpfastcacheInvalidConfigurationException
107+
* @throws \Drupal\phpfastcache\Exceptions\CacheBackendException
100108
*/
101109
protected function getPhpfastcacheInstance(): ExtendedCacheItemPoolInterface {
102110
return PhpfastcacheInstanceBuilder::buildInstance($this->settings);
103111
}
104112

105113
/**
106-
* Get settings from database.
107-
* At this level of runtime execution
108-
* settings are not available yet.
114+
* Get settings straight from database.
115+
* At this level of runtime execution,
116+
* settings API is not yet available.
109117
*
110118
* @return array
111119
*/
@@ -126,19 +134,24 @@ public function getSettingsFromDatabase(): array {
126134
* @param string $bin
127135
* The cache bin for which the object is created.
128136
*
129-
* @return \Drupal\phpfastcache\Cache\PhpfastcacheBackend|\Drupal\phpfastcache\Cache\PhpfastcacheVoidBackend
137+
* @return \Drupal\Core\Cache\CacheBackendInterface
130138
* The cache backend object for the specified cache bin.
131139
* @throws CacheBackendException
132140
*/
133141
public function get($bin) {
134-
try{
135-
return new $this->backendClass($bin, $this->cachePool, $this->settings);
136-
}catch(\Throwable $e){
137-
throw new CacheBackendException(\sprintf(
138-
'Failed to create a cache backend instance for "%s" cache bin, got the following error: %s',
139-
$bin,
140-
$e->getMessage()
141-
));
142+
try {
143+
if (!isset($this->cacheBins[ $bin ])) {
144+
$this->cacheBins[ $bin ] = new $this->backendClass($bin, $this->cachePool, $this->settings);
145+
}
146+
return $this->cacheBins[ $bin ];
147+
} catch (\Throwable $e) {
148+
throw new CacheBackendException(
149+
\sprintf(
150+
'Failed to create a cache backend instance for "%s" cache bin, got the following error: %s',
151+
$bin,
152+
$e->getMessage()
153+
)
154+
);
142155
}
143156
}
144157
}

src/Cache/PhpfastcacheInstanceBuilder.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use Phpfastcache\CacheManager;
1515
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface;
1616
use Phpfastcache\Exceptions\PhpfastcacheDriverCheckException;
17-
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;
1817

1918
class PhpfastcacheInstanceBuilder {
2019

@@ -53,9 +52,11 @@ public static function buildInstance($settings): ExtendedCacheItemPoolInterface
5352
);
5453
} catch (PhpfastcacheDriverCheckException $e) {
5554
$error = "The '{$driverName}' driver failed to initialize with the following error: {$e->getMessage()} line {$e->getLine()} in {$e->getFile()}.";
55+
CacheManager::clearInstances();
5656
$instance = CacheManager::getInstance('Devnull');
5757
} catch (\Throwable $e) {
5858
$error = "The '{$driverName}' driver encountered the following error: {$e->getMessage()} line {$e->getLine()} in {$e->getFile()}.";
59+
CacheManager::clearInstances();
5960
$instance = CacheManager::getInstance('Devnull');
6061
}
6162

src/EventSubscriber/PhpfastcacheSubscriber.php

+14-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Drupal\Component\Render\FormattableMarkup;
66
use Drupal\Core\Cache\CacheFactoryInterface;
7+
use Drupal\Core\Extension\ModuleHandler;
78
use Drupal\Core\Utility\Error;
89
use Drupal\phpfastcache\Cache\PhpfastcacheBackendFactory;
910
use Phpfastcache\Exceptions\PhpfastcacheRootException;
@@ -26,8 +27,14 @@ class PhpfastcacheSubscriber implements EventSubscriberInterface {
2627
*/
2728
protected $cacheFactory;
2829

29-
public function __construct(CacheFactoryInterface $cacheFactory) {
30+
/**
31+
* @var \Drupal\Core\Extension\ModuleHandler
32+
*/
33+
protected $moduleHandler;
34+
35+
public function __construct(CacheFactoryInterface $cacheFactory, ModuleHandler $moduleHandler) {
3036
$this->cacheFactory = $cacheFactory;
37+
$this->moduleHandler = $moduleHandler;
3138
}
3239

3340
/**
@@ -36,12 +43,15 @@ public function __construct(CacheFactoryInterface $cacheFactory) {
3643
public function onPhpfastcacheException(GetResponseForExceptionEvent $event) {
3744
/**
3845
* Make sure that we handle the exceptions
39-
* that we should about
46+
* that we are allowed to manage
4047
*/
4148
if ($this->cacheFactory instanceof PhpfastcacheBackendFactory) {
4249
$settings = $this->cacheFactory->getSettingsFromDatabase();
43-
$culpritDriver = \ucfirst($settings[ 'phpfastcache_default_driver' ]);
44-
if ($settings[ 'phpfastcache_env' ] === PhpfastcacheBackendFactory::ENV_DEV && $event->getException() instanceof PhpfastcacheRootException) {
50+
if ($settings[ 'phpfastcache_env' ] === PhpfastcacheBackendFactory::ENV_DEV
51+
&& $event->getException() instanceof PhpfastcacheRootException
52+
&& $this->moduleHandler->moduleExists('devel')
53+
) {
54+
$culpritDriver = \ucfirst($settings[ 'phpfastcache_default_driver' ]);
4555
/**
4656
* Preparing message args
4757
*/

src/Form/PhpFastCacheAdminSettingsForm.php

+52-18
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33
namespace Drupal\phpfastcache\Form;
44

55
use Drupal\Component\Utility\Random;
6+
use Drupal\Core\Config\ConfigFactoryInterface;
7+
use Drupal\Core\Extension\ModuleHandler;
68
use Drupal\Core\Form\ConfigFormBase;
79
use Drupal\Core\Form\FormStateInterface;
810
use Drupal\phpfastcache\Cache\PhpfastcacheBackendFactory;
911
use Drupal\phpfastcache\Form\Fields\PhpfastcacheAdminFieldClassMap;
1012
use Drupal\phpfastcache\CacheManager;
1113
use Phpfastcache\Api as PhpfastcacheApi;
1214
use Phpfastcache\Exceptions\PhpfastcacheDriverCheckException;
15+
use Symfony\Component\DependencyInjection\ContainerInterface;
1316

1417
/**
1518
* Configure phpfastcache settings for this site.
@@ -19,6 +22,32 @@ class PhpFastCacheAdminSettingsForm extends ConfigFormBase {
1922

2023
const PREFIX_REGEXP = '^\w*$';
2124

25+
/**
26+
* @var \Drupal\Core\Extension\ModuleHandler
27+
*/
28+
protected $moduleHandler;
29+
30+
/**
31+
* PhpFastCacheAdminSettingsForm constructor.
32+
*
33+
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
34+
* @param \Drupal\Core\Extension\ModuleHandler $moduleHandler
35+
*/
36+
public function __construct(ConfigFactoryInterface $config_factory, ModuleHandler $moduleHandler) {
37+
parent::__construct($config_factory);
38+
$this->moduleHandler = $moduleHandler;
39+
}
40+
41+
/**
42+
* {@inheritdoc}
43+
*/
44+
public static function create(ContainerInterface $container) {
45+
return new static(
46+
$container->get('config.factory'),
47+
$container->get('module_handler')
48+
);
49+
}
50+
2251
/**
2352
* {@inheritdoc}
2453
*/
@@ -94,27 +123,31 @@ public function buildForm(array $form, FormStateInterface $form_state) {
94123
],
95124
];
96125

97-
// @todo conditional if webprofiler module
98-
$form[ 'general' ][ 'phpfastcache_settings_wrapper' ][ 'phpfastcache_env' ] = [
99-
'#default_value' => (string) $config->get('phpfastcache_env'),
100-
'#description' => $this->t(
101-
'<strong>Production</strong>: Will displays minimal information in case of failure.<br />
126+
if($this->moduleHandler->moduleExists('devel')){
127+
$form[ 'general' ][ 'phpfastcache_settings_wrapper' ][ 'phpfastcache_env' ] = [
128+
'#default_value' => (string) $config->get('phpfastcache_env'),
129+
'#description' => $this->t(
130+
'<strong>Production</strong>: Will displays minimal information in case of failure.<br />
102131
<strong>Development</strong>: Will displays very verbose information in case of failure.'
103-
),
104-
'#required' => TRUE,
105-
'#options' => [
106-
PhpfastcacheBackendFactory::ENV_DEV => t('Development'),
107-
PhpfastcacheBackendFactory::ENV_PROD => t('Production'),
108-
],
109-
'#title' => $this->t('PhpFastCache environment'),
110-
'#type' => 'select',
111-
];
132+
),
133+
'#required' => TRUE,
134+
'#options' => [
135+
PhpfastcacheBackendFactory::ENV_DEV => t('Development'),
136+
PhpfastcacheBackendFactory::ENV_PROD => t('Production'),
137+
],
138+
'#title' => $this->t('PhpFastCache environment'),
139+
'#type' => 'select',
140+
];
141+
}else if((string) $config->get('phpfastcache_env') === PhpfastcacheBackendFactory::ENV_DEV){
142+
$config->set('phpfastcache_env', PhpfastcacheBackendFactory::ENV_PROD)
143+
->save();
144+
}
112145

113146
$form[ 'general' ][ 'phpfastcache_settings_wrapper' ][ 'phpfastcache_prefix' ] = [
114147
'#default_value' => (string) ($config->get('phpfastcache_prefix') ?: $randomService->name(6, TRUE)),
115148
'#description' => $this->t(
116149
'The cache keyspace prefix that will be used to identify this website.
117-
This value length <strong>MUST</strong> be up to 8 chars and 4 chars minimum. <br />
150+
This value length <strong>MUST</strong> be up to 8 chars and 2 chars minimum. <br />
118151
This value <strong>MUST</strong> be unique depending your other Drupal installations on this cache backend. <br />
119152
This value <strong>MUST</strong> be alpha-numeric (' . self::PREFIX_REGEXP . ')'
120153
),
@@ -205,7 +238,6 @@ public function validateForm(array &$form, FormStateInterface $form_state) {
205238
$form_state->setError($form, 'The driver chosen is unavailable !');
206239
}
207240

208-
209241
/**
210242
* Field Validation: phpfastcache_prefix
211243
*/
@@ -228,11 +260,13 @@ public function validateForm(array &$form, FormStateInterface $form_state) {
228260
public function submitForm(array &$form, FormStateInterface $form_state) {
229261
$config = $this->config('phpfastcache.settings');
230262
$config->set('phpfastcache_enabled', (bool) $form_state->getValue('phpfastcache_enabled'))
231-
->set('phpfastcache_env', (string) $form_state->getValue('phpfastcache_env'))
232263
->set('phpfastcache_prefix', (string) $form_state->getValue('phpfastcache_prefix'))
233264
->set('phpfastcache_default_ttl', (int) $form_state->getValue('phpfastcache_default_ttl'))
234265
->set('phpfastcache_default_driver', (string) $form_state->getValue('phpfastcache_default_driver'));
235266

267+
if($this->moduleHandler->moduleExists('devel')){
268+
$config->set('phpfastcache_env', (string) $form_state->getValue('phpfastcache_env'));
269+
}
236270
/*****************
237271
* Drivers settings
238272
*****************/
@@ -260,7 +294,7 @@ protected function isAvailableDriver(string $driverName): bool {
260294
* using memcache and memcached or
261295
* redis and predis together for example
262296
*/
263-
@CacheManager::getInstance($driverName);
297+
$i = @CacheManager::getInstance($driverName);
264298
return TRUE;
265299
} catch (PhpfastcacheDriverCheckException $e) {
266300
return FALSE;

0 commit comments

Comments
 (0)