Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Apr 13, 2020. It is now read-only.

Commit bc6a79f

Browse files
committed
Added environment settings
1 parent 61e15ea commit bc6a79f

File tree

4 files changed

+57
-28
lines changed

4 files changed

+57
-28
lines changed

‎config/install/phpfastcache.settings.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
phpfastcache_enabled: false
2+
phpfastcache_env: prod
23
phpfastcache_default_ttl: 900
34
phpfastcache_htaccess: true
45
phpfastcache_default_driver: files

‎src/Cache/PhpFastCacheBackend.php‎

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@
33

44
use Drupal\Component\Utility\Crypt;
55
use Drupal\Core\Cache\Cache;
6-
use Drupal\Core\Cache\CacheTagsChecksumInterface;
7-
use phpFastCache\Cache\ExtendedCacheItemPoolInterface;
8-
use phpFastCache\CacheManager;
96
use Drupal\Core\Cache\DatabaseBackend;
7+
use Drupal\Core\Cache\CacheBackendInterface;
8+
use phpFastCache\Cache\ExtendedCacheItemPoolInterface;
109

1110

1211
/**
1312
* Class PhpFastCacheService
1413
*/
15-
class PhpFastCacheBackend implements \Drupal\Core\Cache\CacheBackendInterface
14+
class PhpFastCacheBackend implements CacheBackendInterface
1615
{
1716
/**
1817
* @var ExtendedCacheItemPoolInterface
@@ -34,23 +33,17 @@ class PhpFastCacheBackend implements \Drupal\Core\Cache\CacheBackendInterface
3433
*/
3534
protected $binPrefix;
3635

36+
/**
37+
* Constructs a new PhpFastCacheBackend instance.
38+
*
39+
* @param $bin string
40+
* The name of the cache bin.
41+
* @param ExtendedCacheItemPoolInterface $cachePool
42+
*/
3743
public function __construct($bin, $cachePool)
3844
{
39-
/**
40-
* Constructs a new ApcuBackend instance.
41-
*
42-
* @param string $bin
43-
* The name of the cache bin.
44-
* @param string $site_prefix
45-
* The prefix to use for all keys in the storage that belong to this site.
46-
* @param \Drupal\Core\Cache\CacheTagsChecksumInterface $checksum_provider
47-
* The cache tags checksum provider.
48-
*/
49-
//require_once __DIR__ . '/../../phpfastcache-php/src/autoload.php';
50-
5145
$this->cachePool = $cachePool;
5246
$this->bin = $bin;
53-
//$this->checksumProvider = $checksum_provider;
5447
$this->binPrefix = 'pfc::' . $this->bin . '::';
5548
}
5649

‎src/Cache/PhpFastCacheBackendFactory.php‎

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,22 @@
22
namespace Drupal\phpfastcache\Cache;
33

44
use Drupal\Core\Cache\CacheFactoryInterface;
5+
use Drupal\Core\Database\Connection;
56
use phpFastCache\Cache\ExtendedCacheItemPoolInterface;
67
use phpFastCache\CacheManager;
7-
use Drupal\Core\Database\Connection;
8+
use phpFastCache\Exceptions\phpFastCacheCoreException;
89
use phpFastCache\Exceptions\phpFastCacheDriverCheckException;
10+
use Drupal\phpfastcache\Cache\PhpFastCacheVoidBackend;
11+
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;
912

1013
/**
1114
* Class PhpFastCacheService
1215
*/
1316
class PhpFastCacheBackendFactory implements CacheFactoryInterface
1417
{
18+
const ENV_DEV = 'dev';
19+
const ENV_PROD = 'prod';
20+
1521
/**
1622
* Cache collector for debug purposes
1723
* @var array
@@ -24,6 +30,7 @@ class PhpFastCacheBackendFactory implements CacheFactoryInterface
2430
protected $cachePool;
2531

2632
/**
33+
*
2734
* The PhpFastCache backend class to use.
2835
*
2936
* @var string
@@ -74,17 +81,22 @@ public function __construct(Connection $connection)
7481
*
7582
* Let's dying miserably by showing a simple but efficient message
7683
*/
77-
die('PhpFastCache is not enabled, please go to <strong>admin/config/development/phpfastcache</strong> then configure PhpFastCache.');
84+
if($this->settings['phpfastcache_env'] === self::ENV_DEV){
85+
die('PhpFastCache is not enabled, please go to <strong>admin/config/development/phpfastcache</strong> then configure PhpFastCache or comment out the cache backend override in settings.php.');
86+
}else{
87+
die('PhpFastCache is not enabled.');
88+
}
7889
}
7990
else
8091
{
81-
$this->backendClass = 'Drupal\phpfastcache\Cache\PhpFastCacheVoidBackend';
92+
$this->backendClass = PhpFastCacheVoidBackend::class;
8293
}
8394
}
8495
}
8596

8697
/**
8798
* @return \phpFastCache\Cache\ExtendedCacheItemPoolInterface
99+
* @throws ServiceUnavailableHttpException
88100
*/
89101
protected function getPhpFastCacheInstance()
90102
{
@@ -182,13 +194,23 @@ protected function getPhpFastCacheInstance()
182194
* is not recognized set it to Devnull
183195
*/
184196
default:
185-
$instance = CacheManager::getInstance('Devnull');
186-
\Drupal::logger('cache')->critical("Unable to retrieve a valid driver (got '{$driverName}'). Drupal is now working in degraded mode");
197+
$error = "Unable to retrieve a valid driver (got '{$driverName}').";
198+
if($this->settings['phpfastcache_env'] === self::ENV_DEV){
199+
throw new ServiceUnavailableHttpException(60, $error);
200+
}else{
201+
$instance = CacheManager::getInstance('Devnull');
202+
\Drupal::logger('cache')->critical("{$error} Drupal is now working in degraded mode");
203+
}
187204
break;
188205
}
189206
}catch(phpFastCacheDriverCheckException $e){
190-
$instance = CacheManager::getInstance('Devnull');
191-
\Drupal::logger('cache')->critical("The Driver '{$driverName}' failed to initialize with the following error {$e->getMessage()}. Drupal is now working in degraded mode");
207+
$error = "The Driver '{$driverName}' failed to initialize with the following error {$e->getMessage()}.";
208+
if($this->settings['phpfastcache_env'] === self::ENV_DEV){
209+
throw new ServiceUnavailableHttpException(60, $error, $e);
210+
}else{
211+
$instance = CacheManager::getInstance('Devnull');
212+
\Drupal::logger('cache')->critical("{$error} Drupal is now working in degraded mode");
213+
}
192214
}
193215

194216
return $instance;

‎src/Form/PhpFastCacheAdminSettingsForm.php‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
use Drupal\Core\Config\Config;
66
use Drupal\Core\Form\ConfigFormBase;
77
use Drupal\Core\Form\FormStateInterface;
8+
use Drupal\phpfastcache\Cache\PhpFastCacheBackendFactory;
89
use phpFastCache\CacheManager;
910
use phpFastCache\Exceptions\phpFastCacheDriverCheckException;
11+
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;
1012

1113
/**
1214
* Configure phpfastcache settings for this site.
@@ -81,6 +83,19 @@ public function buildForm(array $form, FormStateInterface $form_state) {
8183
],
8284
];
8385

86+
$form[ 'general' ][ 'phpfastcache_settings_wrapper' ][ 'phpfastcache_env' ] = [
87+
'#default_value' => (string) $config->get('phpfastcache_env'),
88+
'#description' => $this->t('<strong>Production</strong>: Will displays minimal information in case of failure.<br />
89+
<strong>Development</strong>: Will displays very verbose information in case of failure.'),
90+
'#required' => TRUE,
91+
'#options' => [
92+
PhpFastCacheBackendFactory::ENV_DEV => t('Production'),
93+
PhpFastCacheBackendFactory::ENV_PROD => t('Development'),
94+
],
95+
'#title' => $this->t('PhpFastCache environment'),
96+
'#type' => 'select',
97+
];
98+
8499
$form[ 'general' ][ 'phpfastcache_settings_wrapper' ][ 'phpfastcache_default_ttl' ] = [
85100
'#default_value' => (int) $config->get('phpfastcache_default_ttl'),
86101
'#description' => $this->t('Enable or disable all the PhpFastCache components'),
@@ -342,15 +357,12 @@ public function buildForm(array $form, FormStateInterface $form_state) {
342357
* {@inheritdoc}
343358
*/
344359
public function validateForm(array &$form, FormStateInterface $form_state) {
345-
346360
try{
347361
CacheManager::getInstance($form_state->getValue('phpfastcache_default_driver'), ['ignoreSymfonyNotice' => true]);
348362
}catch(phpFastCacheDriverCheckException $e){
349363
$form_state->setError($form, 'This driver is not usable at the moment, error code: ' . $e->getMessage());
350364
}catch (\Exception $e){
351-
/**
352-
* @todo Catch others exception here
353-
*/
365+
$form_state->setError($form, 'This driver has encountered an error: ' . $e->getMessage());
354366
}
355367
}
356368

@@ -360,6 +372,7 @@ public function validateForm(array &$form, FormStateInterface $form_state) {
360372
public function submitForm(array &$form, FormStateInterface $form_state) {
361373
$config = $this->config('phpfastcache.settings');
362374
$config->set('phpfastcache_enabled', (bool) $form_state->getValue('phpfastcache_enabled'))
375+
->set('phpfastcache_env', (string) $form_state->getValue('phpfastcache_env'))
363376
->set('phpfastcache_default_ttl', (int) $form_state->getValue('phpfastcache_default_ttl'))
364377
->set('phpfastcache_htaccess', (bool) $form_state->getValue('phpfastcache_htaccess'))
365378
->set('phpfastcache_default_driver', (string) $form_state->getValue('phpfastcache_default_driver'))

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /