AnnotatedClassDiscovery.php

Contains Drupal\Component\Plugin\Discovery\AnnotatedClassDiscovery.

Namespace

Drupal\Component\Plugin\Discovery

File

drupal/core/lib/Drupal/Component/Plugin/Discovery/AnnotatedClassDiscovery.php
View source
<?php

/**
 * @file
 * Contains Drupal\Component\Plugin\Discovery\AnnotatedClassDiscovery.
 */
namespace Drupal\Component\Plugin\Discovery;

use DirectoryIterator;
use Drupal\Component\Plugin\Discovery\DiscoveryInterface;
use Drupal\Component\Reflection\MockFileFinder;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\AnnotationRegistry;
use Doctrine\Common\Reflection\StaticReflectionParser;

/**
 * Defines a discovery mechanism to find annotated plugins in PSR-0 namespaces.
 */
class AnnotatedClassDiscovery implements DiscoveryInterface {

  /**
   * The namespaces within which to find plugin classes.
   *
   * @var array
   */
  protected $pluginNamespaces;

  /**
   * The namespaces of classes that can be used as annotations.
   *
   * @var array
   */
  protected $annotationNamespaces;

  /**
   * The name of the annotation that contains the plugin definition.
   *
   * The class corresponding to this name must implement
   * \Drupal\Component\Annotation\AnnotationInterface.
   *
   * @var string
   */
  protected $pluginDefinitionAnnotationName;

  /**
   * Constructs an AnnotatedClassDiscovery object.
   *
   * @param array $plugin_namespaces
   *   (optional) An array of namespace that may contain plugin implementations.
   *   Defaults to an empty array.
   * @param array $annotation_namespaces
   *   (optional) The namespaces of classes that can be used as annotations.
   *   Defaults to an empty array.
   * @param string $plugin_definition_annotation_name
   *   (optional) The name of the annotation that contains the plugin definition.
   *   Defaults to 'Drupal\Component\Annotation\Plugin'.
   */
  function __construct($plugin_namespaces = array(), $annotation_namespaces = array(), $plugin_definition_annotation_name = 'Drupal\\Component\\Annotation\\Plugin') {
    $this->pluginNamespaces = $plugin_namespaces;
    $this->annotationNamespaces = $annotation_namespaces;
    $this->pluginDefinitionAnnotationName = $plugin_definition_annotation_name;
  }

  /**
   * Implements Drupal\Component\Plugin\Discovery\DiscoveryInterface::getDefinition().
   */
  public function getDefinition($plugin_id) {
    $plugins = $this
      ->getDefinitions();
    return isset($plugins[$plugin_id]) ? $plugins[$plugin_id] : NULL;
  }

  /**
   * Implements Drupal\Component\Plugin\Discovery\DiscoveryInterface::getDefinitions().
   */
  public function getDefinitions() {
    $definitions = array();
    $reader = new AnnotationReader();

    // Prevent @endlink from being parsed as an annotation.
    $reader
      ->addGlobalIgnoredName('endlink');

    // Register the namespaces of classes that can be used for annotations.
    AnnotationRegistry::registerAutoloadNamespaces($this
      ->getAnnotationNamespaces());

    // Search for classes within all PSR-0 namespace locations.
    foreach ($this
      ->getPluginNamespaces() as $namespace => $dirs) {
      foreach ($dirs as $dir) {
        $dir .= DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, $namespace);
        if (file_exists($dir)) {
          foreach (new DirectoryIterator($dir) as $fileinfo) {

            // @todo Once core requires 5.3.6, use $fileinfo->getExtension().
            if (pathinfo($fileinfo
              ->getFilename(), PATHINFO_EXTENSION) == 'php') {
              $class = $namespace . '\\' . $fileinfo
                ->getBasename('.php');

              // The filename is already known, so there is no need to find the
              // file. However, StaticReflectionParser needs a finder, so use a
              // mock version.
              $finder = MockFileFinder::create($fileinfo
                ->getPathName());
              $parser = new StaticReflectionParser($class, $finder);
              if ($annotation = $reader
                ->getClassAnnotation($parser
                ->getReflectionClass(), $this->pluginDefinitionAnnotationName)) {

                // AnnotationInterface::get() returns the array definition
                // instead of requiring us to work with the annotation object.
                $definition = $annotation
                  ->get();
                $definition['class'] = $class;
                $definitions[$definition['id']] = $definition;
              }
            }
          }
        }
      }
    }
    return $definitions;
  }

  /**
   * Returns an array of PSR-0 namespaces to search for plugin classes.
   */
  protected function getPluginNamespaces() {
    return $this->pluginNamespaces;
  }

  /**
   * Returns an array of PSR-0 namespaces to search for annotation classes.
   */
  protected function getAnnotationNamespaces() {
    return $this->annotationNamespaces;
  }

}

Classes

Namesort descending Description
AnnotatedClassDiscovery Defines a discovery mechanism to find annotated plugins in PSR-0 namespaces.