forked from RESTful-Drupal/restful
-
Notifications
You must be signed in to change notification settings - Fork 0
/
restful.entity.inc
198 lines (186 loc) · 6.25 KB
/
restful.entity.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php
/**
* @file
* Contains entity related code.
*/
use Doctrine\Common\Collections\ArrayCollection;
use Drupal\restful\Exception\ServerConfigurationException;
use Drupal\restful\Plugin\resource\Decorators\CacheDecoratedResource;
use Drupal\restful\Plugin\resource\Decorators\CacheDecoratedResourceInterface;
use Drupal\restful\RenderCache\Entity\CacheFragmentController;
use Drupal\restful\RenderCache\RenderCache;
/**
* Implements hook_entity_info().
*/
function restful_entity_info() {
$items['rate_limit'] = array(
'label' => t('Rate limit'),
'entity class' => '\\Drupal\\restful\\RateLimit\\Entity\\RateLimit',
'controller class' => '\\Drupal\\restful\\RateLimit\\Entity\\RateLimitController',
'base table' => 'restful_rate_limit',
'fieldable' => TRUE,
'entity keys' => array(
'id' => 'rlid',
'label' => 'identifier',
'bundle' => 'event',
),
'bundles' => array(),
'bundle keys' => array(
'bundle' => 'type',
),
'module' => 'restful',
'entity cache' => module_exists('entitycache'),
);
$items['cache_fragment'] = array(
'label' => t('Cache fragment'),
'entity class' => '\\Drupal\\restful\\RenderCache\\Entity\\CacheFragment',
'controller class' => '\\Drupal\\restful\\RenderCache\\Entity\\CacheFragmentController',
'base table' => 'restful_cache_fragment',
'fieldable' => FALSE,
'entity keys' => array(
'id' => 'tid',
'label' => 'identifier',
'bundle' => 'type',
),
'bundles' => array(),
'bundle keys' => array(
'bundle' => 'type',
),
'module' => 'restful',
'entity cache' => FALSE,
);
return $items;
}
/**
* Helper function that extract cache hashes from an entity.
*/
function _restful_entity_cache_hashes($entity, $type) {
if ($type == 'cache_fragment') {
return array();
}
// Limit to the fragments for our entity type.
list($entity_id) = entity_extract_ids($type, $entity);
$query = new \EntityFieldQuery();
$query
->entityCondition('entity_type', 'cache_fragment')
->propertyCondition('type', 'entity')
->propertyCondition('value', CacheDecoratedResource::serializeKeyValue($type, $entity_id));
return CacheFragmentController::lookUpHashes($query);
}
/**
* Implements hook_entity_update().
*/
function restful_entity_update($entity, $type) {
$hashes = &drupal_static('restful_entity_clear_hashes', array());
$new_hashes = _restful_entity_cache_hashes($entity, $type);
array_walk($new_hashes, '_restful_entity_clear_all_resources');
$hashes += $new_hashes;
restful_register_shutdown_function_once('restful_entity_clear_render_cache');
}
/**
* Implements hook_entity_delete().
*/
function restful_entity_delete($entity, $type) {
$hashes = &drupal_static('restful_entity_clear_hashes', array());
$new_hashes = _restful_entity_cache_hashes($entity, $type);
array_walk($new_hashes, '_restful_entity_clear_all_resources');
$hashes += $new_hashes;
restful_register_shutdown_function_once('restful_entity_clear_render_cache');
}
/**
* Implements hook_user_update().
*/
function restful_user_update(&$edit, $account, $category) {
// Search for all the cache fragments with our entity id.
$query = new \EntityFieldQuery();
$query
->entityCondition('entity_type', 'cache_fragment')
->propertyCondition('type', 'user_id')
->propertyCondition('value', $account->uid);
$hashes = &drupal_static('restful_entity_clear_hashes', array());
$new_hashes = CacheFragmentController::lookUpHashes($query);
array_walk($new_hashes, '_restful_entity_clear_all_resources');
$hashes += $new_hashes;
restful_register_shutdown_function_once('restful_entity_clear_render_cache');
}
/**
* Implements hook_user_delete().
*/
function restful_user_delete($account) {
// Search for all the cache fragments with our entity id.
$query = new \EntityFieldQuery();
$query
->entityCondition('entity_type', 'cache_fragment')
->propertyCondition('type', 'user_id')
->propertyCondition('value', $account->uid);
$hashes = &drupal_static('restful_entity_clear_hashes', array());
$new_hashes = CacheFragmentController::lookUpHashes($query);
array_walk($new_hashes, '_restful_entity_clear_all_resources');
$hashes += $new_hashes;
restful_register_shutdown_function_once('restful_entity_clear_render_cache');
}
/**
* Helper function to schedule a shutdown once.
*
* @param callable $callback
* The callback.
*/
function restful_register_shutdown_function_once($callback) {
$existing_callbacks = drupal_register_shutdown_function();
$added = (bool) array_filter($existing_callbacks, function ($item) use ($callback) {
return $item['callback'] == $callback;
});
if (!$added) {
drupal_register_shutdown_function($callback);
}
}
/**
* Clear the cache back ends for the given hash.
*
* @param string $cid
* The cache ID to clear.
*/
function _restful_entity_clear_all_resources($cid) {
if (!$instance_id = CacheFragmentController::resourceIdFromHash($cid)) {
return;
}
try {
$handler = restful()->getResourceManager()->getPlugin($instance_id);
}
catch (ServerConfigurationException $e) {
watchdog_exception('restful', $e);
return;
}
if (!$handler instanceof CacheDecoratedResourceInterface) {
return;
}
// Clear the cache bin.
$handler->getCacheController()->clear($cid);
}
/**
* Delete the scheduled fragments and caches on shutdown.
*/
function restful_entity_clear_render_cache() {
if ($hashes = drupal_static('restful_entity_clear_hashes', array())) {
$hashes = array_unique($hashes);
drupal_static_reset('restful_entity_clear_hashes');
$resource_manager = restful()->getResourceManager();
foreach ($hashes as $hash) {
if (!$instance_id = CacheFragmentController::resourceIdFromHash($hash)) {
continue;
}
$handler = $resource_manager->getPlugin($instance_id);
if (!$handler instanceof CacheDecoratedResourceInterface) {
continue;
}
if (!$handler->hasSimpleInvalidation()) {
continue;
}
// You can get away without the fragments for a clear.
$cache_object = new RenderCache(new ArrayCollection(), $hash, $handler->getCacheController());
// Do a clear with the RenderCache object to also remove the cache
// fragment entities.
$cache_object->clear();
}
}
}