Version 1
[yaffs-website] / web / themes / contrib / bootstrap / src / Plugin / UpdateManager.php
1 <?php
2 /**
3  * @file
4  * Contains \Drupal\bootstrap\Plugin\UpdateManager.
5  */
6
7 namespace Drupal\bootstrap\Plugin;
8
9 use Drupal\bootstrap\Theme;
10 use Drupal\Component\Utility\SortArray;
11
12 /**
13  * Manages discovery and instantiation of Bootstrap updates.
14  *
15  * @ingroup plugins_update
16  */
17 class UpdateManager extends PluginManager {
18
19   /**
20    * Constructs a new \Drupal\bootstrap\Plugin\UpdateManager object.
21    *
22    * @param \Drupal\bootstrap\Theme $theme
23    *   The theme to use for discovery.
24    */
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']]);
31
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());
39   }
40
41   /**
42    * {@inheritdoc}
43    */
44   public function getDefinitions($sorted = TRUE) {
45     $definitions = parent::getDefinitions();
46
47     // Sort by the schema number (a.k.a. plugin ID).
48     if ($sorted) {
49       uasort($definitions, function ($a, $b) {
50         return SortArray::sortByKeyInt($a, $b, 'id');
51       });
52     }
53
54     return $definitions;
55   }
56
57   /**
58    * Retrieves the latest update schema.
59    *
60    * @return int|array
61    *   The latest update schema.
62    */
63   public function getLatestSchema() {
64     $schema = \Drupal::CORE_MINIMUM_SCHEMA_VERSION;
65     if ($schemas = $this->getSchemas()) {
66       $schema = max(max($schemas), $schema);
67     }
68     return $schema;
69   }
70
71   /**
72    * Retrieves any pending updates.
73    *
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.
77    *
78    * @return \Drupal\bootstrap\Plugin\Update\UpdateInterface[]
79    *   An associative array containing update objects, keyed by their version.
80    */
81   public function getPendingUpdates($private = FALSE) {
82     $pending = [];
83     $installed = $this->theme->getSetting('schemas', []);
84     foreach ($this->getUpdates($private) as $version => $update) {
85       if ($version > $installed) {
86         $pending[$version] = $update;
87       }
88     }
89     return $pending;
90   }
91
92   /**
93    * Retrieves update plugins for the theme.
94    *
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.
98    *
99    * @return \Drupal\bootstrap\Plugin\Update\UpdateInterface[]
100    *   An associative array containing update objects, keyed by their version.
101    */
102   public function getUpdates($private = FALSE) {
103     $updates = [];
104     foreach ($this->getSchemas($private) as $schema) {
105       $updates[$schema] = $this->createInstance($schema, ['theme' => $this->theme]);
106     }
107     return $updates;
108   }
109
110   /**
111    * Retrieves the update schema identifiers.
112    *
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.
116    *
117    * @return array
118    *   An indexed array of schema identifiers.
119    */
120   protected function getSchemas($private = FALSE) {
121     $definitions = $this->getDefinitions();
122
123     // Remove private updates.
124     if (!$private) {
125       foreach ($definitions as $plugin_id => $definition) {
126         if (!empty($definition['private'])) {
127           unset($definitions[$plugin_id]);
128         }
129       }
130     }
131
132     return array_keys($definitions);
133   }
134
135   /*************************
136    * Deprecated methods.
137    *************************/
138
139   /**
140    * Retrieves the latest update schema.
141    *
142    * @return int
143    *   The latest update schema.
144    *
145    * @deprecated 8.x-3.0-rc2, will be removed before 8.x-3.0 is released.
146    *
147    * @see \Drupal\bootstrap\Plugin\UpdateManager::getLatestSchema
148    */
149   public function getLatestVersion() {
150     return $this->getLatestSchema();
151   }
152
153 }