AnnotatedClassDiscovery.php in Service Container 7.2
File
modules/providers/service_container_annotation_discovery/src/Plugin/Discovery/AnnotatedClassDiscovery.php
View source
<?php
namespace Drupal\service_container_annotation_discovery\Plugin\Discovery;
use Doctrine\Common\Annotations\AnnotationRegistry;
use Doctrine\Common\Annotations\SimpleAnnotationReader;
use Doctrine\Common\Reflection\StaticReflectionParser;
use Drupal\Component\Annotation\Reflection\MockFileFinder;
use Drupal\Component\Plugin\Discovery\DiscoveryInterface;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
class AnnotatedClassDiscovery implements DiscoveryInterface {
protected $pluginManagerDefinition;
protected $pluginNamespaces;
protected $pluginDefinitionAnnotationName;
protected $annotationReader;
function __construct($plugin_manager_definition, $plugin_definition_annotation_name = 'Drupal\\Component\\Annotation\\Plugin') {
$namespaces = array();
foreach (module_list() as $module_name) {
$directory = DRUPAL_ROOT . '/' . drupal_get_path('module', $module_name) . '/src/' . trim($plugin_manager_definition['directory'], DIRECTORY_SEPARATOR);
$namespaces['Drupal\\' . $module_name] = array(
$directory,
);
}
$this->pluginNamespaces = new \ArrayObject($namespaces);
$this->pluginDefinitionAnnotationName = isset($plugin_manager_definition['class']) ? $plugin_manager_definition['class'] : $plugin_definition_annotation_name;
$this->pluginManagerDefinition = $plugin_manager_definition;
}
protected function getAnnotationReader() {
if (!isset($this->annotationReader)) {
$this->annotationReader = new SimpleAnnotationReader();
$namespace = substr($this->pluginDefinitionAnnotationName, 0, strrpos($this->pluginDefinitionAnnotationName, '\\'));
$this->annotationReader
->addNamespace($namespace);
}
return $this->annotationReader;
}
protected function getPluginNamespaces() {
return $this->pluginNamespaces;
}
public function getDefinitions() {
$definitions = array();
$reader = $this
->getAnnotationReader();
AnnotationRegistry::reset();
AnnotationRegistry::registerLoader('class_exists');
foreach ($this
->getPluginNamespaces() as $namespace => $dirs) {
foreach ($dirs as $dir) {
if (file_exists($dir)) {
$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS));
foreach ($iterator as $fileinfo) {
if ($fileinfo
->getExtension() == 'php') {
$sub_path = $iterator
->getSubIterator()
->getSubPath();
$sub_path = $sub_path ? str_replace('/', '\\', $sub_path) . '\\' : '';
$class = $namespace . '\\' . str_replace('/', '\\', $this->pluginManagerDefinition['directory']) . '\\' . $sub_path . $fileinfo
->getBasename('.php');
$finder = MockFileFinder::create($fileinfo
->getPathName());
$parser = new StaticReflectionParser($class, $finder, TRUE);
if ($annotation = $reader
->getClassAnnotation($parser
->getReflectionClass(), $this->pluginDefinitionAnnotationName)) {
$this
->prepareAnnotationDefinition($annotation, $class);
$definitions[$annotation
->getId()] = $annotation
->get();
}
}
}
}
}
}
AnnotationRegistry::reset();
return $definitions;
}
protected function prepareAnnotationDefinition($annotation, $class) {
$annotation
->setClass($class);
}
public function getDefinition($plugin_id, $exception_on_invalid = TRUE) {
$definitions = $this
->getDefinitions();
$definition = isset($definitions[$plugin_id]) ? $definitions[$plugin_id] : FALSE;
if (!$definition && $exception_on_invalid) {
throw new PluginNotFoundException($plugin_id, sprintf('The "%s" plugin does not exist.', $plugin_id));
}
return $definition;
}
public function hasDefinition($plugin_id) {
return (bool) $this
->getDefinition($plugin_id, FALSE);
}
}