Same filename in this branch
- 10 core/lib/Drupal/Core/Plugin/Discovery/AnnotatedClassDiscovery.php
- 10 core/lib/Drupal/Component/Annotation/Plugin/Discovery/AnnotatedClassDiscovery.php
Same filename and directory in other branches
- 8.9.x core/lib/Drupal/Component/Annotation/Plugin/Discovery/AnnotatedClassDiscovery.php
- 9 core/lib/Drupal/Component/Annotation/Plugin/Discovery/AnnotatedClassDiscovery.php
Namespace
Drupal\Component\Annotation\Plugin\Discovery
File
core/lib/Drupal/Component/Annotation/Plugin/Discovery/AnnotatedClassDiscovery.php
View source
<?php
namespace Drupal\Component\Annotation\Plugin\Discovery;
use Drupal\Component\Annotation\AnnotationInterface;
use Drupal\Component\FileCache\FileCacheFactory;
use Drupal\Component\Plugin\Discovery\DiscoveryInterface;
use Drupal\Component\Annotation\Doctrine\SimpleAnnotationReader;
use Drupal\Component\Annotation\Doctrine\StaticReflectionParser;
use Drupal\Component\Annotation\Reflection\MockFileFinder;
use Doctrine\Common\Annotations\AnnotationRegistry;
use Drupal\Component\Plugin\Discovery\DiscoveryTrait;
use Drupal\Component\Utility\Crypt;
class AnnotatedClassDiscovery implements DiscoveryInterface {
use DiscoveryTrait;
protected $pluginNamespaces;
protected $pluginDefinitionAnnotationName;
protected $annotationReader;
protected $annotationNamespaces = [];
protected $fileCache;
public function __construct($plugin_namespaces = [], $plugin_definition_annotation_name = 'Drupal\\Component\\Annotation\\Plugin', array $annotation_namespaces = []) {
$this->pluginNamespaces = $plugin_namespaces;
$this->pluginDefinitionAnnotationName = $plugin_definition_annotation_name;
$this->annotationNamespaces = $annotation_namespaces;
$file_cache_suffix = str_replace('\\', '_', $plugin_definition_annotation_name);
$file_cache_suffix .= ':' . Crypt::hashBase64(serialize($annotation_namespaces));
$this->fileCache = FileCacheFactory::get('annotation_discovery:' . $file_cache_suffix);
}
protected function getAnnotationReader() {
if (!isset($this->annotationReader)) {
$this->annotationReader = new SimpleAnnotationReader();
$namespace = substr($this->pluginDefinitionAnnotationName, 0, strrpos($this->pluginDefinitionAnnotationName, '\\'));
$this->annotationReader
->addNamespace($namespace);
foreach ($this->annotationNamespaces as $namespace) {
$this->annotationReader
->addNamespace($namespace);
}
}
return $this->annotationReader;
}
public function getDefinitions() {
$definitions = [];
$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') {
if ($cached = $this->fileCache
->get($fileinfo
->getPathName())) {
if (isset($cached['id'])) {
$definitions[$cached['id']] = unserialize($cached['content']);
}
continue;
}
$sub_path = $iterator
->getSubIterator()
->getSubPath();
$sub_path = $sub_path ? str_replace(DIRECTORY_SEPARATOR, '\\', $sub_path) . '\\' : '';
$class = $namespace . '\\' . $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);
$id = $annotation
->getId();
$content = $annotation
->get();
$definitions[$id] = $content;
$this->fileCache
->set($fileinfo
->getPathName(), [
'id' => $id,
'content' => serialize($content),
]);
}
else {
$this->fileCache
->set($fileinfo
->getPathName(), [
NULL,
]);
}
}
}
}
}
}
AnnotationRegistry::reset();
return $definitions;
}
protected function prepareAnnotationDefinition(AnnotationInterface $annotation, $class) {
$annotation
->setClass($class);
}
protected function getPluginNamespaces() {
return $this->pluginNamespaces;
}
}
Classes