PluginDependencyTrait.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. if ($definition instanceof PluginDefinitionInterface) {
  32. $dependencies['module'][] = $definition->getProvider();
  33. if ($definition instanceof DependentPluginDefinitionInterface && $config_dependencies = $definition->getConfigDependencies()) {
  34. $dependencies = NestedArray::mergeDeep($dependencies, $config_dependencies);
  35. }
  36. }
  37. elseif (is_array($definition)) {
  38. $dependencies['module'][] = $definition['provider'];
  39. // Plugins can declare additional dependencies in their definition.
  40. if (isset($definition['config_dependencies'])) {
  41. $dependencies = NestedArray::mergeDeep($dependencies, $definition['config_dependencies']);
  42. }
  43. }
  44. // If a plugin is dependent, calculate its dependencies.
  45. if ($instance instanceof DependentPluginInterface && $plugin_dependencies = $instance->calculateDependencies()) {
  46. $dependencies = NestedArray::mergeDeep($dependencies, $plugin_dependencies);
  47. }
  48. return $dependencies;
  49. }
  50. /**
  51. * Calculates and adds dependencies of a specific plugin instance.
  52. *
  53. * Dependencies are added for the module that provides the plugin, as well
  54. * as any dependencies declared by the instance's calculateDependencies()
  55. * method, if it implements
  56. * \Drupal\Component\Plugin\DependentPluginInterface.
  57. *
  58. * @param \Drupal\Component\Plugin\PluginInspectionInterface $instance
  59. * The plugin instance.
  60. */
  61. protected function calculatePluginDependencies(PluginInspectionInterface $instance) {
  62. $this->addDependencies($this->getPluginDependencies($instance));
  63. }
  64. }