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. public function getDefinitions($sorted = TRUE) {
  35. $definitions = parent::getDefinitions();
  36. // Sort by the schema number (a.k.a. plugin ID).
  37. if ($sorted) {
  38. uasort($definitions, function ($a, $b) {
  39. return SortArray::sortByKeyInt($a, $b, 'id');
  40. });
  41. }
  42. return $definitions;
  43. }
  44. /**
  45. * Retrieves the latest update schema.
  46. *
  47. * @return int|array
  48. * The latest update schema.
  49. */
  50. public function getLatestSchema() {
  51. $schema = \Drupal::CORE_MINIMUM_SCHEMA_VERSION;
  52. if ($schemas = $this->getSchemas()) {
  53. $schema = max(max($schemas), $schema);
  54. }
  55. return $schema;
  56. }
  57. /**
  58. * Retrieves any pending updates.
  59. *
  60. * @param bool $private
  61. * Toggle determining whether or not to include private updates, intended
  62. * for only the theme that created it. Defaults to: FALSE.
  63. *
  64. * @return \Drupal\bootstrap\Plugin\Update\UpdateInterface[]
  65. * An associative array containing update objects, keyed by their version.
  66. */
  67. public function getPendingUpdates($private = FALSE) {
  68. $pending = [];
  69. $installed_schemas = $this->theme->getSetting('schemas', []);
  70. foreach ($this->getUpdates($private) as $version => $update) {
  71. $installed_schema = $installed_schemas[$update->getProvider()] ?? \Drupal::CORE_MINIMUM_SCHEMA_VERSION;
  72. if ($version > $installed_schema) {
  73. $pending[$version] = $update;
  74. }
  75. }
  76. return $pending;
  77. }
  78. /**
  79. * Retrieves update plugins for the theme.
  80. *
  81. * @param bool $private
  82. * Toggle determining whether or not to include private updates, intended
  83. * for only the theme that created it. Defaults to: FALSE.
  84. *
  85. * @return \Drupal\bootstrap\Plugin\Update\UpdateInterface[]
  86. * An associative array containing update objects, keyed by their version.
  87. */
  88. public function getUpdates($private = FALSE) {
  89. $updates = [];
  90. foreach ($this->getSchemas($private) as $schema) {
  91. $updates[$schema] = $this->createInstance($schema, ['theme' => $this->theme]);
  92. }
  93. return $updates;
  94. }
  95. /**
  96. * Retrieves the update schema identifiers.
  97. *
  98. * @param bool $private
  99. * Toggle determining whether or not to include private updates, intended
  100. * for only the theme that created it. Defaults to: FALSE.
  101. *
  102. * @return array
  103. * An indexed array of schema identifiers.
  104. */
  105. protected function getSchemas($private = FALSE) {
  106. $definitions = $this->getDefinitions();
  107. // Remove private updates.
  108. if (!$private) {
  109. foreach ($definitions as $plugin_id => $definition) {
  110. if (!empty($definition['private'])) {
  111. unset($definitions[$plugin_id]);
  112. }
  113. }
  114. }
  115. return array_keys($definitions);
  116. }
  117. /*************************
  118. * Deprecated methods.
  119. *************************/
  120. /**
  121. * Retrieves the latest update schema.
  122. *
  123. * @return int
  124. * The latest update schema.
  125. *
  126. * @deprecated 8.x-3.0-rc2, will be removed before 8.x-3.0 is released.
  127. *
  128. * @see \Drupal\bootstrap\Plugin\UpdateManager::getLatestSchema
  129. */
  130. public function getLatestVersion() {
  131. return $this->getLatestSchema();
  132. }
  133. }

Classes

Name Description
UpdateManager Manages discovery and instantiation of Bootstrap updates.