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\\\\(?[\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; } }