class AnnotatedClassDiscovery

Same name in this branch
  1. 8.x drupal/core/lib/Drupal/Core/Plugin/Discovery/AnnotatedClassDiscovery.php \Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery
  2. 8.x drupal/core/lib/Drupal/Component/Plugin/Discovery/AnnotatedClassDiscovery.php \Drupal\Component\Plugin\Discovery\AnnotatedClassDiscovery

Defines a discovery mechanism to find annotated plugins in PSR-0 namespaces.

Hierarchy

Expanded class hierarchy of AnnotatedClassDiscovery

2 files declare their use of AnnotatedClassDiscovery
AnnotatedClassDiscovery.php in drupal/core/lib/Drupal/Core/Plugin/Discovery/AnnotatedClassDiscovery.php
Definition of Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery.
ViewsHandlerDiscovery.php in drupal/core/modules/views/lib/Drupal/views/Plugin/Discovery/ViewsHandlerDiscovery.php
Contains \Drupal\views\Plugin\Discovery\ViewsHandlerDiscovery.

File

drupal/core/lib/Drupal/Component/Plugin/Discovery/AnnotatedClassDiscovery.php, line 20
Contains Drupal\Component\Plugin\Discovery\AnnotatedClassDiscovery.

Namespace

Drupal\Component\Plugin\Discovery
View source
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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AnnotatedClassDiscovery::$annotationNamespaces protected property The namespaces of classes that can be used as annotations.
AnnotatedClassDiscovery::$pluginDefinitionAnnotationName protected property The name of the annotation that contains the plugin definition.
AnnotatedClassDiscovery::$pluginNamespaces protected property The namespaces within which to find plugin classes.
AnnotatedClassDiscovery::getAnnotationNamespaces protected function Returns an array of PSR-0 namespaces to search for annotation classes.
AnnotatedClassDiscovery::getDefinition public function Implements Drupal\Component\Plugin\Discovery\DiscoveryInterface::getDefinition(). Overrides DiscoveryInterface::getDefinition
AnnotatedClassDiscovery::getDefinitions public function Implements Drupal\Component\Plugin\Discovery\DiscoveryInterface::getDefinitions(). Overrides DiscoveryInterface::getDefinitions 1
AnnotatedClassDiscovery::getPluginNamespaces protected function Returns an array of PSR-0 namespaces to search for plugin classes. 2
AnnotatedClassDiscovery::__construct function Constructs an AnnotatedClassDiscovery object. 2