4 * Contains \Drupal\bootstrap\Plugin\UpdateManager.
7 namespace Drupal\bootstrap\Plugin;
9 use Drupal\bootstrap\Theme;
10 use Drupal\Component\Utility\SortArray;
13 * Manages discovery and instantiation of Bootstrap updates.
15 * @ingroup plugins_update
17 class UpdateManager extends PluginManager {
20 * Constructs a new \Drupal\bootstrap\Plugin\UpdateManager object.
22 * @param \Drupal\bootstrap\Theme $theme
23 * The theme to use for discovery.
25 public function __construct(Theme $theme) {
26 // Unlike other plugins in this base theme, this one should only discover
27 // update plugins that are unique to its own theme to avoid plugin ID
28 // collision (e.g. base and sub-theme both implement an update plugin
29 // with the id "8001").
30 $this->namespaces = new \ArrayObject(['Drupal\\' . $theme->getName() => [DRUPAL_ROOT . '/' . $theme->getPath() . '/src']]);
32 $this->theme = $theme;
33 $this->subdir = 'Plugin/Update';
34 $this->pluginDefinitionAnnotationName = 'Drupal\bootstrap\Annotation\BootstrapUpdate';
35 $this->pluginInterface = 'Drupal\bootstrap\Plugin\Update\UpdateInterface';
36 $this->themeHandler = \Drupal::service('theme_handler');
37 $this->themeManager = \Drupal::service('theme.manager');
38 $this->setCacheBackend(\Drupal::cache('discovery'), 'theme:' . $theme->getName() . ':update', $this->getCacheTags());
44 public function getDefinitions($sorted = TRUE) {
45 $definitions = parent::getDefinitions();
47 // Sort by the schema number (a.k.a. plugin ID).
49 uasort($definitions, function ($a, $b) {
50 return SortArray::sortByKeyInt($a, $b, 'id');
58 * Retrieves the latest update schema.
61 * The latest update schema.
63 public function getLatestSchema() {
64 $schema = \Drupal::CORE_MINIMUM_SCHEMA_VERSION;
65 if ($schemas = $this->getSchemas()) {
66 $schema = max(max($schemas), $schema);
72 * Retrieves any pending updates.
74 * @param bool $private
75 * Toggle determining whether or not to include private updates, intended
76 * for only the theme that created it. Defaults to: FALSE.
78 * @return \Drupal\bootstrap\Plugin\Update\UpdateInterface[]
79 * An associative array containing update objects, keyed by their version.
81 public function getPendingUpdates($private = FALSE) {
83 $installed = $this->theme->getSetting('schemas', []);
84 foreach ($this->getUpdates($private) as $version => $update) {
85 if ($version > $installed) {
86 $pending[$version] = $update;
93 * Retrieves update plugins for the theme.
95 * @param bool $private
96 * Toggle determining whether or not to include private updates, intended
97 * for only the theme that created it. Defaults to: FALSE.
99 * @return \Drupal\bootstrap\Plugin\Update\UpdateInterface[]
100 * An associative array containing update objects, keyed by their version.
102 public function getUpdates($private = FALSE) {
104 foreach ($this->getSchemas($private) as $schema) {
105 $updates[$schema] = $this->createInstance($schema, ['theme' => $this->theme]);
111 * Retrieves the update schema identifiers.
113 * @param bool $private
114 * Toggle determining whether or not to include private updates, intended
115 * for only the theme that created it. Defaults to: FALSE.
118 * An indexed array of schema identifiers.
120 protected function getSchemas($private = FALSE) {
121 $definitions = $this->getDefinitions();
123 // Remove private updates.
125 foreach ($definitions as $plugin_id => $definition) {
126 if (!empty($definition['private'])) {
127 unset($definitions[$plugin_id]);
132 return array_keys($definitions);
135 /*************************
136 * Deprecated methods.
137 *************************/
140 * Retrieves the latest update schema.
143 * The latest update schema.
145 * @deprecated 8.x-3.0-rc2, will be removed before 8.x-3.0 is released.
147 * @see \Drupal\bootstrap\Plugin\UpdateManager::getLatestSchema
149 public function getLatestVersion() {
150 return $this->getLatestSchema();