Skip to content

Instantly share code, notes, and snippets.

@EclipseGc
Created August 3, 2012 04:22
Show Gist options
  • Save EclipseGc/3244343 to your computer and use it in GitHub Desktop.
Save EclipseGc/3244343 to your computer and use it in GitHub Desktop.
<?php
/**
* @file
* Definition of Drupal\Component\Plugin\Discovery\AnnotatedClassDiscovery.
*/
namespace Drupal\Core\Plugin\Discovery;
use Drupal\Component\Plugin\Discovery\DiscoveryInterface;
use Drupal\Core\Annotation\DirectoryIterator;
use ReflectionClass;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\AnnotationRegistry;
use Drupal\Core\Annotation\Plugin;
/**
* A discovery mechanism finds annotated plugins in PSR-0 namespaces.
*/
class AnnotatedClassDiscovery implements DiscoveryInterface {
function __construct($owner, $type) {
$this->owner = $owner;
$this->type = $type;
}
/**
* Implements DiscoveryInterface::getDefinition().
*/
public function getDefinition($plugin_id) {
$plugins = $this->getDefinitions();
return isset($plugins[$plugin_id]) ? $plugins[$plugin_id] : array();
}
/**
* Implements 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) {
if ($fileinfo->getExtension() == 'php') {
$reflectionClass = new ReflectionClass(str_replace(DIRECTORY_SEPARATOR, '\\', "$prefix/". $fileinfo->getBasename('.php')));
if ($annotation = $reader->getClassAnnotation($reflectionClass, 'Drupal\Core\Annotation\Plugin')) {
$definition = $annotation->get();
$definition['class'] = $reflectionClass->name;
$definitions[$definition['plugin_id']] = $definition;
}
}
}
}
}
}
return $definitions;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment