diff --git a/core/lib/Drupal/Component/Annotation/Plugin/Discovery/AnnotatedClassDiscovery.php b/core/lib/Drupal/Component/Annotation/Plugin/Discovery/AnnotatedClassDiscovery.php index edfce73..a073665 100644 --- a/core/lib/Drupal/Component/Annotation/Plugin/Discovery/AnnotatedClassDiscovery.php +++ b/core/lib/Drupal/Component/Annotation/Plugin/Discovery/AnnotatedClassDiscovery.php @@ -47,6 +47,13 @@ class AnnotatedClassDiscovery implements DiscoveryInterface { protected $annotationReader; /** + * Additional namespaces to be scanned for annotation classes. + * + * @var string[] + */ + protected $annotationNamespaces = array(); + + /** * Constructs a new instance. * * @param string[] $plugin_namespaces @@ -55,10 +62,13 @@ class AnnotatedClassDiscovery implements DiscoveryInterface { * @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 be scanned for annotation classes. */ - function __construct($plugin_namespaces = array(), $plugin_definition_annotation_name = 'Drupal\Component\Annotation\Plugin') { + function __construct($plugin_namespaces = array(), $plugin_definition_annotation_name = 'Drupal\Component\Annotation\Plugin', array $annotation_namespaces = array()) { $this->pluginNamespaces = $plugin_namespaces; $this->pluginDefinitionAnnotationName = $plugin_definition_annotation_name; + $this->annotationNamespaces = $annotation_namespaces; } /** @@ -74,6 +84,8 @@ protected function getAnnotationReader() { // Add the namespaces from the main plugin annotation, like @EntityType. $namespace = substr($this->pluginDefinitionAnnotationName, 0, strrpos($this->pluginDefinitionAnnotationName, '\\')); $this->annotationReader->addNamespace($namespace); + // Register additional namespaces to be scanned for annotations. + array_walk($this->annotationNamespaces, [$this->annotationReader, 'addNamespace']); } return $this->annotationReader; } diff --git a/core/lib/Drupal/Core/Plugin/DefaultPluginManager.php b/core/lib/Drupal/Core/Plugin/DefaultPluginManager.php index 5b7b86b..14e9b2a 100644 --- a/core/lib/Drupal/Core/Plugin/DefaultPluginManager.php +++ b/core/lib/Drupal/Core/Plugin/DefaultPluginManager.php @@ -110,6 +110,14 @@ class DefaultPluginManager extends PluginManagerBase implements PluginManagerInt protected $namespaces; /** + * Additional namespaces the annotation discovery mechanism should scan for + * annotation definitions. + * + * @var string[] + */ + protected $annotationNamespaces = array(); + + /** * Creates the discovery object. * * @param string|bool $subdir @@ -124,13 +132,16 @@ class DefaultPluginManager extends PluginManagerBase implements PluginManagerInt * @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 $namespaces, ModuleHandlerInterface $module_handler, $plugin_interface = NULL, $plugin_definition_annotation_name = 'Drupal\Component\Annotation\Plugin') { + public function __construct($subdir, \Traversable $namespaces, ModuleHandlerInterface $module_handler, $plugin_interface = NULL, $plugin_definition_annotation_name = 'Drupal\Component\Annotation\Plugin', array $annotation_namespaces = array()) { $this->subdir = $subdir; $this->namespaces = $namespaces; $this->pluginDefinitionAnnotationName = $plugin_definition_annotation_name; $this->pluginInterface = $plugin_interface; $this->moduleHandler = $module_handler; + $this->annotationNamespaces = $annotation_namespaces; } /** @@ -200,6 +211,18 @@ public function clearCachedDefinitions() { } /** + * Adds a namespace to be scanned for annotation definitions. + * + * @param string $namespace + * The namespace to add. + */ + public function addAnnotationNamespace($namespace) { + if (!in_array($namespace, $this->annotationNamespaces)) { + array_push($this->annotationNamespaces, $namespace); + } + } + + /** * Returns the cached plugin definitions of the decorated discovery class. * * @return array|null @@ -259,7 +282,6 @@ protected function cacheSet($cid, $data, $expire = Cache::PERMANENT, array $tags } } - /** * Performs extra processing on plugin definitions. * @@ -278,7 +300,7 @@ public function processDefinition(&$definition, $plugin_id) { */ protected function getDiscovery() { if (!$this->discovery) { - $discovery = new AnnotatedClassDiscovery($this->subdir, $this->namespaces, $this->pluginDefinitionAnnotationName); + $discovery = new AnnotatedClassDiscovery($this->subdir, $this->namespaces, $this->pluginDefinitionAnnotationName, $this->annotationNamespaces); $this->discovery = new ContainerDerivativeDiscoveryDecorator($discovery); } return $this->discovery; diff --git a/core/lib/Drupal/Core/Plugin/Discovery/AnnotatedClassDiscovery.php b/core/lib/Drupal/Core/Plugin/Discovery/AnnotatedClassDiscovery.php index 6bc3bbb..bd9043c 100644 --- a/core/lib/Drupal/Core/Plugin/Discovery/AnnotatedClassDiscovery.php +++ b/core/lib/Drupal/Core/Plugin/Discovery/AnnotatedClassDiscovery.php @@ -52,8 +52,10 @@ class AnnotatedClassDiscovery extends ComponentAnnotatedClassDiscovery { * @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. */ - function __construct($subdir, \Traversable $root_namespaces, $plugin_definition_annotation_name = 'Drupal\Component\Annotation\Plugin') { + function __construct($subdir, \Traversable $root_namespaces, $plugin_definition_annotation_name = 'Drupal\Component\Annotation\Plugin', array $annotation_namespaces = array()) { if ($subdir) { // Prepend a directory separator to $subdir, // if it does not already have one. @@ -65,7 +67,7 @@ function __construct($subdir, \Traversable $root_namespaces, $plugin_definition_ } $this->rootNamespacesIterator = $root_namespaces; $plugin_namespaces = array(); - parent::__construct($plugin_namespaces, $plugin_definition_annotation_name); + parent::__construct($plugin_namespaces, $plugin_definition_annotation_name, $annotation_namespaces); } /**