summaryrefslogtreecommitdiffstatshomepage
path: root/core/lib/Drupal/Core/Plugin/PluginDependencyTrait.php
blob: 7ae768708b0fa0f457c371b3666dfe4bfd085353 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php

namespace Drupal\Core\Plugin;

use Drupal\Component\Plugin\Definition\PluginDefinitionInterface;
use Drupal\Component\Plugin\DependentPluginInterface;
use Drupal\Component\Plugin\PluginInspectionInterface;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Entity\DependencyTrait;
use Drupal\Core\Plugin\Definition\DependentPluginDefinitionInterface;

/**
 * Provides a trait for calculating the dependencies of a plugin.
 */
trait PluginDependencyTrait {

  use DependencyTrait;

  /**
   * Calculates and returns dependencies of a specific plugin instance.
   *
   * Dependencies are added for the module that provides the plugin, as well
   * as any dependencies declared by the instance's calculateDependencies()
   * method, if it implements
   * \Drupal\Component\Plugin\DependentPluginInterface.
   *
   * @param \Drupal\Component\Plugin\PluginInspectionInterface $instance
   *   The plugin instance.
   *
   * @return array
   *   An array of dependencies keyed by the type of dependency.
   */
  protected function getPluginDependencies(PluginInspectionInterface $instance) {
    $dependencies = [];
    $definition = $instance->getPluginDefinition();

    $provider = NULL;
    $config_dependencies = [];
    if ($definition instanceof PluginDefinitionInterface) {
      $provider = $definition->getProvider();

      if ($definition instanceof DependentPluginDefinitionInterface) {
        $config_dependencies = $definition->getConfigDependencies();
      }
    }
    elseif (is_array($definition)) {
      $provider = $definition['provider'];

      if (isset($definition['config_dependencies'])) {
        $config_dependencies = $definition['config_dependencies'];
      }
    }

    // Add the provider as a dependency, taking into account if it's a module or
    // a theme.
    if ($provider) {
      if ($provider === 'core' || $this->moduleHandler()->moduleExists($provider)) {
        $dependencies['module'][] = $provider;
      }
      elseif ($this->themeHandler()->themeExists($provider)) {
        $dependencies['theme'][] = $provider;
      }
    }

    // Add the config dependencies.
    if ($config_dependencies) {
      $dependencies = NestedArray::mergeDeep($dependencies, $config_dependencies);
    }

    // If a plugin is dependent, calculate its dependencies.
    if ($instance instanceof DependentPluginInterface && $plugin_dependencies = $instance->calculateDependencies()) {
      $dependencies = NestedArray::mergeDeep($dependencies, $plugin_dependencies);
    }
    return $dependencies;
  }

  /**
   * Calculates and adds dependencies of a specific plugin instance.
   *
   * Dependencies are added for the module that provides the plugin, as well
   * as any dependencies declared by the instance's calculateDependencies()
   * method, if it implements
   * \Drupal\Component\Plugin\DependentPluginInterface.
   *
   * @param \Drupal\Component\Plugin\PluginInspectionInterface $instance
   *   The plugin instance.
   */
  protected function calculatePluginDependencies(PluginInspectionInterface $instance) {
    $this->addDependencies($this->getPluginDependencies($instance));
  }

  /**
   * Wraps the module handler.
   *
   * @return \Drupal\Core\Extension\ModuleHandlerInterface
   *   The module handler.
   */
  protected function moduleHandler() {
    return \Drupal::moduleHandler();
  }

  /**
   * Wraps the theme handler.
   *
   * @return \Drupal\Core\Extension\ThemeHandlerInterface
   *   The theme handler.
   */
  protected function themeHandler() {
    return \Drupal::service('theme_handler');
  }

}