From 24e3de0c843fe453e780572ef42c394a3be0947b Mon Sep 17 00:00:00 2001 From: Bram Goffings Date: Fri, 10 Aug 2012 14:58:02 +0200 Subject: [PATCH] annotations --- .../Drupal/Component/Reflection/MockFileFinder.php | 49 +++++++++++++ .../Drupal/Core/Annotation/AnnotationInterface.php | 20 ++++++ core/lib/Drupal/Core/Annotation/Plugin.php | 47 +++++++++++++ core/lib/Drupal/Core/Annotation/Translation.php | 47 +++++++++++++ .../Plugin/Discovery/AnnotatedClassDiscovery.php | 80 ++++++++++++++++++++++ core/modules/aggregator/aggregator.module | 13 ---- .../Drupal/aggregator/Plugin/FetcherManager.php | 5 +- .../Plugin/aggregator/fetcher/DefaultFetcher.php | 10 +++ 8 files changed, 256 insertions(+), 15 deletions(-) create mode 100644 core/lib/Drupal/Component/Reflection/MockFileFinder.php create mode 100644 core/lib/Drupal/Core/Annotation/AnnotationInterface.php create mode 100644 core/lib/Drupal/Core/Annotation/Plugin.php create mode 100644 core/lib/Drupal/Core/Annotation/Translation.php create mode 100644 core/lib/Drupal/Core/Plugin/Discovery/AnnotatedClassDiscovery.php diff --git a/core/lib/Drupal/Component/Reflection/MockFileFinder.php b/core/lib/Drupal/Component/Reflection/MockFileFinder.php new file mode 100644 index 0000000..c6f26b4 --- /dev/null +++ b/core/lib/Drupal/Component/Reflection/MockFileFinder.php @@ -0,0 +1,49 @@ +filename; + } + + /** + * Factory for the mock file finder. + */ + static public function create($filename) { + $object = new static(array()); + $object->filename = $filename; + return $object; + } + +} diff --git a/core/lib/Drupal/Core/Annotation/AnnotationInterface.php b/core/lib/Drupal/Core/Annotation/AnnotationInterface.php new file mode 100644 index 0000000..3382d48 --- /dev/null +++ b/core/lib/Drupal/Core/Annotation/AnnotationInterface.php @@ -0,0 +1,20 @@ + $value) { + if ($value instanceof AnnotationInterface) { + $this->definition[$key] = $value->get(); + } + else { + $this->definition[$key] = $value; + } + } + } + + /** + * Implements Drupal\Core\Annotation\AnnotationInterface::get(). + */ + public function get() { + return $this->definition; + } +} diff --git a/core/lib/Drupal/Core/Annotation/Translation.php b/core/lib/Drupal/Core/Annotation/Translation.php new file mode 100644 index 0000000..ebe886d --- /dev/null +++ b/core/lib/Drupal/Core/Annotation/Translation.php @@ -0,0 +1,47 @@ + $values['context'], + ); + } + $this->translation = t($string, array(), $options); + } + + /** + * Implements Drupal\Core\Annotation\AnnotationInterface::get(). + */ + public function get() { + return $this->translation; + } +} diff --git a/core/lib/Drupal/Core/Plugin/Discovery/AnnotatedClassDiscovery.php b/core/lib/Drupal/Core/Plugin/Discovery/AnnotatedClassDiscovery.php new file mode 100644 index 0000000..1ef5c46 --- /dev/null +++ b/core/lib/Drupal/Core/Plugin/Discovery/AnnotatedClassDiscovery.php @@ -0,0 +1,80 @@ +owner = $owner; + $this->type = $type; + } + + /** + * Implements Drupal\Component\Plugin\Discovery\DiscoveryInterface::getDefinition(). + */ + public function getDefinition($plugin_id) { + $plugins = $this->getDefinitions(); + return isset($plugins[$plugin_id]) ? $plugins[$plugin_id] : array(); + } + + /** + * Implements Drupal\Component\Plugin\Discovery\DiscoveryInterface::getDefinitions(). + */ + public function getDefinitions() { + $definitions = array(); + $reader = new AnnotationReader(); + AnnotationRegistry::registerAutoloadNamespace('Drupal\Core\Annotation', array(DRUPAL_ROOT . '/core/lib')); + $namespaces = drupal_classloader()->getNamespaces(); + foreach ($namespaces as $ns => $namespace_dirs) { + $ns = str_replace('\\', DIRECTORY_SEPARATOR, $ns); + foreach ($namespace_dirs as $dir) { + $prefix = implode(DIRECTORY_SEPARATOR, array( + $ns, + 'Plugin', + $this->owner, + $this->type + )); + $dir .= DIRECTORY_SEPARATOR . $prefix; + if (file_exists($dir)) { + $directories = new DirectoryIterator($dir); + foreach ($directories as $fileinfo) { + // @todo Once core requires 5.3.6 use $fileinfo->getExtension(). + if (pathinfo($fileinfo->getFilename(), PATHINFO_EXTENSION) == 'php') { + $class = str_replace(DIRECTORY_SEPARATOR, '\\', "$prefix/" . $fileinfo->getBasename('.php')); + // The file name is already known so no need to find the file, + // but StaticReflectionParser needs one so use a mock version. + $finder = MockFileFinder::create($fileinfo->getPathName()); + $parser = new StaticReflectionParser($class, $finder); + if ($annotation = $reader->getClassAnnotation($parser->getReflectionClass(), 'Drupal\Core\Annotation\Plugin')) { + $definition = $annotation->get(); + $definition['class'] = $class; + $definitions[$definition['id']] = $definition; + } + } + } + } + } + } + return $definitions; + } + +} diff --git a/core/modules/aggregator/aggregator.module b/core/modules/aggregator/aggregator.module index 723de86..047026a 100644 --- a/core/modules/aggregator/aggregator.module +++ b/core/modules/aggregator/aggregator.module @@ -801,19 +801,6 @@ function _aggregator_items($count) { } /** - * Implements hook_aggregator_fetch_info(). - */ -function aggregator_aggregator_fetch_info() { - return array( - 'aggregator' => array( - 'class' => 'Drupal\aggregator\Plugin\aggregator\fetcher\DefaultFetcher', - 'title' => t('Default fetcher'), - 'description' => t('Downloads data from a URL using Drupal\'s HTTP request handler.'), - ), - ); -} - -/** * Implements hook_preprocess_HOOK() for block.tpl.php. */ function aggregator_preprocess_block(&$variables) { diff --git a/core/modules/aggregator/lib/Drupal/aggregator/Plugin/FetcherManager.php b/core/modules/aggregator/lib/Drupal/aggregator/Plugin/FetcherManager.php index ae24934..583a03a 100644 --- a/core/modules/aggregator/lib/Drupal/aggregator/Plugin/FetcherManager.php +++ b/core/modules/aggregator/lib/Drupal/aggregator/Plugin/FetcherManager.php @@ -8,8 +8,9 @@ namespace Drupal\aggregator\Plugin; use Drupal\Component\Plugin\PluginManagerBase; -use Drupal\Core\Plugin\Discovery\HookDiscovery; use Drupal\Component\Plugin\Factory\DefaultFactory; +use Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery; +use Drupal\Core\Plugin\Discovery\CacheDecorator; /** * Manages aggregator fetcher plugins. @@ -17,7 +18,7 @@ use Drupal\Component\Plugin\Factory\DefaultFactory; class FetcherManager extends PluginManagerBase { public function __construct() { - $this->discovery = new HookDiscovery('aggregator_fetch_info'); + $this->discovery = new AnnotatedClassDiscovery('aggregator', 'fetcher'); $this->factory = new DefaultFactory($this->discovery); } } diff --git a/core/modules/aggregator/lib/Drupal/aggregator/Plugin/aggregator/fetcher/DefaultFetcher.php b/core/modules/aggregator/lib/Drupal/aggregator/Plugin/aggregator/fetcher/DefaultFetcher.php index e4b663b..583b0be 100644 --- a/core/modules/aggregator/lib/Drupal/aggregator/Plugin/aggregator/fetcher/DefaultFetcher.php +++ b/core/modules/aggregator/lib/Drupal/aggregator/Plugin/aggregator/fetcher/DefaultFetcher.php @@ -8,12 +8,22 @@ namespace Drupal\aggregator\Plugin\aggregator\fetcher; use Drupal\aggregator\Plugin\FetcherInterface; +use Drupal\Core\Annotation\Plugin; +use Drupal\Core\Annotation\Translation; /** * Defines a default fetcher implementation. * * Uses drupal_http_request() to download the feed. */ + +/** + * @Plugin( + * id = "aggregator", + * title = @Translation("Default fetcher"), + * description = @Translation("Downloads data from a URL using Drupal's HTTP request handler.") + * ) + */ class DefaultFetcher implements FetcherInterface { /** -- 1.7.11.msysgit.1