summaryrefslogtreecommitdiffstats
path: root/core/lib/Drupal/Core/Extension/CachedModuleHandler.php
blob: a3f3eac4eeb22609e4b7b1a04a1c8a9e2d674644 (plain)
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
<?php

/**
 * @file
 * Contains Drupal\Core\Extension\CachedModuleHandler.
 */

namespace Drupal\Core\Extension;

use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\KeyValueStore\KeyValueStoreInterface;

/**
 * Class that manages enabled modules in a Drupal installation.
 */
class CachedModuleHandler extends ModuleHandler implements CachedModuleHandlerInterface {

  /**
   * State key/value store.
   *
   * @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface
   */
  protected $state;

  /**
   * Cache backend for storing enabled modules.
   *
   * @var \Drupal\Core\Cache\CacheBackendInterface
   */
  protected $bootstrapCache;

  /**
   * Whether the cache needs to be written.
   *
   * @var boolean
   */
  protected $cacheNeedsWriting = FALSE;

  /**
   * Constructs a new CachedModuleHandler object.
   */
  public function __construct(array $module_list = array(), KeyValueStoreInterface $state, CacheBackendInterface $bootstrap_cache) {
    parent::__construct($module_list);
    $this->state = $state;
    $this->bootstrapCache = $bootstrap_cache;
  }

  /**
   * Implements \Drupal\Core\Extension\ModuleHandlerInterface::getBootstrapModules().
   */
  public function getBootstrapModules() {
    if (isset($this->bootstrapModules)) {
      return $this->bootstrapModules;
    }
    if ($cached = $this->bootstrapCache->get('bootstrap_modules')) {
      $bootstrap_list = $cached->data;
    }
    else {
      $bootstrap_list = $this->state->get('system.module.bootstrap') ?: array();
      $this->bootstrapCache->set('bootstrap_modules', $bootstrap_list);
    }
    $this->bootstrapModules = array_keys($bootstrap_list);
    return $this->bootstrapModules;
  }

  /**
   * Implements \Drupal\Core\Extension\ModuleHandlerInterface::resetImplementations().
   */
  public function resetImplementations() {
    // We maintain a persistent cache of hook implementations in addition to the
    // static cache to avoid looping through every module and every hook on each
    // request. Benchmarks show that the benefit of this caching outweighs the
    // additional database hit even when using the default database caching
    // backend and only a small number of modules are enabled. The cost of the
    // $this->bootstrapCache->get() is more or less constant and reduced further when
    // non-database caching backends are used, so there will be more significant
    // gains when a large number of modules are installed or hooks invoked, since
    // this can quickly lead to module_hook() being called several thousand times
    // per request.
    parent::resetImplementations();
    $this->bootstrapCache->set('module_implements', array());
    $this->bootstrapCache->delete('hook_info');
  }

  /**
   * Implements \Drupal\Core\Extension\CachedModuleHandlerInterface::writeCache().
   */
  public function writeCache() {
    if ($this->cacheNeedsWriting) {
      $this->bootstrapCache->set('module_implements', $this->implementations);
      $this->cacheNeedsWriting = FALSE;
    }
  }

  /**
   * Overrides \Drupal\Core\Extension\ModuleHandler::getImplementationInfo().
   */
  protected function getImplementationInfo($hook) {
    if (!isset($this->implementations)) {
      $this->implementations = $this->getCachedImplementationInfo();
    }
    if (!isset($this->implementations[$hook])) {
      // The hook is not cached, so ensure that whether or not it has
      // implementations, the cache is updated at the end of the request.
      $this->cacheNeedsWriting = TRUE;
      $this->implementations[$hook] = parent::getImplementationInfo($hook);
    }
    else {
      foreach ($this->implementations[$hook] as $module => $group) {
        // If this hook implementation is stored in a lazy-loaded file, include
        // that file first.
        if ($group) {
          $this->loadInclude($module, 'inc', "$module.$group");
        }
        // It is possible that a module removed a hook implementation without the
        // implementations cache being rebuilt yet, so we check whether the
        // function exists on each request to avoid undefined function errors.
        // Since module_hook() may needlessly try to load the include file again,
        // function_exists() is used directly here.
        if (!function_exists($module . '_' . $hook)) {
          // Clear out the stale implementation from the cache and force a cache
          // refresh to forget about no longer existing hook implementations.
          unset($this->implementations[$hook][$module]);
          $this->cacheNeedsWriting = TRUE;
        }
      }
    }
    return $this->implementations[$hook];
  }

  /**
   * Overrides \Drupal\Core\Extension\ModuleHandler::getHookInfo().
   */
  protected function getHookInfo() {
    // When this function is indirectly invoked from bootstrap_invoke_all() prior
    // to all modules being loaded, we do not want to cache an incomplete
    // hook_hookInfo() result, so instead return an empty array. This requires
    // bootstrap hook implementations to reside in the .module file, which is
    // optimal for performance anyway.
    if (!$this->loaded) {
      return array();
    }
    if (!isset($this->hookInfo)) {
      if ($cache = $this->bootstrapCache->get('hook_info')) {
        $this->hookInfo = $cache->data;
      }
      else {
        $this->hookInfo = parent::getHookInfo();
        $this->bootstrapCache->set('hook_info', $this->hookInfo);
      }
    }
    return $this->hookInfo;
  }

  /**
   * Retrieves hook implementation info from the cache.
   */
  protected function getCachedImplementationInfo() {
    if ($cache = $this->bootstrapCache->get('module_implements')) {
      return $cache->data;
    }
    else {
      return array();
    }
  }

}