You are here

class Plugin in Service Container 7.2

Same name and namespace in other branches
  1. 7 lib/Drupal/Component/Annotation/Plugin.php \Drupal\Component\Annotation\Plugin

Defines a Plugin annotation object.

Annotations in plugin classes can use this class in order to pass various metadata about the plugin through the parser to DiscoveryInterface::getDefinitions() calls. This allows the metadata of a class to be located with the class itself, rather than in module-based info hooks.

Hierarchy

Expanded class hierarchy of Plugin

12 files declare their use of Plugin
Block.php in lib/Drupal/Core/Block/Annotation/Block.php
Contains \Drupal\Core\Block\Annotation\Block.
Plugin1A.php in modules/providers/service_container_annotation_discovery/tests/modules/service_container_annotation_discovery_test/src/Plugin/Plugin1/Plugin1A/Plugin1A.php
Plugin1B.php in modules/providers/service_container_annotation_discovery/tests/modules/service_container_annotation_discovery_test/src/Plugin/Plugin1/Plugin1B/Plugin1B.php
Plugin2A.php in modules/providers/service_container_annotation_discovery/tests/modules/service_container_annotation_discovery_test/src/Plugin/Plugin2/Plugin2A/Plugin2A.php
Plugin2B.php in modules/providers/service_container_annotation_discovery/tests/modules/service_container_annotation_discovery_test/src/Plugin/Plugin2/Plugin2B/Plugin2B.php

... See full list

2 string references to 'Plugin'
service_container_annotation_discovery_subtest.services.yml in modules/providers/service_container_annotation_discovery/tests/modules/service_container_annotation_discovery_subtest/service_container_annotation_discovery_subtest.services.yml
modules/providers/service_container_annotation_discovery/tests/modules/service_container_annotation_discovery_subtest/service_container_annotation_discovery_subtest.services.yml
service_container_annotation_discovery_test.services.yml in modules/providers/service_container_annotation_discovery/tests/modules/service_container_annotation_discovery_test/service_container_annotation_discovery_test.services.yml
modules/providers/service_container_annotation_discovery/tests/modules/service_container_annotation_discovery_test/service_container_annotation_discovery_test.services.yml

File

lib/Drupal/Component/Annotation/Plugin.php, line 25
Contains \Drupal\Component\Annotation\Plugin.

Namespace

Drupal\Component\Annotation
View source
class Plugin implements AnnotationInterface {

  /**
   * The plugin definition read from the class annotation.
   *
   * @var array
   */
  protected $definition;

  /**
   * Constructs a Plugin object.
   *
   * Builds up the plugin definition and invokes the get() method for any
   * classed annotations that were used.
   */
  public function __construct($values) {
    $reflection = new \ReflectionClass($this);

    // Only keep actual default values by ignoring NULL values.
    $defaults = array_filter($reflection
      ->getDefaultProperties(), function ($value) {
      return $value !== NULL;
    });
    $parsed_values = $this
      ->parse($values);
    $this->definition = NestedArray::mergeDeep($defaults, $parsed_values);
  }

  /**
   * Parses an annotation into its definition.
   *
   * @param array $values
   *   The annotation array.
   *
   * @return array
   *  The parsed annotation as a definition.
   */
  protected function parse(array $values) {
    $definitions = array();
    foreach ($values as $key => $value) {
      if ($value instanceof AnnotationInterface) {
        $definitions[$key] = $value
          ->get();
      }
      elseif (is_array($value)) {
        $definitions[$key] = $this
          ->parse($value);
      }
      else {
        $definitions[$key] = $value;
      }
    }
    return $definitions;
  }

  /**
   * {@inheritdoc}
   */
  public function get() {
    return $this->definition;
  }

  /**
   * {@inheritdoc}
   */
  public function getProvider() {
    return isset($this->definition['provider']) ? $this->definition['provider'] : FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function setProvider($provider) {
    $this->definition['provider'] = $provider;
  }

  /**
   * {@inheritdoc}
   */
  public function getId() {
    return $this->definition['id'];
  }

  /**
   * {@inheritdoc}
   */
  public function getClass() {
    return $this->definition['class'];
  }

  /**
   * {@inheritdoc}
   */
  public function setClass($class) {
    $this->definition['class'] = $class;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Plugin::$definition protected property The plugin definition read from the class annotation.
Plugin::get public function Gets the value of an annotation. Overrides AnnotationInterface::get
Plugin::getClass public function Gets the class of the annotated class. Overrides AnnotationInterface::getClass
Plugin::getId public function Gets the unique ID for this annotated class. Overrides AnnotationInterface::getId
Plugin::getProvider public function Gets the name of the provider of the annotated class. Overrides AnnotationInterface::getProvider
Plugin::parse protected function Parses an annotation into its definition.
Plugin::setClass public function Sets the class of the annotated class. Overrides AnnotationInterface::setClass
Plugin::setProvider public function Sets the name of the provider of the annotated class. Overrides AnnotationInterface::setProvider
Plugin::__construct public function Constructs a Plugin object.