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