3 namespace Drupal\Core\Update;
5 use Drupal\Core\Extension\Extension;
6 use Drupal\Core\Extension\ExtensionDiscovery;
7 use Drupal\Core\KeyValueStore\KeyValueStoreInterface;
10 * Provides all and missing update implementations.
12 * Note: This registry is specific to a type of updates, like 'post_update' as
15 * It therefore scans for functions named like the type of updates, so it looks
16 * like MODULE_UPDATETYPE_NAME() with NAME being a machine name.
18 class UpdateRegistry {
21 * The used update name.
25 protected $updateType = 'post_update';
35 * The filename of the log file.
39 protected $logFilename;
44 protected $enabledModules;
47 * The key value storage.
49 * @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface
54 * Should we respect update functions in tests.
58 protected $includeTests = NULL;
68 * Constructs a new UpdateRegistry.
72 * @param string $site_path
74 * @param string[] $enabled_modules
75 * A list of enabled modules.
76 * @param \Drupal\Core\KeyValueStore\KeyValueStoreInterface $key_value
77 * The key value store.
78 * @param bool|null $include_tests
79 * (optional) A flag whether to include tests in the scanning of modules.
81 public function __construct($root, $site_path, array $enabled_modules, KeyValueStoreInterface $key_value, $include_tests = NULL) {
83 $this->sitePath = $site_path;
84 $this->enabledModules = $enabled_modules;
85 $this->keyValue = $key_value;
86 $this->includeTests = $include_tests;
90 * Gets all available update functions.
93 * A list of update functions.
95 protected function getAvailableUpdateFunctions() {
96 $regexp = '/^(?<module>.+)_' . $this->updateType . '_(?<name>.+)$/';
97 $functions = get_defined_functions();
100 foreach (preg_grep('/_' . $this->updateType . '_/', $functions['user']) as $function) {
101 // If this function is a module update function, add it to the list of
103 if (preg_match($regexp, $function, $matches)) {
104 if (in_array($matches['module'], $this->enabledModules)) {
105 $updates[] = $matches['module'] . '_' . $this->updateType . '_' . $matches['name'];
110 // Ensure that the update order is deterministic.
116 * Find all update functions that haven't been executed.
119 * A list of update functions.
121 public function getPendingUpdateFunctions() {
122 // We need a) the list of active modules (we get that from the config
123 // bootstrap factory) and b) the path to the modules, we use the extension
124 // discovery for that.
126 $this->scanExtensionsAndLoadUpdateFiles();
128 // First figure out which hook_{$this->updateType}_NAME got executed
130 $existing_update_functions = $this->keyValue->get('existing_updates', []);
132 $available_update_functions = $this->getAvailableUpdateFunctions();
133 $not_executed_update_functions = array_diff($available_update_functions, $existing_update_functions);
135 return $not_executed_update_functions;
139 * Loads all update files for a given list of extension.
141 * @param \Drupal\Core\Extension\Extension[] $module_extensions
142 * The extensions used for loading.
144 protected function loadUpdateFiles(array $module_extensions) {
145 // Load all the {$this->updateType}.php files.
146 foreach ($this->enabledModules as $module) {
147 if (isset($module_extensions[$module])) {
148 $this->loadUpdateFile($module_extensions[$module]);
154 * Loads the {$this->updateType}.php file for a given extension.
156 * @param \Drupal\Core\Extension\Extension $module
157 * The extension of the module to load its file.
159 protected function loadUpdateFile(Extension $module) {
160 $filename = $this->root . '/' . $module->getPath() . '/' . $module->getName() . ".{$this->updateType}.php";
161 if (file_exists($filename)) {
162 include_once $filename;
167 * Returns a list of all the pending updates.
170 * An associative array keyed by module name which contains all information
171 * about database updates that need to be run, and any updates that are not
172 * going to proceed due to missing requirements.
174 * The subarray for each module can contain the following keys:
175 * - start: The starting update that is to be processed. If this does not
176 * exist then do not process any updates for this module as there are
177 * other requirements that need to be resolved.
178 * - pending: An array of all the pending updates for the module including
179 * the description from source code comment for each update function.
180 * This array is keyed by the update name.
182 public function getPendingUpdateInformation() {
183 $functions = $this->getPendingUpdateFunctions();
186 foreach ($functions as $function) {
187 list($module, $update) = explode("_{$this->updateType}_", $function);
188 // The description for an update comes from its Doxygen.
189 $func = new \ReflectionFunction($function);
190 $description = trim(str_replace(["\n", '*', '/'], '', $func->getDocComment()), ' ');
191 $ret[$module]['pending'][$update] = $description;
192 if (!isset($ret[$module]['start'])) {
193 $ret[$module]['start'] = $update;
200 * Registers that update fucntions got executed.
202 * @param string[] $function_names
203 * The executed update functions.
207 public function registerInvokedUpdates(array $function_names) {
208 $executed_updates = $this->keyValue->get('existing_updates', []);
209 $executed_updates = array_merge($executed_updates, $function_names);
210 $this->keyValue->set('existing_updates', $executed_updates);
216 * Returns all available updates for a given module.
218 * @param string $module_name
222 * A list of update functions.
224 public function getModuleUpdateFunctions($module_name) {
225 $this->scanExtensionsAndLoadUpdateFiles();
226 $all_functions = $this->getAvailableUpdateFunctions();
228 return array_filter($all_functions, function($function_name) use ($module_name) {
229 list($function_module_name, ) = explode("_{$this->updateType}_", $function_name);
230 return $function_module_name === $module_name;
235 * Scans all module + profile extensions and load the update files.
237 protected function scanExtensionsAndLoadUpdateFiles() {
238 // Scan the module list.
239 $extension_discovery = new ExtensionDiscovery($this->root, FALSE, [], $this->sitePath);
240 $module_extensions = $extension_discovery->scan('module');
242 $profile_extensions = $extension_discovery->scan('profile');
243 $extensions = array_merge($module_extensions, $profile_extensions);
245 $this->loadUpdateFiles($extensions);
249 * Filters out already executed update functions by module.
251 * @param string $module
254 public function filterOutInvokedUpdatesByModule($module) {
255 $existing_update_functions = $this->keyValue->get('existing_updates', []);
257 $remaining_update_functions = array_filter($existing_update_functions, function($function_name) use ($module) {
258 return strpos($function_name, "{$module}_{$this->updateType}_") !== 0;
261 $this->keyValue->set('existing_updates', array_values($remaining_update_functions));