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

Commit ca06b21

Browse files
committed
Added VoidBackend if PhpFastCache is disabled
1 parent 4a0b99f commit ca06b21

File tree

4 files changed

+206
-8
lines changed

4 files changed

+206
-8
lines changed

phpfastcache.services.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ services:
55
- {name: event_subscriber}
66
cache.backend.phpfastcache:
77
class: Drupal\phpfastcache\Cache\PhpFastCacheBackendFactory
8-
arguments: ['@settings', '@cache_tags.invalidator.checksum']
8+
arguments: ['@database', '@cache_tags.invalidator.checksum']

src/Cache/PhpFastCacheBackend.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ class PhpFastCacheBackend implements \Drupal\Core\Cache\CacheBackendInterface
4343

4444
public function __construct($bin, $cachePool, CacheTagsChecksumInterface $checksum_provider)
4545
{
46-
//var_dump(func_get_args());exit;
4746
/**
4847
* Constructs a new ApcuBackend instance.
4948
*
@@ -55,6 +54,7 @@ public function __construct($bin, $cachePool, CacheTagsChecksumInterface $checks
5554
* The cache tags checksum provider.
5655
*/
5756
require_once __DIR__ . '/../../phpfastcache-php/src/autoload.php';
57+
5858
$this->cachePool = $cachePool;
5959
$this->bin = $bin;
6060
$this->checksumProvider = $checksum_provider;

src/Cache/PhpFastCacheBackendFactory.php

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
<?php
22
namespace Drupal\phpfastcache\Cache;
33

4-
use Drupal\Component\Utility\Crypt;
5-
use Drupal\Core\Cache\Cache;
64
use Drupal\Core\Cache\CacheTagsChecksumInterface;
75
use phpFastCache\Cache\ExtendedCacheItemPoolInterface;
86
use phpFastCache\CacheManager;
9-
use Drupal\Core\Cache\DatabaseBackend;
10-
7+
use Drupal\Core\Database\Connection;
118

129
/**
1310
* Class PhpFastCacheService
@@ -40,9 +37,20 @@ class PhpFastCacheBackendFactory implements \Drupal\Core\Cache\CacheFactoryInter
4037
*/
4138
protected $settings;
4239

43-
public function __construct($settings, CacheTagsChecksumInterface $checksum_provider)
40+
/**
41+
* The database connection.
42+
*
43+
* @var \Drupal\Core\Database\Connection
44+
*/
45+
protected $connection;
46+
47+
/**
48+
* PhpFastCacheBackendFactory constructor.
49+
* @param \Drupal\Core\Database\Connection $connection
50+
* @param \Drupal\Core\Cache\CacheTagsChecksumInterface $checksum_provider
51+
*/
52+
public function __construct(Connection $connection, CacheTagsChecksumInterface $checksum_provider)
4453
{
45-
//var_dump(func_get_args());exit;
4654
/**
4755
* Due to the low-level execution stack of PhpFastCacheBackend
4856
* we have to hard-include the PhpFastCache autoload here
@@ -51,6 +59,45 @@ public function __construct($settings, CacheTagsChecksumInterface $checksum_prov
5159
$this->backendClass = 'Drupal\phpfastcache\Cache\PhpFastCacheBackend';
5260
$this->checksumProvider = $checksum_provider;
5361
$this->cachePool = CacheManager::Files(['ignoreSymfonyNotice' => true]);
62+
$this->connection = $connection;
63+
$this->settings = $this->getSettingsFromDatabase();
64+
65+
if(!$this->settings['phpfastcache_enabled']){
66+
if(strpos($_SERVER['REQUEST_URI'], 'admin/config/development/phpfastcache') === false)
67+
{
68+
/**
69+
* At this level nothing is efficient
70+
* - drupal_set_message() is not working/displaying anything
71+
* - throwing exception displays a fatal error without backtrace
72+
* - echoing destroys header leading to another fatal error
73+
*
74+
* Let's dying miserably by showing a simple but efficient message
75+
*/
76+
die('PhpFastCache is not enabled, please go to <strong>admin/config/development/phpfastcache</strong> then configure PhpFastCache.');
77+
}
78+
else
79+
{
80+
$this->backendClass = 'Drupal\phpfastcache\Cache\PhpFastCacheVoidBackend';
81+
}
82+
}
83+
}
84+
85+
/**
86+
* Get settings from database.
87+
* At this level of runtime execution
88+
* settings are not available yet.
89+
* @return array
90+
*/
91+
protected function getSettingsFromDatabase()
92+
{
93+
$query = 'SELECT `data`
94+
FROM {' . $this->connection->escapeTable('config') . '}
95+
WHERE `name` = :name
96+
LIMIT 1';
97+
$params = [':name' => 'phpfastcache.settings'];
98+
$result = $this->connection->query($query, $params);
99+
100+
return unserialize($result->fetchField());
54101
}
55102

56103
/**

src/Cache/PhpFastCacheVoidBackend.php

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
<?php
2+
/**
3+
* Z
4+
*/
5+
namespace Drupal\phpfastcache\Cache;
6+
7+
use Drupal\Core\Cache\Cache;
8+
use Drupal\Core\Cache\CacheTagsChecksumInterface;
9+
10+
11+
/**
12+
* Class PhpFastCacheService
13+
*/
14+
class PhpFastCacheVoidBackend implements \Drupal\Core\Cache\CacheBackendInterface
15+
{
16+
/**
17+
* The name of the cache bin to use.
18+
*
19+
* @var string
20+
*/
21+
protected $bin;
22+
23+
/**
24+
* Prefix for all keys in this cache bin.
25+
*
26+
* Includes the site-specific prefix in $sitePrefix.
27+
*
28+
* @var string
29+
*/
30+
protected $binPrefix;
31+
32+
/**
33+
* The cache tags checksum provider.
34+
*
35+
* @var \Drupal\Core\Cache\CacheTagsChecksumInterface
36+
*/
37+
protected $checksumProvider;
38+
39+
public function __construct($bin, $cachePool, CacheTagsChecksumInterface $checksum_provider)
40+
{
41+
/**
42+
* Constructs a new ApcuBackend instance.
43+
*
44+
* @param string $bin
45+
* The name of the cache bin.
46+
* @param string $site_prefix
47+
* The prefix to use for all keys in the storage that belong to this site.
48+
* @param \Drupal\Core\Cache\CacheTagsChecksumInterface $checksum_provider
49+
* The cache tags checksum provider.
50+
*/
51+
require_once __DIR__ . '/../../phpfastcache-php/src/autoload.php';
52+
}
53+
54+
55+
/**
56+
* @inheritDoc
57+
*/
58+
public function get($cid, $allow_invalid = false)
59+
{
60+
return false;
61+
}
62+
63+
/**
64+
* @inheritDoc
65+
*/
66+
public function getMultiple(&$cids, $allow_invalid = false)
67+
{
68+
return [];
69+
}
70+
71+
/**
72+
* @inheritDoc
73+
*/
74+
public function set($cid, $data, $expire = Cache::PERMANENT, array $tags = [])
75+
{
76+
77+
}
78+
79+
/**
80+
* @inheritDoc
81+
*/
82+
public function setMultiple(array $items)
83+
{
84+
85+
}
86+
87+
/**
88+
* @inheritDoc
89+
*/
90+
public function delete($cid)
91+
{
92+
93+
}
94+
95+
/**
96+
* @inheritDoc
97+
*/
98+
public function deleteMultiple(array $cids)
99+
{
100+
101+
}
102+
103+
/**
104+
* @inheritDoc
105+
*/
106+
public function deleteAll()
107+
{
108+
109+
}
110+
111+
/**
112+
* @inheritDoc
113+
*/
114+
public function invalidate($cid)
115+
{
116+
117+
}
118+
119+
/**
120+
* @inheritDoc
121+
*/
122+
public function invalidateMultiple(array $cids)
123+
{
124+
}
125+
126+
/**
127+
* @inheritDoc
128+
*/
129+
public function invalidateAll()
130+
{
131+
throw new UnsupportedMethodException('Method invalidateAll() is currently not supported by PhpFastCache as there no way to list items in cache');
132+
}
133+
134+
/**
135+
* @inheritDoc
136+
*/
137+
public function garbageCollection()
138+
{
139+
/**
140+
* Does not concerns PhpFastCache
141+
*/
142+
}
143+
144+
/**
145+
* @inheritDoc
146+
*/
147+
public function removeBin()
148+
{
149+
// TODO: Implement removeBin() method.
150+
}
151+
}

0 commit comments

Comments
 (0)