Pull merge.
[yaffs-website] / web / core / lib / Drupal / Core / Plugin / PluginDependencyTrait.php
1 <?php
2
3 namespace Drupal\Core\Plugin;
4
5 use Drupal\Component\Plugin\Definition\PluginDefinitionInterface;
6 use Drupal\Component\Plugin\DependentPluginInterface;
7 use Drupal\Component\Plugin\PluginInspectionInterface;
8 use Drupal\Component\Utility\NestedArray;
9 use Drupal\Core\Entity\DependencyTrait;
10 use Drupal\Core\Plugin\Definition\DependentPluginDefinitionInterface;
11
12 /**
13  * Provides a trait for calculating the dependencies of a plugin.
14  */
15 trait PluginDependencyTrait {
16
17   use DependencyTrait;
18
19   /**
20    * Calculates and returns dependencies of a specific plugin instance.
21    *
22    * Dependencies are added for the module that provides the plugin, as well
23    * as any dependencies declared by the instance's calculateDependencies()
24    * method, if it implements
25    * \Drupal\Component\Plugin\DependentPluginInterface.
26    *
27    * @param \Drupal\Component\Plugin\PluginInspectionInterface $instance
28    *   The plugin instance.
29    *
30    * @return array
31    *   An array of dependencies keyed by the type of dependency.
32    */
33   protected function getPluginDependencies(PluginInspectionInterface $instance) {
34     $dependencies = [];
35     $definition = $instance->getPluginDefinition();
36     if ($definition instanceof PluginDefinitionInterface) {
37       $dependencies['module'][] = $definition->getProvider();
38       if ($definition instanceof DependentPluginDefinitionInterface && $config_dependencies = $definition->getConfigDependencies()) {
39         $dependencies = NestedArray::mergeDeep($dependencies, $config_dependencies);
40       }
41     }
42     elseif (is_array($definition)) {
43       $dependencies['module'][] = $definition['provider'];
44       // Plugins can declare additional dependencies in their definition.
45       if (isset($definition['config_dependencies'])) {
46         $dependencies = NestedArray::mergeDeep($dependencies, $definition['config_dependencies']);
47       }
48     }
49
50     // If a plugin is dependent, calculate its dependencies.
51     if ($instance instanceof DependentPluginInterface && $plugin_dependencies = $instance->calculateDependencies()) {
52       $dependencies = NestedArray::mergeDeep($dependencies, $plugin_dependencies);
53     }
54     return $dependencies;
55   }
56
57   /**
58    * Calculates and adds dependencies of a specific plugin instance.
59    *
60    * Dependencies are added for the module that provides the plugin, as well
61    * as any dependencies declared by the instance's calculateDependencies()
62    * method, if it implements
63    * \Drupal\Component\Plugin\DependentPluginInterface.
64    *
65    * @param \Drupal\Component\Plugin\PluginInspectionInterface $instance
66    *   The plugin instance.
67    */
68   protected function calculatePluginDependencies(PluginInspectionInterface $instance) {
69     $this->addDependencies($this->getPluginDependencies($instance));
70   }
71
72 }