UpdateRegistry.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <?php
  2. namespace Drupal\Core\Update;
  3. use Drupal\Core\Extension\Extension;
  4. use Drupal\Core\Extension\ExtensionDiscovery;
  5. use Drupal\Core\KeyValueStore\KeyValueStoreInterface;
  6. /**
  7. * Provides all and missing update implementations.
  8. *
  9. * Note: This registry is specific to a type of updates, like 'post_update' as
  10. * example.
  11. *
  12. * It therefore scans for functions named like the type of updates, so it looks
  13. * like MODULE_UPDATETYPE_NAME() with NAME being a machine name.
  14. */
  15. class UpdateRegistry {
  16. /**
  17. * The used update name.
  18. *
  19. * @var string
  20. */
  21. protected $updateType = 'post_update';
  22. /**
  23. * The app root.
  24. *
  25. * @var string
  26. */
  27. protected $root;
  28. /**
  29. * The filename of the log file.
  30. *
  31. * @var string
  32. */
  33. protected $logFilename;
  34. /**
  35. * @var string[]
  36. */
  37. protected $enabledModules;
  38. /**
  39. * The key value storage.
  40. *
  41. * @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface
  42. */
  43. protected $keyValue;
  44. /**
  45. * Should we respect update functions in tests.
  46. *
  47. * @var bool|null
  48. */
  49. protected $includeTests = NULL;
  50. /**
  51. * The site path.
  52. *
  53. * @var string
  54. */
  55. protected $sitePath;
  56. /**
  57. * Constructs a new UpdateRegistry.
  58. *
  59. * @param string $root
  60. * The app root.
  61. * @param string $site_path
  62. * The site path.
  63. * @param string[] $enabled_modules
  64. * A list of enabled modules.
  65. * @param \Drupal\Core\KeyValueStore\KeyValueStoreInterface $key_value
  66. * The key value store.
  67. * @param bool|null $include_tests
  68. * (optional) A flag whether to include tests in the scanning of modules.
  69. */
  70. public function __construct($root, $site_path, array $enabled_modules, KeyValueStoreInterface $key_value, $include_tests = NULL) {
  71. $this->root = $root;
  72. $this->sitePath = $site_path;
  73. $this->enabledModules = $enabled_modules;
  74. $this->keyValue = $key_value;
  75. $this->includeTests = $include_tests;
  76. }
  77. /**
  78. * Gets removed hook_post_update_NAME() implementations for a module.
  79. *
  80. * @return string[]
  81. * A list of post-update functions that have been removed.
  82. */
  83. public function getRemovedPostUpdates($module) {
  84. $this->scanExtensionsAndLoadUpdateFiles();
  85. $function = "{$module}_removed_post_updates";
  86. if (function_exists($function)) {
  87. return $function();
  88. }
  89. return [];
  90. }
  91. /**
  92. * Gets all available update functions.
  93. *
  94. * @return callable[]
  95. * A list of update functions.
  96. */
  97. protected function getAvailableUpdateFunctions() {
  98. $regexp = '/^(?<module>.+)_' . $this->updateType . '_(?<name>.+)$/';
  99. $functions = get_defined_functions();
  100. $updates = [];
  101. foreach (preg_grep('/_' . $this->updateType . '_/', $functions['user']) as $function) {
  102. // If this function is a module update function, add it to the list of
  103. // module updates.
  104. if (preg_match($regexp, $function, $matches)) {
  105. if (in_array($matches['module'], $this->enabledModules)) {
  106. $function_name = $matches['module'] . '_' . $this->updateType . '_' . $matches['name'];
  107. if ($this->updateType === 'post_update') {
  108. $removed = array_keys($this->getRemovedPostUpdates($matches['module']));
  109. if (array_search($function_name, $removed) !== FALSE) {
  110. throw new RemovedPostUpdateNameException(sprintf('The following update is specified as removed in hook_removed_post_updates() but still exists in the code base: %s', $function_name));
  111. }
  112. }
  113. $updates[] = $function_name;
  114. }
  115. }
  116. }
  117. // Ensure that the update order is deterministic.
  118. sort($updates);
  119. return $updates;
  120. }
  121. /**
  122. * Find all update functions that haven't been executed.
  123. *
  124. * @return callable[]
  125. * A list of update functions.
  126. */
  127. public function getPendingUpdateFunctions() {
  128. // We need a) the list of active modules (we get that from the config
  129. // bootstrap factory) and b) the path to the modules, we use the extension
  130. // discovery for that.
  131. $this->scanExtensionsAndLoadUpdateFiles();
  132. // First figure out which hook_{$this->updateType}_NAME got executed
  133. // already.
  134. $existing_update_functions = $this->keyValue->get('existing_updates', []);
  135. $available_update_functions = $this->getAvailableUpdateFunctions();
  136. $not_executed_update_functions = array_diff($available_update_functions, $existing_update_functions);
  137. return $not_executed_update_functions;
  138. }
  139. /**
  140. * Loads all update files for a given list of extension.
  141. *
  142. * @param \Drupal\Core\Extension\Extension[] $module_extensions
  143. * The extensions used for loading.
  144. */
  145. protected function loadUpdateFiles(array $module_extensions) {
  146. // Load all the {$this->updateType}.php files.
  147. foreach ($this->enabledModules as $module) {
  148. if (isset($module_extensions[$module])) {
  149. $this->loadUpdateFile($module_extensions[$module]);
  150. }
  151. }
  152. }
  153. /**
  154. * Loads the {$this->updateType}.php file for a given extension.
  155. *
  156. * @param \Drupal\Core\Extension\Extension $module
  157. * The extension of the module to load its file.
  158. */
  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;
  163. }
  164. }
  165. /**
  166. * Returns a list of all the pending updates.
  167. *
  168. * @return array[]
  169. * An associative array keyed by module name which contains all information
  170. * about database updates that need to be run, and any updates that are not
  171. * going to proceed due to missing requirements.
  172. *
  173. * The subarray for each module can contain the following keys:
  174. * - start: The starting update that is to be processed. If this does not
  175. * exist then do not process any updates for this module as there are
  176. * other requirements that need to be resolved.
  177. * - pending: An array of all the pending updates for the module including
  178. * the description from source code comment for each update function.
  179. * This array is keyed by the update name.
  180. */
  181. public function getPendingUpdateInformation() {
  182. $functions = $this->getPendingUpdateFunctions();
  183. $ret = [];
  184. foreach ($functions as $function) {
  185. list($module, $update) = explode("_{$this->updateType}_", $function);
  186. // The description for an update comes from its Doxygen.
  187. $func = new \ReflectionFunction($function);
  188. $description = trim(str_replace(["\n", '*', '/'], '', $func->getDocComment()), ' ');
  189. $ret[$module]['pending'][$update] = $description;
  190. if (!isset($ret[$module]['start'])) {
  191. $ret[$module]['start'] = $update;
  192. }
  193. }
  194. return $ret;
  195. }
  196. /**
  197. * Registers that update functions were executed.
  198. *
  199. * @param string[] $function_names
  200. * The executed update functions.
  201. *
  202. * @return $this
  203. */
  204. public function registerInvokedUpdates(array $function_names) {
  205. $executed_updates = $this->keyValue->get('existing_updates', []);
  206. $executed_updates = array_merge($executed_updates, $function_names);
  207. $this->keyValue->set('existing_updates', $executed_updates);
  208. return $this;
  209. }
  210. /**
  211. * Returns all available updates for a given module.
  212. *
  213. * @param string $module_name
  214. * The module name.
  215. *
  216. * @return callable[]
  217. * A list of update functions.
  218. */
  219. public function getModuleUpdateFunctions($module_name) {
  220. $this->scanExtensionsAndLoadUpdateFiles();
  221. $all_functions = $this->getAvailableUpdateFunctions();
  222. return array_filter($all_functions, function ($function_name) use ($module_name) {
  223. list($function_module_name,) = explode("_{$this->updateType}_", $function_name);
  224. return $function_module_name === $module_name;
  225. });
  226. }
  227. /**
  228. * Scans all module + profile extensions and load the update files.
  229. */
  230. protected function scanExtensionsAndLoadUpdateFiles() {
  231. // Scan the module list.
  232. $extension_discovery = new ExtensionDiscovery($this->root, FALSE, [], $this->sitePath);
  233. $module_extensions = $extension_discovery->scan('module');
  234. $profile_extensions = $extension_discovery->scan('profile');
  235. $extensions = array_merge($module_extensions, $profile_extensions);
  236. $this->loadUpdateFiles($extensions);
  237. }
  238. /**
  239. * Filters out already executed update functions by module.
  240. *
  241. * @param string $module
  242. * The module name.
  243. */
  244. public function filterOutInvokedUpdatesByModule($module) {
  245. $existing_update_functions = $this->keyValue->get('existing_updates', []);
  246. $remaining_update_functions = array_filter($existing_update_functions, function ($function_name) use ($module) {
  247. return strpos($function_name, "{$module}_{$this->updateType}_") !== 0;
  248. });
  249. $this->keyValue->set('existing_updates', array_values($remaining_update_functions));
  250. }
  251. }