InfoHookDecorator.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace Drupal\Core\Plugin\Discovery;
  3. use Drupal\Component\Plugin\Discovery\DiscoveryInterface;
  4. use Drupal\Component\Plugin\Discovery\DiscoveryTrait;
  5. /**
  6. * Allows info hook implementations to enhance discovered plugin definitions.
  7. */
  8. class InfoHookDecorator implements DiscoveryInterface {
  9. use DiscoveryTrait;
  10. /**
  11. * The Discovery object being decorated.
  12. *
  13. * @var \Drupal\Component\Plugin\Discovery\DiscoveryInterface
  14. */
  15. protected $decorated;
  16. /**
  17. * The name of the info hook that will be implemented by this discovery instance.
  18. *
  19. * @var string
  20. */
  21. protected $hook;
  22. /**
  23. * Constructs a InfoHookDecorator object.
  24. *
  25. * @param \Drupal\Component\Plugin\Discovery\DiscoveryInterface $decorated
  26. * The object implementing DiscoveryInterface that is being decorated.
  27. * @param string $hook
  28. * The name of the info hook to be invoked by this discovery instance.
  29. */
  30. public function __construct(DiscoveryInterface $decorated, $hook) {
  31. $this->decorated = $decorated;
  32. $this->hook = $hook;
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function getDefinitions() {
  38. $definitions = $this->decorated->getDefinitions();
  39. foreach (\Drupal::moduleHandler()->getImplementations($this->hook) as $module) {
  40. $function = $module . '_' . $this->hook;
  41. $function($definitions);
  42. }
  43. return $definitions;
  44. }
  45. /**
  46. * Passes through all unknown calls onto the decorated object.
  47. */
  48. public function __call($method, $args) {
  49. return call_user_func_array([$this->decorated, $method], $args);
  50. }
  51. }