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 = $this->theme->getSetting('schemas', []);
  70. foreach ($this->getUpdates($private) as $version => $update) {
  71. if ($version > $installed) {
  72. $pending[$version] = $update;
  73. }
  74. }
  75. return $pending;
  76. }
  77. /**
  78. * Retrieves update plugins for the theme.
  79. *
  80. * @param bool $private
  81. * Toggle determining whether or not to include private updates, intended
  82. * for only the theme that created it. Defaults to: FALSE.
  83. *
  84. * @return \Drupal\bootstrap\Plugin\Update\UpdateInterface[]
  85. * An associative array containing update objects, keyed by their version.
  86. */
  87. public function getUpdates($private = FALSE) {
  88. $updates = [];
  89. foreach ($this->getSchemas($private) as $schema) {
  90. $updates[$schema] = $this->createInstance($schema, ['theme' => $this->theme]);
  91. }
  92. return $updates;
  93. }
  94. /**
  95. * Retrieves the update schema identifiers.
  96. *
  97. * @param bool $private
  98. * Toggle determining whether or not to include private updates, intended
  99. * for only the theme that created it. Defaults to: FALSE.
  100. *
  101. * @return array
  102. * An indexed array of schema identifiers.
  103. */
  104. protected function getSchemas($private = FALSE) {
  105. $definitions = $this->getDefinitions();
  106. // Remove private updates.
  107. if (!$private) {
  108. foreach ($definitions as $plugin_id => $definition) {
  109. if (!empty($definition['private'])) {
  110. unset($definitions[$plugin_id]);
  111. }
  112. }
  113. }
  114. return array_keys($definitions);
  115. }
  116. /*************************
  117. * Deprecated methods.
  118. *************************/
  119. /**
  120. * Retrieves the latest update schema.
  121. *
  122. * @return int
  123. * The latest update schema.
  124. *
  125. * @deprecated 8.x-3.0-rc2, will be removed before 8.x-3.0 is released.
  126. *
  127. * @see \Drupal\bootstrap\Plugin\UpdateManager::getLatestSchema
  128. */
  129. public function getLatestVersion() {
  130. return $this->getLatestSchema();
  131. }
  132. }

Classes

Name Description
UpdateManager Manages discovery and instantiation of Bootstrap updates.