1<?php 2 3namespace Drupal\Component\Annotation; 4 5use Drupal\Component\Utility\NestedArray; 6 7/** 8 * Defines a Plugin annotation object. 9 * 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 14 * info hooks. 15 * 16 * @ingroup plugin_api 17 * 18 * @Annotation 19 */ 20class Plugin implements AnnotationInterface { 21 22 /** 23 * The plugin definition read from the class annotation. 24 * 25 * @var array 26 */ 27 protected $definition; 28 29 /** 30 * Constructs a Plugin object. 31 * 32 * Builds up the plugin definition and invokes the get() method for any 33 * classed annotations that were used. 34 */ 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; 40 }); 41 $parsed_values = $this->parse($values); 42 $this->definition = NestedArray::mergeDeepArray([$defaults, $parsed_values], TRUE); 43 } 44 45 /** 46 * Parses an annotation into its definition. 47 * 48 * @param array $values 49 * The annotation array. 50 * 51 * @return array 52 * The parsed annotation as a definition. 53 */ 54 protected function parse(array $values) { 55 $definitions = []; 56 foreach ($values as $key => $value) { 57 if ($value instanceof AnnotationInterface) { 58 $definitions[$key] = $value->get(); 59 } 60 elseif (is_array($value)) { 61 $definitions[$key] = $this->parse($value); 62 } 63 else { 64 $definitions[$key] = $value; 65 } 66 } 67 return $definitions; 68 } 69 70 /** 71 * {@inheritdoc} 72 */ 73 public function get() { 74 return $this->definition; 75 } 76 77 /** 78 * {@inheritdoc} 79 */ 80 public function getProvider() { 81 return isset($this->definition['provider']) ? $this->definition['provider'] : FALSE; 82 } 83 84 /** 85 * {@inheritdoc} 86 */ 87 public function setProvider($provider) { 88 $this->definition['provider'] = $provider; 89 } 90 91 /** 92 * {@inheritdoc} 93 */ 94 public function getId() { 95 return $this->definition['id']; 96 } 97 98 /** 99 * {@inheritdoc} 100 */ 101 public function getClass() { 102 return $this->definition['class']; 103 } 104 105 /** 106 * {@inheritdoc} 107 */ 108 public function setClass($class) { 109 $this->definition['class'] = $class; 110 } 111 112} 113