summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2016-04-04 18:07:08 (GMT)
committerAlex Pott2016-04-04 18:07:08 (GMT)
commitfa4ec00fd13322fee4e2d32def0d3e334908d040 (patch)
treed2f84c7ec61e3ad08367a9c238ec000414f672d5
parentd466fb2cf580690da9f0102495c615bbbace4620 (diff)
Issue #2600926 by benjy, mikeryan, phenaproxima, hussainweb, alvar0hurtad0: Allow annotations to inherit across namespaces
-rw-r--r--core/lib/Drupal/Component/Annotation/Plugin/Discovery/AnnotatedClassDiscovery.php17
-rw-r--r--core/lib/Drupal/Core/Plugin/DefaultPluginManager.php19
-rw-r--r--core/lib/Drupal/Core/Plugin/Discovery/AnnotatedClassDiscovery.php6
-rw-r--r--core/modules/system/src/Tests/Plugin/Discovery/AnnotatedClassDiscoveryTest.php13
-rw-r--r--core/modules/system/tests/modules/plugin_test_extended/plugin_test_extended.info.yml6
-rw-r--r--core/modules/system/tests/modules/plugin_test_extended/src/Plugin/Annotation/PluginExtended.php19
-rw-r--r--core/modules/system/tests/modules/plugin_test_extended/src/Plugin/plugin_test/fruit/BigApple.php19
7 files changed, 90 insertions, 9 deletions
diff --git a/core/lib/Drupal/Component/Annotation/Plugin/Discovery/AnnotatedClassDiscovery.php b/core/lib/Drupal/Component/Annotation/Plugin/Discovery/AnnotatedClassDiscovery.php
index edfce73..11ed449 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 = [];
+
+ /**
* 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 = []) {
$this->pluginNamespaces = $plugin_namespaces;
$this->pluginDefinitionAnnotationName = $plugin_definition_annotation_name;
+ $this->annotationNamespaces = $annotation_namespaces;
}
/**
@@ -74,6 +84,11 @@ class AnnotatedClassDiscovery implements DiscoveryInterface {
// 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.
+ foreach ($this->annotationNamespaces as $namespace) {
+ $this->annotationReader->addNamespace($namespace);
+ }
}
return $this->annotationReader;
}
diff --git a/core/lib/Drupal/Core/Plugin/DefaultPluginManager.php b/core/lib/Drupal/Core/Plugin/DefaultPluginManager.php
index 26b7161..5e8d89f 100644
--- a/core/lib/Drupal/Core/Plugin/DefaultPluginManager.php
+++ b/core/lib/Drupal/Core/Plugin/DefaultPluginManager.php
@@ -42,7 +42,7 @@ class DefaultPluginManager extends PluginManagerBase implements PluginManagerInt
*
* @var array
*/
- protected $cacheTags = array();
+ protected $cacheTags = [];
/**
* Name of the alter hook if one should be invoked.
@@ -73,7 +73,7 @@ class DefaultPluginManager extends PluginManagerBase implements PluginManagerInt
*
* @var array
*/
- protected $defaults = array();
+ protected $defaults = [];
/**
* The name of the annotation that contains the plugin definition.
@@ -98,6 +98,14 @@ class DefaultPluginManager extends PluginManagerBase implements PluginManagerInt
protected $namespaces;
/**
+ * Additional namespaces the annotation discovery mechanism should scan for
+ * annotation definitions.
+ *
+ * @var string[]
+ */
+ protected $additionalAnnotationNamespaces = [];
+
+ /**
* Creates the discovery object.
*
* @param string|bool $subdir
@@ -112,13 +120,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[] $additional_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 $additional_annotation_namespaces = []) {
$this->subdir = $subdir;
$this->namespaces = $namespaces;
$this->pluginDefinitionAnnotationName = $plugin_definition_annotation_name;
$this->pluginInterface = $plugin_interface;
$this->moduleHandler = $module_handler;
+ $this->additionalAnnotationNamespaces = $additional_annotation_namespaces;
}
/**
@@ -242,7 +253,7 @@ class DefaultPluginManager extends PluginManagerBase implements PluginManagerInt
*/
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->additionalAnnotationNamespaces);
$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 8cb4888..d0de724 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 = []) {
if ($subdir) {
// Prepend a directory separator to $subdir,
// if it does not already have one.
@@ -65,7 +67,7 @@ class AnnotatedClassDiscovery extends ComponentAnnotatedClassDiscovery {
}
$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);
}
/**
diff --git a/core/modules/system/src/Tests/Plugin/Discovery/AnnotatedClassDiscoveryTest.php b/core/modules/system/src/Tests/Plugin/Discovery/AnnotatedClassDiscoveryTest.php
index 6cca8f5..d038dbf 100644
--- a/core/modules/system/src/Tests/Plugin/Discovery/AnnotatedClassDiscoveryTest.php
+++ b/core/modules/system/src/Tests/Plugin/Discovery/AnnotatedClassDiscoveryTest.php
@@ -57,12 +57,21 @@ class AnnotatedClassDiscoveryTest extends DiscoveryTestBase {
'class' => 'Drupal\plugin_test\Plugin\plugin_test\fruit\Orange',
'provider' => 'plugin_test',
),
+ 'big_apple' => array(
+ 'id' => 'big_apple',
+ 'label' => 'Big Apple',
+ 'color' => 'green',
+ 'class' => 'Drupal\plugin_test_extended\Plugin\plugin_test\fruit\BigApple',
+ 'provider' => 'plugin_test_extended',
+ ),
);
$base_directory = \Drupal::root() . '/core/modules/system/tests/modules/plugin_test/src';
- $namespaces = new \ArrayObject(array('Drupal\plugin_test' => $base_directory));
+ $base_directory2 = \Drupal::root() . '/core/modules/system/tests/modules/plugin_test_extended/src';
+ $namespaces = new \ArrayObject(array('Drupal\plugin_test' => $base_directory, 'Drupal\plugin_test_extended' => $base_directory2));
- $this->discovery = new AnnotatedClassDiscovery('Plugin/plugin_test/fruit', $namespaces);
+ $annotation_namespaces = ['Drupal\plugin_test\Plugin\Annotation', 'Drupal\plugin_test_extended\Plugin\Annotation'];
+ $this->discovery = new AnnotatedClassDiscovery('Plugin/plugin_test/fruit', $namespaces, 'Drupal\Component\Annotation\Plugin', $annotation_namespaces);
$this->emptyDiscovery = new AnnotatedClassDiscovery('Plugin/non_existing_module/non_existing_plugin_type', $namespaces);
}
diff --git a/core/modules/system/tests/modules/plugin_test_extended/plugin_test_extended.info.yml b/core/modules/system/tests/modules/plugin_test_extended/plugin_test_extended.info.yml
new file mode 100644
index 0000000..f3d0a5b
--- /dev/null
+++ b/core/modules/system/tests/modules/plugin_test_extended/plugin_test_extended.info.yml
@@ -0,0 +1,6 @@
+name: 'Plugin Test Extended'
+type: module
+description: 'Test annotations can extend other annotations in a different namespace.'
+package: Testing
+version: VERSION
+core: 8.x
diff --git a/core/modules/system/tests/modules/plugin_test_extended/src/Plugin/Annotation/PluginExtended.php b/core/modules/system/tests/modules/plugin_test_extended/src/Plugin/Annotation/PluginExtended.php
new file mode 100644
index 0000000..d8139e8
--- /dev/null
+++ b/core/modules/system/tests/modules/plugin_test_extended/src/Plugin/Annotation/PluginExtended.php
@@ -0,0 +1,19 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\plugin_test_extended\Plugin\Annotation\PluginExampleExtended
+ */
+
+namespace Drupal\plugin_test_extended\Plugin\Annotation;
+
+use Drupal\Component\Annotation\Plugin;
+
+/**
+ * Defines a test annotation that extends an annotation in another namespace.
+ *
+ * @Annotation
+ */
+class PluginExtended extends Plugin {
+
+}
diff --git a/core/modules/system/tests/modules/plugin_test_extended/src/Plugin/plugin_test/fruit/BigApple.php b/core/modules/system/tests/modules/plugin_test_extended/src/Plugin/plugin_test/fruit/BigApple.php
new file mode 100644
index 0000000..001e405
--- /dev/null
+++ b/core/modules/system/tests/modules/plugin_test_extended/src/Plugin/plugin_test/fruit/BigApple.php
@@ -0,0 +1,19 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\plugin_test_extended\Plugin\plugin_test\fruit\BigApple
+ */
+
+namespace Drupal\plugin_test_extended\Plugin\plugin_test\fruit;
+
+/**
+ * @PluginExtended(
+ * id = "big_apple",
+ * label = "Big Apple",
+ * color = "green"
+ * )
+ */
+class BigApple {
+
+}