Commit e05902e5 authored by webchick's avatar webchick

Issue #2084513 by donquixote, EclipseGc: Annotation class loading could be more elegant.

parent 577a41d4
......@@ -26,13 +26,6 @@ class AnnotatedClassDiscovery implements DiscoveryInterface {
*/
protected $pluginNamespaces;
/**
* The namespaces of classes that can be used as annotations.
*
* @var array
*/
protected $annotationNamespaces;
/**
* The name of the annotation that contains the plugin definition.
*
......@@ -49,16 +42,12 @@ class AnnotatedClassDiscovery implements DiscoveryInterface {
* @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') {
function __construct($plugin_namespaces = array(), $plugin_definition_annotation_name = 'Drupal\Component\Annotation\Plugin') {
$this->pluginNamespaces = $plugin_namespaces;
$this->annotationNamespaces = $annotation_namespaces;
$this->pluginDefinitionAnnotationName = $plugin_definition_annotation_name;
}
......@@ -80,8 +69,10 @@ public function getDefinitions() {
$reader->addGlobalIgnoredName('endlink');
$reader->addGlobalIgnoredName('file');
// Clear the annotation loaders of any previous annotation classes.
AnnotationRegistry::reset();
// Register the namespaces of classes that can be used for annotations.
AnnotationRegistry::registerAutoloadNamespaces($this->getAnnotationNamespaces());
AnnotationRegistry::registerLoader('class_exists');
// Search for classes within all PSR-0 namespace locations.
foreach ($this->getPluginNamespaces() as $namespace => $dirs) {
......@@ -111,6 +102,10 @@ public function getDefinitions() {
}
}
}
// Don't let annotation loaders pile up.
AnnotationRegistry::reset();
return $definitions;
}
......@@ -121,11 +116,4 @@ protected function getPluginNamespaces() {
return $this->pluginNamespaces;
}
/**
* Returns an array of PSR-0 namespaces to search for annotation classes.
*/
protected function getAnnotationNamespaces() {
return $this->annotationNamespaces;
}
}
......@@ -28,7 +28,7 @@ class ActionManager extends PluginManagerBase {
* keyed by the corresponding namespace to look for plugin implementations.
*/
public function __construct(\Traversable $namespaces) {
$this->discovery = new AnnotatedClassDiscovery('Plugin/Action', $namespaces, array(), 'Drupal\Core\Annotation\Action');
$this->discovery = new AnnotatedClassDiscovery('Plugin/Action', $namespaces, 'Drupal\Core\Annotation\Action');
$this->discovery = new AlterDecorator($this->discovery, 'action_info');
$this->factory = new ContainerFactory($this);
......
......@@ -36,10 +36,7 @@ public function __construct(\Traversable $namespaces, CacheBackendInterface $cac
$this->alterInfo($module_handler, 'condition_info');
$this->setCacheBackend($cache_backend, $language_manager, 'condition');
$annotation_namespaces = array(
'Drupal\Core\Condition\Annotation' => DRUPAL_ROOT . '/core/lib',
);
parent::__construct('Plugin/Condition', $namespaces, $annotation_namespaces, 'Drupal\Core\Condition\Annotation\Condition');
parent::__construct('Plugin/Condition', $namespaces, 'Drupal\Core\Condition\Annotation\Condition');
}
/**
......
......@@ -143,10 +143,7 @@ public function __construct(\Traversable $namespaces, ContainerInterface $contai
}
protected function doDiscovery($namespaces) {
$annotation_namespaces = array(
'Drupal\Core\Entity\Annotation' => DRUPAL_ROOT . '/core/lib',
);
$this->discovery = new AnnotatedClassDiscovery('Entity', $namespaces, $annotation_namespaces, 'Drupal\Core\Entity\Annotation\EntityType');
$this->discovery = new AnnotatedClassDiscovery('Entity', $namespaces, 'Drupal\Core\Entity\Annotation\EntityType');
$this->discovery = new InfoHookDecorator($this->discovery, 'entity_info');
$this->discovery = new AlterDecorator($this->discovery, 'entity_info');
$this->discovery = new CacheDecorator($this->discovery, 'entity_info:' . $this->languageManager->getLanguage(Language::TYPE_INTERFACE)->id, 'cache', CacheBackendInterface::CACHE_PERMANENT, array('entity_info' => TRUE));
......
......@@ -42,10 +42,7 @@ class FieldTypePluginManager extends DefaultPluginManager {
* The module handler.
*/
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, LanguageManager $language_manager, ModuleHandlerInterface $module_handler) {
$annotation_namespaces = array(
'Drupal\Core\Entity\Annotation' => DRUPAL_ROOT . '/core/lib',
);
parent::__construct('Plugin/field/field_type', $namespaces, $annotation_namespaces, 'Drupal\Core\Entity\Annotation\FieldType');
parent::__construct('Plugin/field/field_type', $namespaces, 'Drupal\Core\Entity\Annotation\FieldType');
$this->alterInfo($module_handler, 'field_info');
$this->setCacheBackend($cache_backend, $language_manager, 'field_types');
......
......@@ -65,7 +65,7 @@ class LocalActionManager extends DefaultPluginManager {
* The language manager.
*/
public function __construct(\Traversable $namespaces, ControllerResolverInterface $controller_resolver, Request $request, ModuleHandlerInterface $module_handler, CacheBackendInterface $cache_backend, LanguageManager $language_manager) {
parent::__construct('Plugin/Menu/LocalAction', $namespaces, array(), 'Drupal\Core\Annotation\Menu\LocalAction');
parent::__construct('Plugin/Menu/LocalAction', $namespaces, 'Drupal\Core\Annotation\Menu\LocalAction');
$this->controllerResolver = $controller_resolver;
$this->request = $request;
......
......@@ -96,16 +96,13 @@ class DefaultPluginManager extends PluginManagerBase implements PluginManagerInt
* @param \Traversable $namespaces
* An object that implements \Traversable which contains the root paths
* keyed by the corresponding namespace to look for plugin implementations.
* @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'.
*/
public function __construct($subdir, \Traversable $namespaces, $annotation_namespaces = array(), $plugin_definition_annotation_name = 'Drupal\Component\Annotation\Plugin') {
public function __construct($subdir, \Traversable $namespaces, $plugin_definition_annotation_name = 'Drupal\Component\Annotation\Plugin') {
$this->subdir = $subdir;
$this->discovery = new AnnotatedClassDiscovery($subdir, $namespaces, $annotation_namespaces, $plugin_definition_annotation_name);
$this->discovery = new AnnotatedClassDiscovery($subdir, $namespaces, $plugin_definition_annotation_name);
$this->discovery = new ContainerDerivativeDiscoveryDecorator($this->discovery);
$this->factory = new ContainerFactory($this);
}
......
......@@ -2,7 +2,7 @@
/**
* @file
* Definition of Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery.
* Contains \Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery.
*/
namespace Drupal\Core\Plugin\Discovery;
......@@ -41,24 +41,17 @@ class AnnotatedClassDiscovery extends ComponentAnnotatedClassDiscovery {
* 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 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($subdir, \Traversable $root_namespaces, $annotation_namespaces = array(), $plugin_definition_annotation_name = 'Drupal\Component\Annotation\Plugin') {
function __construct($subdir, \Traversable $root_namespaces, $plugin_definition_annotation_name = 'Drupal\Component\Annotation\Plugin') {
if ($subdir) {
$this->subdir = str_replace('/', '\\', $subdir);
}
$this->rootNamespacesIterator = $root_namespaces;
$annotation_namespaces += array(
'Drupal\Component\Annotation' => DRUPAL_ROOT . '/core/lib',
'Drupal\Core\Annotation' => DRUPAL_ROOT . '/core/lib',
);
$plugin_namespaces = array();
parent::__construct($plugin_namespaces, $annotation_namespaces, $plugin_definition_annotation_name);
parent::__construct($plugin_namespaces, $plugin_definition_annotation_name);
}
/**
......
......@@ -49,10 +49,7 @@ public function __construct(\Traversable $namespaces, CacheBackendInterface $cac
$this->alterInfo($module_handler, 'data_type_info');
$this->setCacheBackend($cache_backend, $language_manager, 'typed_data:types');
$annotation_namespaces = array(
'Drupal\Core\TypedData\Annotation' => DRUPAL_ROOT . '/core/lib',
);
parent::__construct('Plugin/DataType', $namespaces, $annotation_namespaces, 'Drupal\Core\TypedData\Annotation\DataType');
parent::__construct('Plugin/DataType', $namespaces, 'Drupal\Core\TypedData\Annotation\DataType');
}
/**
......
......@@ -36,11 +36,7 @@ public function __construct($type, \Traversable $namespaces, CacheBackendInterfa
'processor' => 'Drupal\aggregator\Annotation\AggregatorProcessor',
);
$annotation_namespaces = array(
'Drupal\aggregator\Annotation' => DRUPAL_ROOT . '/core/modules/aggregator/lib',
);
parent::__construct("Plugin/aggregator/$type", $namespaces, $annotation_namespaces, $type_annotations[$type]);
parent::__construct("Plugin/aggregator/$type", $namespaces, $type_annotations[$type]);
$this->setCacheBackend($cache_backend, $language_manager, "aggregator_$type");
}
......
......@@ -52,8 +52,7 @@ class BlockManager extends DefaultPluginManager {
* The translation manager.
*/
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, LanguageManager $language_manager, ModuleHandlerInterface $module_handler, TranslationInterface $translation_manager) {
$annotation_namespaces = array('Drupal\block\Annotation' => $namespaces['Drupal\block']);
parent::__construct('Plugin/Block', $namespaces, $annotation_namespaces, 'Drupal\block\Annotation\Block');
parent::__construct('Plugin/Block', $namespaces, 'Drupal\block\Annotation\Block');
$this->alterInfo($module_handler, 'block');
$this->setCacheBackend($cache_backend, $language_manager, 'block_plugins');
......
......@@ -33,8 +33,7 @@ class CKEditorPluginManager extends DefaultPluginManager {
* The module handler to invoke the alter hook with.
*/
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, LanguageManager $language_manager, ModuleHandlerInterface $module_handler) {
$annotation_namespaces = array('Drupal\ckeditor\Annotation' => $namespaces['Drupal\ckeditor']);
parent::__construct('Plugin/CKEditorPlugin', $namespaces, $annotation_namespaces, 'Drupal\ckeditor\Annotation\CKEditorPlugin');
parent::__construct('Plugin/CKEditorPlugin', $namespaces, 'Drupal\ckeditor\Annotation\CKEditorPlugin');
$this->alterInfo($module_handler, 'ckeditor_plugin_info');
$this->setCacheBackend($cache_backend, $language_manager, 'ckeditor_plugin');
}
......
......@@ -29,8 +29,7 @@ class InPlaceEditorManager extends PluginManagerBase {
* keyed by the corresponding namespace to look for plugin implementations,
*/
public function __construct(\Traversable $namespaces) {
$annotation_namespaces = array('Drupal\edit\Annotation' => $namespaces['Drupal\edit']);
$this->discovery = new AnnotatedClassDiscovery('Plugin/InPlaceEditor', $namespaces, $annotation_namespaces, 'Drupal\edit\Annotation\InPlaceEditor');
$this->discovery = new AnnotatedClassDiscovery('Plugin/InPlaceEditor', $namespaces, 'Drupal\edit\Annotation\InPlaceEditor');
$this->discovery = new AlterDecorator($this->discovery, 'edit_editor');
$this->discovery = new CacheDecorator($this->discovery, 'edit:editor');
$this->factory = new DefaultFactory($this->discovery);
......
......@@ -31,8 +31,7 @@ class EditorManager extends DefaultPluginManager {
* The module handler to invoke the alter hook with.
*/
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, LanguageManager $language_manager, ModuleHandlerInterface $module_handler) {
$annotation_namespaces = array('Drupal\editor\Annotation' => $namespaces['Drupal\editor']);
parent::__construct('Plugin/Editor', $namespaces, $annotation_namespaces, 'Drupal\editor\Annotation\Editor');
parent::__construct('Plugin/Editor', $namespaces, 'Drupal\editor\Annotation\Editor');
$this->alterInfo($module_handler, 'editor_info');
$this->setCacheBackend($cache_backend, $language_manager, 'editor');
}
......
......@@ -27,8 +27,7 @@ class SelectionPluginManager extends DefaultPluginManager {
* {@inheritdoc}
*/
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, LanguageManager $language_manager, ModuleHandlerInterface $module_handler) {
$annotation_namespaces = array('Drupal\entity_reference\Annotation' => $namespaces['Drupal\entity_reference']);
$this->discovery = new AnnotatedClassDiscovery('Plugin/entity_reference/selection', $namespaces, $annotation_namespaces, 'Drupal\entity_reference\Annotation\EntityReferenceSelection');
$this->discovery = new AnnotatedClassDiscovery('Plugin/entity_reference/selection', $namespaces, 'Drupal\entity_reference\Annotation\EntityReferenceSelection');
// We're not using the parent constructor because we use a different factory
// method and don't need the derivative discovery decorator.
......
......@@ -54,9 +54,8 @@ class FormatterPluginManager extends DefaultPluginManager {
* The 'field type' plugin manager.
*/
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler, LanguageManager $language_manager, FieldTypePluginManager $field_type_manager) {
$annotation_namespaces = array('Drupal\field\Annotation' => $namespaces['Drupal\field']);
parent::__construct('Plugin/field/formatter', $namespaces, $annotation_namespaces, 'Drupal\field\Annotation\FieldFormatter');
parent::__construct('Plugin/field/formatter', $namespaces, 'Drupal\field\Annotation\FieldFormatter');
$this->setCacheBackend($cache_backend, $language_manager, 'field_formatter_types');
$this->alterInfo($module_handler, 'field_formatter_info');
......
......@@ -54,9 +54,7 @@ class WidgetPluginManager extends DefaultPluginManager {
* The 'field type' plugin manager.
*/
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler, LanguageManager $language_manager, FieldTypePluginManager $field_type_manager) {
$annotation_namespaces = array('Drupal\field\Annotation' => $namespaces['Drupal\field']);
parent::__construct('Plugin/field/widget', $namespaces, $annotation_namespaces, 'Drupal\field\Annotation\FieldWidget');
parent::__construct('Plugin/field/widget', $namespaces, 'Drupal\field\Annotation\FieldWidget');
$this->setCacheBackend($cache_backend, $language_manager, 'field_widget_types');
$this->alterInfo($module_handler, 'field_widget_info');
......
......@@ -39,8 +39,7 @@ class FilterPluginManager extends DefaultPluginManager {
* The module handler to invoke the alter hook with.
*/
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, LanguageManager $language_manager, ModuleHandlerInterface $module_handler) {
$annotation_namespaces = array('Drupal\filter\Annotation' => $namespaces['Drupal\filter']);
parent::__construct('Plugin/Filter', $namespaces, $annotation_namespaces, 'Drupal\filter\Annotation\Filter');
parent::__construct('Plugin/Filter', $namespaces, 'Drupal\filter\Annotation\Filter');
$this->alterInfo($module_handler, 'filter_info');
$this->setCacheBackend($cache_backend, $language_manager, 'filter_plugins', array('filter_formats' => TRUE));
}
......
......@@ -21,8 +21,7 @@ class ImageEffectManager extends DefaultPluginManager {
* {@inheritdoc}
*/
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, LanguageManager $language_manager, ModuleHandlerInterface $module_handler) {
$annotation_namespaces = array('Drupal\image\Annotation' => $namespaces['Drupal\image']);
parent::__construct('Plugin/ImageEffect', $namespaces, $annotation_namespaces, 'Drupal\image\Annotation\ImageEffect');
parent::__construct('Plugin/ImageEffect', $namespaces, 'Drupal\image\Annotation\ImageEffect');
$this->alterInfo($module_handler, 'image_effect_info');
$this->setCacheBackend($cache_backend, $language_manager, 'image_effect');
......
......@@ -28,8 +28,7 @@ class SearchPluginManager extends DefaultPluginManager {
* {@inheritdoc}
*/
public function __construct(\Traversable $namespaces, ConfigFactory $config_factory) {
$annotation_namespaces = array('Drupal\search\Annotation' => $namespaces['Drupal\search']);
parent::__construct('Plugin/Search', $namespaces, $annotation_namespaces, 'Drupal\search\Annotation\SearchPlugin');
parent::__construct('Plugin/Search', $namespaces, 'Drupal\search\Annotation\SearchPlugin');
$this->configFactory = $config_factory;
}
......
......@@ -28,8 +28,7 @@ class ImageToolkitManager extends DefaultPluginManager {
* The language manager.
*/
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, LanguageManager $language_manager) {
$annotation_namespaces = array('Drupal\system\Annotation' => $namespaces['Drupal\system']);
parent::__construct('Plugin/ImageToolkit', $namespaces, $annotation_namespaces, 'Drupal\system\Annotation\ImageToolkit');
parent::__construct('Plugin/ImageToolkit', $namespaces, 'Drupal\system\Annotation\ImageToolkit');
$this->setCacheBackend($cache_backend, $language_manager, 'image_toolkit');
}
......
......@@ -42,12 +42,9 @@ protected function setUp() {
),
);
$root_namespaces = new \ArrayObject(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/plugin_test/custom_annotation', $root_namespaces, $annotation_namespaces, 'Drupal\plugin_test\Plugin\Annotation\PluginExample');
$this->emptyDiscovery = new AnnotatedClassDiscovery('Plugin/non_existing_module/non_existing_plugin_type', $root_namespaces, $annotation_namespaces, 'Drupal\plugin_test\Plugin\Annotation\PluginExample');
$this->discovery = new AnnotatedClassDiscovery('Plugin/plugin_test/custom_annotation', $root_namespaces, 'Drupal\plugin_test\Plugin\Annotation\PluginExample');
$this->emptyDiscovery = new AnnotatedClassDiscovery('Plugin/non_existing_module/non_existing_plugin_type', $root_namespaces, 'Drupal\plugin_test\Plugin\Annotation\PluginExample');
}
}
......@@ -31,8 +31,7 @@ class TipPluginManager extends DefaultPluginManager {
* The module handler to invoke the alter hook with.
*/
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, LanguageManager $language_manager, ModuleHandlerInterface $module_handler) {
$annotation_namespaces = array('Drupal\tour\Annotation' => $namespaces['Drupal\tour']);
parent::__construct('Plugin/tour/tip', $namespaces, $annotation_namespaces, 'Drupal\tour\Annotation\Tip');
parent::__construct('Plugin/tour/tip', $namespaces, 'Drupal\tour\Annotation\Tip');
$this->alterInfo($module_handler, 'tour_tips_info');
$this->setCacheBackend($cache_backend, $language_manager, 'tour');
......
......@@ -41,16 +41,12 @@ function __construct($type, \Traversable $root_namespaces) {
$this->type = $type;
$this->rootNamespacesIterator = $root_namespaces;
$annotation_namespaces = array(
'Drupal\Component\Annotation' => DRUPAL_ROOT . '/core/lib',
);
$plugin_namespaces = array();
foreach ($root_namespaces as $namespace => $dir) {
$plugin_namespaces["$namespace\\Plugin\\views\\{$type}"] = array($dir);
}
$this->pluginNamespaces = $plugin_namespaces;
$this->annotationNamespaces = $annotation_namespaces;
$this->pluginDefinitionAnnotationName = 'Drupal\Component\Annotation\PluginID';
}
......
......@@ -34,9 +34,8 @@ class ViewsPluginManager extends DefaultPluginManager {
* The module handler to invoke the alter hook with.
*/
public function __construct($type, \Traversable $namespaces, CacheBackendInterface $cache_backend, LanguageManager $language_manager, ModuleHandlerInterface $module_handler) {
$annotation_namespaces = array('Drupal\views\Annotation' => $namespaces['Drupal\views']);
$plugin_definition_annotation_name = 'Drupal\views\Annotation\Views' . Container::camelize($type);
parent::__construct("Plugin/views/$type", $namespaces, $annotation_namespaces, $plugin_definition_annotation_name);
parent::__construct("Plugin/views/$type", $namespaces, $plugin_definition_annotation_name);
$this->defaults += array(
'parent' => 'parent',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment