file UpdateManager.php

Namespace

Drupal\bootstrap\Plugin
  1. <?php
  2. namespace Drupal\bootstrap\Plugin;
  3. use Drupal\bootstrap\Theme;
  4. use Drupal\Component\Utility\SortArray;
  5. /**
  6. * Manages discovery and instantiation of Bootstrap updates.
  7. *
  8. * @ingroup plugins_update
  9. */
  10. class UpdateManager extends PluginManager {
  11. /**
  12. * Constructs a new \Drupal\bootstrap\Plugin\UpdateManager object.
  13. *
  14. * @param \Drupal\bootstrap\Theme $theme
  15. * The theme to use for discovery.
  16. */
  17. public function __construct(Theme $theme) {
  18. // Unlike other plugins in this base theme, this one should only discover
  19. // update plugins that are unique to its own theme to avoid plugin ID
  20. // collision (e.g. base and sub-theme both implement an update plugin
  21. // with the id "8001").
  22. $this->namespaces = new \ArrayObject(['Drupal\\' . $theme->getName() => [DRUPAL_ROOT . '/' . $theme->getPath() . '/src']]);
  23. $this->theme = $theme;
  24. $this->subdir = 'Plugin/Update';
  25. $this->pluginDefinitionAnnotationName = 'Drupal\bootstrap\Annotation\BootstrapUpdate';
  26. $this->pluginInterface = 'Drupal\bootstrap\Plugin\Update\UpdateInterface';
  27. $this->themeHandler = \Drupal::service('theme_handler');
  28. $this->themeManager = \Drupal::service('theme.manager');
  29. $this->setCacheBackend(\Drupal::cache('discovery'), 'theme:' . $theme->getName() . ':update', $this->getCacheTags());
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. protected function sortDefinitions(array &$definitions) {
  35. // Sort by the schema number (a.k.a. plugin ID).
  36. uasort($definitions, function ($a, $b) {
  37. return SortArray::sortByKeyInt($a, $b, 'id');
  38. });
  39. }
  40. /**
  41. * Retrieves the latest update schema.
  42. *
  43. * @return int|array
  44. * The latest update schema.
  45. */
  46. public function getLatestSchema() {
  47. $schema = \Drupal::CORE_MINIMUM_SCHEMA_VERSION;
  48. if ($schemas = $this->getSchemas()) {
  49. $schema = max(max($schemas), $schema);
  50. }
  51. return $schema;
  52. }
  53. /**
  54. * Retrieves any pending updates.
  55. *
  56. * @param bool $private
  57. * Toggle determining whether or not to include private updates, intended
  58. * for only the theme that created it. Defaults to: FALSE.
  59. *
  60. * @return \Drupal\bootstrap\Plugin\Update\UpdateInterface[]
  61. * An associative array containing update objects, keyed by their version.
  62. */
  63. public function getPendingUpdates($private = FALSE) {
  64. $pending = [];
  65. $installed_schemas = $this->theme->getSetting('schemas', []);
  66. foreach ($this->getUpdates($private) as $version => $update) {
  67. $installed_schema = $installed_schemas[$update->getProvider()] ?? \Drupal::CORE_MINIMUM_SCHEMA_VERSION;
  68. if ($version > $installed_schema) {
  69. $pending[$version] = $update;
  70. }
  71. }
  72. return $pending;
  73. }
  74. /**
  75. * Retrieves update plugins for the theme.
  76. *
  77. * @param bool $private
  78. * Toggle determining whether or not to include private updates, intended
  79. * for only the theme that created it. Defaults to: FALSE.
  80. *
  81. * @return \Drupal\bootstrap\Plugin\Update\UpdateInterface[]
  82. * An associative array containing update objects, keyed by their version.
  83. */
  84. public function getUpdates($private = FALSE) {
  85. $updates = [];
  86. foreach ($this->getSchemas($private) as $schema) {
  87. $updates[$schema] = $this->createInstance($schema, ['theme' => $this->theme]);
  88. }
  89. return $updates;
  90. }
  91. /**
  92. * Retrieves the update schema identifiers.
  93. *
  94. * @param bool $private
  95. * Toggle determining whether or not to include private updates, intended
  96. * for only the theme that created it. Defaults to: FALSE.
  97. *
  98. * @return array
  99. * An indexed array of schema identifiers.
  100. */
  101. protected function getSchemas($private = FALSE) {
  102. $definitions = $this->getDefinitions();
  103. // Remove private updates.
  104. if (!$private) {
  105. foreach ($definitions as $plugin_id => $definition) {
  106. if (!empty($definition['private'])) {
  107. unset($definitions[$plugin_id]);
  108. }
  109. }
  110. }
  111. return array_keys($definitions);
  112. }
  113. /*************************
  114. * Deprecated methods.
  115. *************************/
  116. /**
  117. * Retrieves the latest update schema.
  118. *
  119. * @return int
  120. * The latest update schema.
  121. *
  122. * @deprecated 8.x-3.0-rc2, will be removed before 8.x-3.0 is released.
  123. *
  124. * @see \Drupal\bootstrap\Plugin\UpdateManager::getLatestSchema
  125. */
  126. public function getLatestVersion() {
  127. return $this->getLatestSchema();
  128. }
  129. }

Classes

Name Description
UpdateManager Manages discovery and instantiation of Bootstrap updates.