class AnnotatedClassDiscovery

Same name in this branch
  1. 11.x core/lib/Drupal/Component/Annotation/Plugin/Discovery/AnnotatedClassDiscovery.php \Drupal\Component\Annotation\Plugin\Discovery\AnnotatedClassDiscovery
Same name in other branches
  1. 9 core/lib/Drupal/Core/Plugin/Discovery/AnnotatedClassDiscovery.php \Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery
  2. 9 core/lib/Drupal/Component/Annotation/Plugin/Discovery/AnnotatedClassDiscovery.php \Drupal\Component\Annotation\Plugin\Discovery\AnnotatedClassDiscovery
  3. 8.9.x core/lib/Drupal/Core/Plugin/Discovery/AnnotatedClassDiscovery.php \Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery
  4. 8.9.x core/lib/Drupal/Component/Annotation/Plugin/Discovery/AnnotatedClassDiscovery.php \Drupal\Component\Annotation\Plugin\Discovery\AnnotatedClassDiscovery
  5. 10 core/lib/Drupal/Core/Plugin/Discovery/AnnotatedClassDiscovery.php \Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery
  6. 10 core/lib/Drupal/Component/Annotation/Plugin/Discovery/AnnotatedClassDiscovery.php \Drupal\Component\Annotation\Plugin\Discovery\AnnotatedClassDiscovery

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

Hierarchy

Expanded class hierarchy of AnnotatedClassDiscovery

8 files declare their use of AnnotatedClassDiscovery
AnnotatedClassDiscoveryAutomatedProviders.php in core/modules/migrate/src/Plugin/Discovery/AnnotatedClassDiscoveryAutomatedProviders.php
AnnotatedClassDiscoveryTest.php in core/tests/Drupal/KernelTests/Core/Plugin/Discovery/AnnotatedClassDiscoveryTest.php
CustomAnnotationClassDiscoveryTest.php in core/tests/Drupal/KernelTests/Core/Plugin/Discovery/CustomAnnotationClassDiscoveryTest.php
CustomDirectoryAnnotatedClassDiscoveryTest.php in core/tests/Drupal/KernelTests/Core/Plugin/Discovery/CustomDirectoryAnnotatedClassDiscoveryTest.php
DefaultPluginManager.php in core/lib/Drupal/Core/Plugin/DefaultPluginManager.php

... See full list

File

core/lib/Drupal/Core/Plugin/Discovery/AnnotatedClassDiscovery.php, line 11

Namespace

Drupal\Core\Plugin\Discovery
View source
class AnnotatedClassDiscovery extends ComponentAnnotatedClassDiscovery {
    
    /**
     * The directory suffix.
     *
     * A suffix to append to each PSR-4 directory associated with a base
     * namespace, to form the directories where plugins are found.
     *
     * @var string
     */
    protected $directorySuffix = '';
    
    /**
     * The namespace suffix.
     *
     * A suffix to append to each base namespace, to obtain the namespaces where
     * plugins are found.
     *
     * @var string
     */
    protected $namespaceSuffix = '';
    
    /**
     * A list of base namespaces with their PSR-4 directories.
     *
     * @var \Traversable
     */
    protected $rootNamespacesIterator;
    
    /**
     * Constructs an AnnotatedClassDiscovery object.
     *
     * @param string $subdir
     *   Either the plugin's subdirectory, for example 'Plugin/views/filter', or
     *   empty string if plugins are located at the top level of the namespace.
     * @param \Traversable $root_namespaces
     *   An object that implements \Traversable which contains the root paths
     *   keyed by the corresponding namespace to look for plugin implementations.
     *   If $subdir is not an empty string, it will be appended to each namespace.
     * @param string $plugin_definition_annotation_name
     *   (optional) The name of the annotation that contains the plugin definition.
     *   Defaults to 'Drupal\Component\Annotation\Plugin'.
     * @param string[] $annotation_namespaces
     *   (optional) Additional namespaces to scan for annotation definitions.
     */
    public function __construct($subdir, \Traversable $root_namespaces, $plugin_definition_annotation_name = 'Drupal\\Component\\Annotation\\Plugin', array $annotation_namespaces = []) {
        if ($subdir) {
            // Prepend a directory separator to $subdir,
            // if it does not already have one.
            if ('/' !== $subdir[0]) {
                $subdir = '/' . $subdir;
            }
            $this->directorySuffix = $subdir;
            $this->namespaceSuffix = str_replace('/', '\\', $subdir);
        }
        $this->rootNamespacesIterator = $root_namespaces;
        $plugin_namespaces = [];
        parent::__construct($plugin_namespaces, $plugin_definition_annotation_name, $annotation_namespaces);
    }
    
    /**
     * {@inheritdoc}
     */
    protected function getAnnotationReader() {
        if (!isset($this->annotationReader)) {
            $reader = parent::getAnnotationReader();
            // Add the Core annotation classes like @Translation.
            $reader->addNamespace('Drupal\\Core\\Annotation');
            $this->annotationReader = $reader;
        }
        return $this->annotationReader;
    }
    
    /**
     * {@inheritdoc}
     */
    protected function prepareAnnotationDefinition(AnnotationInterface $annotation, $class) {
        parent::prepareAnnotationDefinition($annotation, $class);
        if (!$annotation->getProvider()) {
            $annotation->setProvider($this->getProviderFromNamespace($class));
        }
    }
    
    /**
     * Extracts the provider name from a Drupal namespace.
     *
     * @param string $namespace
     *   The namespace to extract the provider from.
     *
     * @return string|null
     *   The matching provider name, or NULL otherwise.
     */
    protected function getProviderFromNamespace($namespace) {
        preg_match('|^Drupal\\\\(?<provider>[\\w]+)\\\\|', $namespace, $matches);
        if (isset($matches['provider'])) {
            return mb_strtolower($matches['provider']);
        }
        return NULL;
    }
    
    /**
     * {@inheritdoc}
     */
    protected function getPluginNamespaces() {
        $plugin_namespaces = [];
        if ($this->namespaceSuffix) {
            foreach ($this->rootNamespacesIterator as $namespace => $dirs) {
                // Append the namespace suffix to the base namespace, to obtain the
                // plugin namespace; for example, 'Drupal\Views' may become
                // 'Drupal\Views\Plugin\Block'.
                $namespace .= $this->namespaceSuffix;
                foreach ((array) $dirs as $dir) {
                    // Append the directory suffix to the PSR-4 base directory, to obtain
                    // the directory where plugins are found. For example,
                    // DRUPAL_ROOT . '/core/modules/views/src' may become
                    // DRUPAL_ROOT . '/core/modules/views/src/Plugin/Block'.
                    $plugin_namespaces[$namespace][] = $dir . $this->directorySuffix;
                }
            }
        }
        else {
            // Both the namespace suffix and the directory suffix are empty,
            // so the plugin namespaces and directories are the same as the base
            // directories.
            foreach ($this->rootNamespacesIterator as $namespace => $dirs) {
                $plugin_namespaces[$namespace] = (array) $dirs;
            }
        }
        return $plugin_namespaces;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
AnnotatedClassDiscovery::$annotationNamespaces protected property Additional namespaces to be scanned for annotation classes.
AnnotatedClassDiscovery::$annotationReader protected property The doctrine annotation reader.
AnnotatedClassDiscovery::$directorySuffix protected property The directory suffix.
AnnotatedClassDiscovery::$fileCache protected property The file cache object.
AnnotatedClassDiscovery::$namespaceSuffix protected property The namespace suffix.
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::$rootNamespacesIterator protected property A list of base namespaces with their PSR-4 directories.
AnnotatedClassDiscovery::getAnnotationReader protected function Gets the used doctrine annotation reader. Overrides AnnotatedClassDiscovery::getAnnotationReader
AnnotatedClassDiscovery::getDefinitions public function Gets the definition of all plugins for this type. Overrides DiscoveryTrait::getDefinitions 1
AnnotatedClassDiscovery::getPluginNamespaces protected function Gets an array of PSR-4 namespaces to search for plugin classes. Overrides AnnotatedClassDiscovery::getPluginNamespaces
AnnotatedClassDiscovery::getProviderFromNamespace protected function Extracts the provider name from a Drupal namespace.
AnnotatedClassDiscovery::prepareAnnotationDefinition protected function Prepares the annotation definition. Overrides AnnotatedClassDiscovery::prepareAnnotationDefinition 1
AnnotatedClassDiscovery::__construct public function Constructs an AnnotatedClassDiscovery object. Overrides AnnotatedClassDiscovery::__construct 1
DiscoveryTrait::doGetDefinition protected function Gets a specific plugin definition.
DiscoveryTrait::getDefinition public function 3
DiscoveryTrait::hasDefinition public function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.