UpdateRegistry.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 all available update functions.
  79. *
  80. * @return callable[]
  81. * A list of update functions.
  82. */
  83. protected function getAvailableUpdateFunctions() {
  84. $regexp = '/^(?<module>.+)_' . $this->updateType . '_(?<name>.+)$/';
  85. $functions = get_defined_functions();
  86. $updates = [];
  87. foreach (preg_grep('/_' . $this->updateType . '_/', $functions['user']) as $function) {
  88. // If this function is a module update function, add it to the list of
  89. // module updates.
  90. if (preg_match($regexp, $function, $matches)) {
  91. if (in_array($matches['module'], $this->enabledModules)) {
  92. $updates[] = $matches['module'] . '_' . $this->updateType . '_' . $matches['name'];
  93. }
  94. }
  95. }
  96. // Ensure that the update order is deterministic.
  97. sort($updates);
  98. return $updates;
  99. }
  100. /**
  101. * Find all update functions that haven't been executed.
  102. *
  103. * @return callable[]
  104. * A list of update functions.
  105. */
  106. public function getPendingUpdateFunctions() {
  107. // We need a) the list of active modules (we get that from the config
  108. // bootstrap factory) and b) the path to the modules, we use the extension
  109. // discovery for that.
  110. $this->scanExtensionsAndLoadUpdateFiles();
  111. // First figure out which hook_{$this->updateType}_NAME got executed
  112. // already.
  113. $existing_update_functions = $this->keyValue->get('existing_updates', []);
  114. $available_update_functions = $this->getAvailableUpdateFunctions();
  115. $not_executed_update_functions = array_diff($available_update_functions, $existing_update_functions);
  116. return $not_executed_update_functions;
  117. }
  118. /**
  119. * Loads all update files for a given list of extension.
  120. *
  121. * @param \Drupal\Core\Extension\Extension[] $module_extensions
  122. * The extensions used for loading.
  123. */
  124. protected function loadUpdateFiles(array $module_extensions) {
  125. // Load all the {$this->updateType}.php files.
  126. foreach ($this->enabledModules as $module) {
  127. if (isset($module_extensions[$module])) {
  128. $this->loadUpdateFile($module_extensions[$module]);
  129. }
  130. }
  131. }
  132. /**
  133. * Loads the {$this->updateType}.php file for a given extension.
  134. *
  135. * @param \Drupal\Core\Extension\Extension $module
  136. * The extension of the module to load its file.
  137. */
  138. protected function loadUpdateFile(Extension $module) {
  139. $filename = $this->root . '/' . $module->getPath() . '/' . $module->getName() . ".{$this->updateType}.php";
  140. if (file_exists($filename)) {
  141. include_once $filename;
  142. }
  143. }
  144. /**
  145. * Returns a list of all the pending updates.
  146. *
  147. * @return array[]
  148. * An associative array keyed by module name which contains all information
  149. * about database updates that need to be run, and any updates that are not
  150. * going to proceed due to missing requirements.
  151. *
  152. * The subarray for each module can contain the following keys:
  153. * - start: The starting update that is to be processed. If this does not
  154. * exist then do not process any updates for this module as there are
  155. * other requirements that need to be resolved.
  156. * - pending: An array of all the pending updates for the module including
  157. * the description from source code comment for each update function.
  158. * This array is keyed by the update name.
  159. */
  160. public function getPendingUpdateInformation() {
  161. $functions = $this->getPendingUpdateFunctions();
  162. $ret = [];
  163. foreach ($functions as $function) {
  164. list($module, $update) = explode("_{$this->updateType}_", $function);
  165. // The description for an update comes from its Doxygen.
  166. $func = new \ReflectionFunction($function);
  167. $description = trim(str_replace(["\n", '*', '/'], '', $func->getDocComment()), ' ');
  168. $ret[$module]['pending'][$update] = $description;
  169. if (!isset($ret[$module]['start'])) {
  170. $ret[$module]['start'] = $update;
  171. }
  172. }
  173. return $ret;
  174. }
  175. /**
  176. * Registers that update functions were executed.
  177. *
  178. * @param string[] $function_names
  179. * The executed update functions.
  180. *
  181. * @return $this
  182. */
  183. public function registerInvokedUpdates(array $function_names) {
  184. $executed_updates = $this->keyValue->get('existing_updates', []);
  185. $executed_updates = array_merge($executed_updates, $function_names);
  186. $this->keyValue->set('existing_updates', $executed_updates);
  187. return $this;
  188. }
  189. /**
  190. * Returns all available updates for a given module.
  191. *
  192. * @param string $module_name
  193. * The module name.
  194. *
  195. * @return callable[]
  196. * A list of update functions.
  197. */
  198. public function getModuleUpdateFunctions($module_name) {
  199. $this->scanExtensionsAndLoadUpdateFiles();
  200. $all_functions = $this->getAvailableUpdateFunctions();
  201. return array_filter($all_functions, function ($function_name) use ($module_name) {
  202. list($function_module_name,) = explode("_{$this->updateType}_", $function_name);
  203. return $function_module_name === $module_name;
  204. });
  205. }
  206. /**
  207. * Scans all module + profile extensions and load the update files.
  208. */
  209. protected function scanExtensionsAndLoadUpdateFiles() {
  210. // Scan the module list.
  211. $extension_discovery = new ExtensionDiscovery($this->root, FALSE, [], $this->sitePath);
  212. $module_extensions = $extension_discovery->scan('module');
  213. $profile_extensions = $extension_discovery->scan('profile');
  214. $extensions = array_merge($module_extensions, $profile_extensions);
  215. $this->loadUpdateFiles($extensions);
  216. }
  217. /**
  218. * Filters out already executed update functions by module.
  219. *
  220. * @param string $module
  221. * The module name.
  222. */
  223. public function filterOutInvokedUpdatesByModule($module) {
  224. $existing_update_functions = $this->keyValue->get('existing_updates', []);
  225. $remaining_update_functions = array_filter($existing_update_functions, function ($function_name) use ($module) {
  226. return strpos($function_name, "{$module}_{$this->updateType}_") !== 0;
  227. });
  228. $this->keyValue->set('existing_updates', array_values($remaining_update_functions));
  229. }
  230. }