Completed
Push — 7.x-1.x ( e1c396...0ca620 )
by Frédéric G.
01:51
created

mongodb_cache_module.php ➔ mongodb_cache_exit()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @file
5
 * The MongoDB cache module.
6
 *
7
 * This module only needed:
8
 * - to ensure system_cron triggers expires on cache bins not declared by their
9
 *   owner modules in hook_flush_caches().
10
 * - to ensure a 503 status on exceptions.
11
 */
12
13
use Drupal\mongodb_cache\Cache;
14
15
/**
16
 * Implements hook_exit().
17
 */
18
function mongodb_cache_exit() {
19
  if (Cache::hasException()) {
20
    drupal_add_http_header('Status', '503 Service Unavailable');
21
  }
22
}
23
24
/**
25
 * Implements hook_flush_caches().
26
 *
27
 * Support triggering expiration in modules not declaring their cache bins,
28
 * either by taking them from an explicit variable, or by performing a discovery
29
 * assuming the cache bin names to start by 'cache_'.
30
 */
31
function mongodb_cache_flush_caches() {
32
  // Recursion protection, not static caching, so no drupal_static().
33
  static $reentry = FALSE;
34
35
  if ($reentry) {
36
    return [];
37
  }
38
39
  // Hardcoded in drupal_flush_all_caches() or system_cron(), but not declared
40
  // in system_flush_caches().
41
  $system_bins = [
42
    'cache',
43
    'cache_bootstrap',
44
    'cache_filter',
45
    'cache_form',
46
    'cache_menu',
47
    'cache_page',
48
    'cache_path',
49
  ];
50
51
  $reentry = TRUE;
52
  $owned_bins = module_invoke_all('flush_caches');
53
  $reentry = FALSE;
54
55
  $detected_bins = variable_get('mongodb_cache_extra_bins', NULL);
56
57
  // As with databases, NULL means unknown, so perform a discovery.
58
  if (!isset($detected_bins)) {
59
    $detected_bins = [];
60
    $names = mongodb()->getCollectionNames(FALSE);
61
    foreach ($names as $name) {
62
      if (strpos($name, 'cache_') === 0) {
63
        $detected_bins[] = $name;
64
      }
65
    }
66
  }
67
68
  $adopted_bins = array_diff($detected_bins, $system_bins, $owned_bins);
69
  return $adopted_bins;
70
}
71
72
/**
73
 * Implements hook_module_implements_alter().
74
 */
75
function mongodb_cache_module_implements_alter(&$implementations, $hook) {
76
  if ($hook !== ('flush_caches')) {
77
    return;
78
  }
79
  $module = 'mongodb_cache';
80
  unset($implementations[$module]);
81
  $implementations[$module] = FALSE;
82
}
83