PluginDependencyTrait.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace Drupal\Core\Plugin;
  3. use Drupal\Component\Plugin\Definition\PluginDefinitionInterface;
  4. use Drupal\Component\Plugin\DependentPluginInterface;
  5. use Drupal\Component\Plugin\PluginInspectionInterface;
  6. use Drupal\Component\Utility\NestedArray;
  7. use Drupal\Core\Entity\DependencyTrait;
  8. use Drupal\Core\Plugin\Definition\DependentPluginDefinitionInterface;
  9. /**
  10. * Provides a trait for calculating the dependencies of a plugin.
  11. */
  12. trait PluginDependencyTrait {
  13. use DependencyTrait;
  14. /**
  15. * Calculates and returns dependencies of a specific plugin instance.
  16. *
  17. * Dependencies are added for the module that provides the plugin, as well
  18. * as any dependencies declared by the instance's calculateDependencies()
  19. * method, if it implements
  20. * \Drupal\Component\Plugin\DependentPluginInterface.
  21. *
  22. * @param \Drupal\Component\Plugin\PluginInspectionInterface $instance
  23. * The plugin instance.
  24. *
  25. * @return array
  26. * An array of dependencies keyed by the type of dependency.
  27. */
  28. protected function getPluginDependencies(PluginInspectionInterface $instance) {
  29. $dependencies = [];
  30. $definition = $instance->getPluginDefinition();
  31. $provider = NULL;
  32. $config_dependencies = [];
  33. if ($definition instanceof PluginDefinitionInterface) {
  34. $provider = $definition->getProvider();
  35. if ($definition instanceof DependentPluginDefinitionInterface) {
  36. $config_dependencies = $definition->getConfigDependencies();
  37. }
  38. }
  39. elseif (is_array($definition)) {
  40. $provider = $definition['provider'];
  41. if (isset($definition['config_dependencies'])) {
  42. $config_dependencies = $definition['config_dependencies'];
  43. }
  44. }
  45. // Add the provider as a dependency, taking into account if it's a module or
  46. // a theme.
  47. if ($provider) {
  48. if ($provider === 'core' || $this->moduleHandler()->moduleExists($provider)) {
  49. $dependencies['module'][] = $provider;
  50. }
  51. elseif ($this->themeHandler()->themeExists($provider)) {
  52. $dependencies['theme'][] = $provider;
  53. }
  54. else {
  55. @trigger_error('Declaring a dependency on an uninstalled module is deprecated in Drupal 8.7.0 and will not be supported in Drupal 9.0.0.', E_USER_DEPRECATED);
  56. $dependencies['module'][] = $provider;
  57. }
  58. }
  59. // Add the config dependencies.
  60. if ($config_dependencies) {
  61. $dependencies = NestedArray::mergeDeep($dependencies, $config_dependencies);
  62. }
  63. // If a plugin is dependent, calculate its dependencies.
  64. if ($instance instanceof DependentPluginInterface && $plugin_dependencies = $instance->calculateDependencies()) {
  65. $dependencies = NestedArray::mergeDeep($dependencies, $plugin_dependencies);
  66. }
  67. return $dependencies;
  68. }
  69. /**
  70. * Calculates and adds dependencies of a specific plugin instance.
  71. *
  72. * Dependencies are added for the module that provides the plugin, as well
  73. * as any dependencies declared by the instance's calculateDependencies()
  74. * method, if it implements
  75. * \Drupal\Component\Plugin\DependentPluginInterface.
  76. *
  77. * @param \Drupal\Component\Plugin\PluginInspectionInterface $instance
  78. * The plugin instance.
  79. */
  80. protected function calculatePluginDependencies(PluginInspectionInterface $instance) {
  81. $this->addDependencies($this->getPluginDependencies($instance));
  82. }
  83. /**
  84. * Wraps the module handler.
  85. *
  86. * @return \Drupal\Core\Extension\ModuleHandlerInterface
  87. * The module handler.
  88. */
  89. protected function moduleHandler() {
  90. return \Drupal::moduleHandler();
  91. }
  92. /**
  93. * Wraps the theme handler.
  94. *
  95. * @return \Drupal\Core\Extension\ThemeHandlerInterface
  96. * The theme handler.
  97. */
  98. protected function themeHandler() {
  99. return \Drupal::service('theme_handler');
  100. }
  101. }