Plugin.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace Drupal\Component\Annotation;
  3. use Drupal\Component\Utility\NestedArray;
  4. /**
  5. * Defines a Plugin annotation object.
  6. *
  7. * Annotations in plugin classes can use this class in order to pass various
  8. * metadata about the plugin through the parser to
  9. * DiscoveryInterface::getDefinitions() calls. This allows the metadata
  10. * of a class to be located with the class itself, rather than in module-based
  11. * info hooks.
  12. *
  13. * @ingroup plugin_api
  14. *
  15. * @Annotation
  16. */
  17. class Plugin implements AnnotationInterface {
  18. /**
  19. * The plugin definition read from the class annotation.
  20. *
  21. * @var array
  22. */
  23. protected $definition;
  24. /**
  25. * Constructs a Plugin object.
  26. *
  27. * Builds up the plugin definition and invokes the get() method for any
  28. * classed annotations that were used.
  29. */
  30. public function __construct($values) {
  31. $reflection = new \ReflectionClass($this);
  32. // Only keep actual default values by ignoring NULL values.
  33. $defaults = array_filter($reflection->getDefaultProperties(), function ($value) {
  34. return $value !== NULL;
  35. });
  36. $parsed_values = $this->parse($values);
  37. $this->definition = NestedArray::mergeDeepArray([$defaults, $parsed_values], TRUE);
  38. }
  39. /**
  40. * Parses an annotation into its definition.
  41. *
  42. * @param array $values
  43. * The annotation array.
  44. *
  45. * @return array
  46. * The parsed annotation as a definition.
  47. */
  48. protected function parse(array $values) {
  49. $definitions = [];
  50. foreach ($values as $key => $value) {
  51. if ($value instanceof AnnotationInterface) {
  52. $definitions[$key] = $value->get();
  53. }
  54. elseif (is_array($value)) {
  55. $definitions[$key] = $this->parse($value);
  56. }
  57. else {
  58. $definitions[$key] = $value;
  59. }
  60. }
  61. return $definitions;
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function get() {
  67. return $this->definition;
  68. }
  69. /**
  70. * {@inheritdoc}
  71. */
  72. public function getProvider() {
  73. return isset($this->definition['provider']) ? $this->definition['provider'] : FALSE;
  74. }
  75. /**
  76. * {@inheritdoc}
  77. */
  78. public function setProvider($provider) {
  79. $this->definition['provider'] = $provider;
  80. }
  81. /**
  82. * {@inheritdoc}
  83. */
  84. public function getId() {
  85. return $this->definition['id'];
  86. }
  87. /**
  88. * {@inheritdoc}
  89. */
  90. public function getClass() {
  91. return $this->definition['class'];
  92. }
  93. /**
  94. * {@inheritdoc}
  95. */
  96. public function setClass($class) {
  97. $this->definition['class'] = $class;
  98. }
  99. }