Issue #1967420 by tim.plunkett, dawehner: Allow Core\AnnotatedClassDiscovery to pass all parameters to the constructor of Component\AnnotatedClassDiscovery.

8.0.x
webchick 2013-04-11 22:59:05 -07:00
parent b7b6bc5e63
commit 0c1777a91d
6 changed files with 156 additions and 6 deletions

View File

@ -45,6 +45,16 @@ class AnnotatedClassDiscovery implements DiscoveryInterface {
/**
* 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;

View File

@ -22,9 +22,15 @@ class AnnotatedClassDiscovery extends ComponentAnnotatedClassDiscovery {
* @param string $type
* The plugin type, for example filter.
* @param array $root_namespaces
* Array of root paths keyed by the corresponding namespace to look for
* plugin implementations, \Plugin\$owner\$type will be appended to each
* namespace.
* (optional) An array of root paths keyed by the corresponding namespace to
* look for plugin implementations. '\Plugin\$owner\$type' will be appended
* to each namespace. 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'.
*
* @todo Figure out how to make the following comment FALSE.
* Drupal modules can be enabled (and therefore, namespaces registered)
@ -33,8 +39,8 @@ class AnnotatedClassDiscovery extends ComponentAnnotatedClassDiscovery {
* until the next request. Additionally when a namespace is unregistered,
* plugins will not be removed until the next request.
*/
function __construct($owner, $type, array $root_namespaces = array()) {
$annotation_namespaces = array(
function __construct($owner, $type, array $root_namespaces = array(), $annotation_namespaces = array(), $plugin_definition_annotation_name = 'Drupal\Component\Annotation\Plugin') {
$annotation_namespaces += array(
'Drupal\Component\Annotation' => DRUPAL_ROOT . '/core/lib',
'Drupal\Core\Annotation' => DRUPAL_ROOT . '/core/lib',
);
@ -42,7 +48,7 @@ class AnnotatedClassDiscovery extends ComponentAnnotatedClassDiscovery {
foreach ($root_namespaces as $namespace => $dir) {
$plugin_namespaces["$namespace\\Plugin\\{$owner}\\{$type}"] = array($dir);
}
parent::__construct($plugin_namespaces, $annotation_namespaces);
parent::__construct($plugin_namespaces, $annotation_namespaces, $plugin_definition_annotation_name);
}
}

View File

@ -0,0 +1,51 @@
<?php
/**
* @file
* Contains \Drupal\system\Tests\Plugin\Discovery\CustomAnnotationClassDiscoveryTest.
*/
namespace Drupal\system\Tests\Plugin\Discovery;
use Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery;
/**
* Tests that a custom annotation class is used.
*
* @see \Drupal\plugin_test\Plugin\Annotation\PluginExample
*/
class CustomAnnotationClassDiscoveryTest extends DiscoveryTestBase {
public static function getInfo() {
return array(
'name' => 'Custom annotation class discovery',
'description' => 'Tests that a custom annotation class is used.',
'group' => 'Plugin API',
);
}
protected function setUp() {
parent::setUp();
$this->expectedDefinitions = array(
'example_1' => array(
'id' => 'example_1',
'custom' => 'John',
'class' => 'Drupal\plugin_test\Plugin\plugin_test\custom_annotation\Example1',
),
'example_2' => array(
'id' => 'example_2',
'custom' => 'Paul',
'class' => 'Drupal\plugin_test\Plugin\plugin_test\custom_annotation\Example2',
),
);
$root_namespaces = array('Drupal\plugin_test' => DRUPAL_ROOT . '/core/modules/system/tests/modules/plugin_test/lib');
$annotation_namespaces = array(
'Drupal\plugin_test\Plugin\Annotation' => DRUPAL_ROOT . '/core/modules/system/tests/modules/plugin_test/lib',
);
$this->discovery = new AnnotatedClassDiscovery('plugin_test', 'custom_annotation', $root_namespaces, $annotation_namespaces, 'Drupal\plugin_test\Plugin\Annotation\PluginExample');
$this->emptyDiscovery = new AnnotatedClassDiscovery('non_existing_module', 'non_existing_plugin_type', $root_namespaces, $annotation_namespaces, 'Drupal\plugin_test\Plugin\Annotation\PluginExample');
}
}

View File

@ -0,0 +1,43 @@
<?php
/**
* @file
* Contains \Drupal\plugin_test\Plugin\Annotation\PluginExample.
*/
namespace Drupal\plugin_test\Plugin\Annotation;
use Drupal\Component\Annotation\AnnotationInterface;
/**
* Defines a custom Plugin annotation.
*
* @Annotation
*/
class PluginExample implements AnnotationInterface {
/**
* The plugin ID.
*
* @var string
*/
public $id;
/**
* Another plugin metadata.
*
* @var string
*/
public $custom;
/**
* {@inheritdoc}
*/
public function get() {
return array(
'id' => $this->id,
'custom' => $this->custom,
);
}
}

View File

@ -0,0 +1,20 @@
<?php
/**
* @file
* Contains \Drupal\plugin_test\Plugin\plugin_test\custom_annotation\Example1.
*/
namespace Drupal\plugin_test\Plugin\plugin_test\custom_annotation;
use Drupal\plugin_test\Plugin\Annotation\PluginExample;
/**
* Provides a test plugin with a custom annotation.
*
* @PluginExample(
* id = "example_1",
* custom = "John"
* )
*/
class Example1 {}

View File

@ -0,0 +1,20 @@
<?php
/**
* @file
* Contains \Drupal\plugin_test\Plugin\plugin_test\custom_annotation\Example2.
*/
namespace Drupal\plugin_test\Plugin\plugin_test\custom_annotation;
use Drupal\plugin_test\Plugin\Annotation\PluginExample;
/**
* Provides a test plugin with a custom annotation.
*
* @PluginExample(
* id = "example_2",
* custom = "Paul"
* )
*/
class Example2 {}