AnnotationBridgeDecorator.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace Drupal\Component\Annotation\Plugin\Discovery;
  3. use Drupal\Component\Plugin\Discovery\DiscoveryInterface;
  4. use Drupal\Component\Plugin\Discovery\DiscoveryTrait;
  5. /**
  6. * Ensures that all definitions are run through the annotation process.
  7. */
  8. class AnnotationBridgeDecorator implements DiscoveryInterface {
  9. use DiscoveryTrait;
  10. /**
  11. * The decorated plugin discovery.
  12. *
  13. * @var \Drupal\Component\Plugin\Discovery\DiscoveryInterface
  14. */
  15. protected $decorated;
  16. /**
  17. * The name of the annotation that contains the plugin definition.
  18. *
  19. * @var string|null
  20. */
  21. protected $pluginDefinitionAnnotationName;
  22. /**
  23. * ObjectDefinitionDiscoveryDecorator constructor.
  24. *
  25. * @param \Drupal\Component\Plugin\Discovery\DiscoveryInterface $decorated
  26. * The discovery object that is being decorated.
  27. * @param string $plugin_definition_annotation_name
  28. * The name of the annotation that contains the plugin definition. The class
  29. * corresponding to this name must implement
  30. * \Drupal\Component\Annotation\AnnotationInterface.
  31. */
  32. public function __construct(DiscoveryInterface $decorated, $plugin_definition_annotation_name) {
  33. $this->decorated = $decorated;
  34. $this->pluginDefinitionAnnotationName = $plugin_definition_annotation_name;
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function getDefinitions() {
  40. $definitions = $this->decorated->getDefinitions();
  41. foreach ($definitions as $id => $definition) {
  42. // Annotation constructors expect an array of values. If the definition is
  43. // not an array, it usually means it has been processed already and can be
  44. // ignored.
  45. if (is_array($definition)) {
  46. $definitions[$id] = (new $this->pluginDefinitionAnnotationName($definition))->get();
  47. }
  48. }
  49. return $definitions;
  50. }
  51. /**
  52. * Passes through all unknown calls onto the decorated object.
  53. *
  54. * @param string $method
  55. * The method to call on the decorated plugin discovery.
  56. * @param array $args
  57. * The arguments to send to the method.
  58. *
  59. * @return mixed
  60. * The method result.
  61. */
  62. public function __call($method, $args) {
  63. return call_user_func_array([$this->decorated, $method], $args);
  64. }
  65. }