3 namespace Drupal\bootstrap\Plugin;
5 use Drupal\bootstrap\Theme;
6 use Drupal\Component\Utility\SortArray;
9 * Manages discovery and instantiation of Bootstrap updates.
11 * @ingroup plugins_update
13 class UpdateManager extends PluginManager {
16 * Constructs a new \Drupal\bootstrap\Plugin\UpdateManager object.
18 * @param \Drupal\bootstrap\Theme $theme
19 * The theme to use for discovery.
21 public function __construct(Theme $theme) {
22 // Unlike other plugins in this base theme, this one should only discover
23 // update plugins that are unique to its own theme to avoid plugin ID
24 // collision (e.g. base and sub-theme both implement an update plugin
25 // with the id "8001").
26 $this->namespaces = new \ArrayObject(['Drupal\\' . $theme->getName() => [DRUPAL_ROOT . '/' . $theme->getPath() . '/src']]);
28 $this->theme = $theme;
29 $this->subdir = 'Plugin/Update';
30 $this->pluginDefinitionAnnotationName = 'Drupal\bootstrap\Annotation\BootstrapUpdate';
31 $this->pluginInterface = 'Drupal\bootstrap\Plugin\Update\UpdateInterface';
32 $this->themeHandler = \Drupal::service('theme_handler');
33 $this->themeManager = \Drupal::service('theme.manager');
34 $this->setCacheBackend(\Drupal::cache('discovery'), 'theme:' . $theme->getName() . ':update', $this->getCacheTags());
40 public function getDefinitions($sorted = TRUE) {
41 $definitions = parent::getDefinitions();
43 // Sort by the schema number (a.k.a. plugin ID).
45 uasort($definitions, function ($a, $b) {
46 return SortArray::sortByKeyInt($a, $b, 'id');
54 * Retrieves the latest update schema.
57 * The latest update schema.
59 public function getLatestSchema() {
60 $schema = \Drupal::CORE_MINIMUM_SCHEMA_VERSION;
61 if ($schemas = $this->getSchemas()) {
62 $schema = max(max($schemas), $schema);
68 * Retrieves any pending updates.
70 * @param bool $private
71 * Toggle determining whether or not to include private updates, intended
72 * for only the theme that created it. Defaults to: FALSE.
74 * @return \Drupal\bootstrap\Plugin\Update\UpdateInterface[]
75 * An associative array containing update objects, keyed by their version.
77 public function getPendingUpdates($private = FALSE) {
79 $installed = $this->theme->getSetting('schemas', []);
80 foreach ($this->getUpdates($private) as $version => $update) {
81 if ($version > $installed) {
82 $pending[$version] = $update;
89 * Retrieves update plugins for the theme.
91 * @param bool $private
92 * Toggle determining whether or not to include private updates, intended
93 * for only the theme that created it. Defaults to: FALSE.
95 * @return \Drupal\bootstrap\Plugin\Update\UpdateInterface[]
96 * An associative array containing update objects, keyed by their version.
98 public function getUpdates($private = FALSE) {
100 foreach ($this->getSchemas($private) as $schema) {
101 $updates[$schema] = $this->createInstance($schema, ['theme' => $this->theme]);
107 * Retrieves the update schema identifiers.
109 * @param bool $private
110 * Toggle determining whether or not to include private updates, intended
111 * for only the theme that created it. Defaults to: FALSE.
114 * An indexed array of schema identifiers.
116 protected function getSchemas($private = FALSE) {
117 $definitions = $this->getDefinitions();
119 // Remove private updates.
121 foreach ($definitions as $plugin_id => $definition) {
122 if (!empty($definition['private'])) {
123 unset($definitions[$plugin_id]);
128 return array_keys($definitions);
131 /*************************
132 * Deprecated methods.
133 *************************/
136 * Retrieves the latest update schema.
139 * The latest update schema.
141 * @deprecated 8.x-3.0-rc2, will be removed before 8.x-3.0 is released.
143 * @see \Drupal\bootstrap\Plugin\UpdateManager::getLatestSchema
145 public function getLatestVersion() {
146 return $this->getLatestSchema();