Commit 1da4d15c authored by alexpott's avatar alexpott

Issue #2447803 by amateescu, Fabianx, Berdir: Use FileCache for caching discovered annotations

parent 3ed6e6b1
......@@ -3,6 +3,7 @@
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\Reflection\MockFileFinder;
use Doctrine\Common\Annotations\SimpleAnnotationReader;
......@@ -48,6 +49,13 @@ class AnnotatedClassDiscovery implements DiscoveryInterface {
*/
protected $annotationNamespaces = [];
/**
* The file cache object.
*
* @var \Drupal\Component\FileCache\FileCacheInterface
*/
protected $fileCache;
/**
* Constructs a new instance.
*
......@@ -64,6 +72,10 @@ function __construct($plugin_namespaces = array(), $plugin_definition_annotation
$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 .= ':' . hash('crc32b', serialize($annotation_namespaces));
$this->fileCache = FileCacheFactory::get('annotation_discovery:' . $file_cache_suffix);
}
/**
......@@ -110,6 +122,14 @@ public function getDefinitions() {
);
foreach ($iterator as $fileinfo) {
if ($fileinfo->getExtension() == 'php') {
if ($cached = $this->fileCache->get($fileinfo->getPathName())) {
if (isset($cached['id'])) {
// Explicitly unserialize this to create a new object instance.
$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');
......@@ -123,7 +143,16 @@ public function getDefinitions() {
/** @var $annotation \Drupal\Component\Annotation\AnnotationInterface */
if ($annotation = $reader->getClassAnnotation($parser->getReflectionClass(), $this->pluginDefinitionAnnotationName)) {
$this->prepareAnnotationDefinition($annotation, $class);
$definitions[$annotation->getId()] = $annotation->get();
$id = $annotation->getId();
$content = $annotation->get();
$definitions[$id] = $content;
// Explicitly serialize this to create a new object instance.
$this->fileCache->set($fileinfo->getPathName(), ['id' => $id, 'content' => serialize($content)]);
}
else {
// Store a NULL object, so the file is not reparsed again.
$this->fileCache->set($fileinfo->getPathName(), [NULL]);
}
}
}
......
......@@ -8,6 +8,7 @@
"php": ">=5.5.9",
"doctrine/common": "2.5.*",
"doctrine/annotations": "1.2.*",
"drupal/core-fileCache": "~8.2",
"drupal/core-plugin": "~8.2",
"drupal/core-utility": "~8.2"
},
......
......@@ -42,6 +42,7 @@ class ViewsHandlerManagerTest extends UnitTestCase {
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->viewsData = $this->getMockBuilder('Drupal\views\ViewsData')
->disableOriginalConstructor()
->getMock();
......
......@@ -24,6 +24,7 @@
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
// getModuleAndPath() returns an array of the module name and directory.
list($module_name, $module_dir) = $this->getModuleAndPath();
......
......@@ -58,6 +58,8 @@ class ElementInfoManagerTest extends UnitTestCase {
* @covers ::__construct
*/
protected function setUp() {
parent::setUp();
$this->cache = $this->getMock('Drupal\Core\Cache\CacheBackendInterface');
$this->cacheTagsInvalidator = $this->getMock('Drupal\Core\Cache\CacheTagsInvalidatorInterface');
$this->moduleHandler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface');
......
......@@ -43,6 +43,8 @@ protected function setUp() {
// Ensure that the NullFileCache implementation is used for the FileCache as
// unit tests should not be relying on caches implicitly.
FileCacheFactory::setConfiguration(['default' => ['class' => '\Drupal\Component\FileCache\NullFileCache']]);
// Ensure that FileCacheFactory has a prefix.
FileCacheFactory::setPrefix('prefix');
$this->root = dirname(dirname(substr(__DIR__, 0, -strlen(__NAMESPACE__))));
}
......
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