3 namespace Drupal\Component\Annotation;
5 use Drupal\Component\Utility\NestedArray;
8 * Defines a Plugin annotation object.
10 * Annotations in plugin classes can use this class in order to pass various
11 * metadata about the plugin through the parser to
12 * DiscoveryInterface::getDefinitions() calls. This allows the metadata
13 * of a class to be located with the class itself, rather than in module-based
20 class Plugin implements AnnotationInterface {
23 * The plugin definition read from the class annotation.
27 protected $definition;
30 * Constructs a Plugin object.
32 * Builds up the plugin definition and invokes the get() method for any
33 * classed annotations that were used.
35 public function __construct($values) {
36 $reflection = new \ReflectionClass($this);
37 // Only keep actual default values by ignoring NULL values.
38 $defaults = array_filter($reflection->getDefaultProperties(), function ($value) {
39 return $value !== NULL;
41 $parsed_values = $this->parse($values);
42 $this->definition = NestedArray::mergeDeep($defaults, $parsed_values);
46 * Parses an annotation into its definition.
48 * @param array $values
49 * The annotation array.
52 * The parsed annotation as a definition.
54 protected function parse(array $values) {
56 foreach ($values as $key => $value) {
57 if ($value instanceof AnnotationInterface) {
58 $definitions[$key] = $value->get();
60 elseif (is_array($value)) {
61 $definitions[$key] = $this->parse($value);
64 $definitions[$key] = $value;
73 public function get() {
74 return $this->definition;
80 public function getProvider() {
81 return isset($this->definition['provider']) ? $this->definition['provider'] : FALSE;
87 public function setProvider($provider) {
88 $this->definition['provider'] = $provider;
94 public function getId() {
95 return $this->definition['id'];
101 public function getClass() {
102 return $this->definition['class'];
108 public function setClass($class) {
109 $this->definition['class'] = $class;