summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2016-06-22 15:32:20 (GMT)
committerAlex Pott2016-06-22 15:32:20 (GMT)
commit1da4d15cd8e39cecbfa1fed90adb935fbb0a77cb (patch)
treee2346b6a8fe8be47b56100f9a90add659dad977a
parent3ed6e6b13f2d3098a627d41d44c7fe4c04ba625f (diff)
Issue #2447803 by amateescu, Fabianx, Berdir: Use FileCache for caching discovered annotations
-rw-r--r--core/lib/Drupal/Component/Annotation/Plugin/Discovery/AnnotatedClassDiscovery.php31
-rw-r--r--core/lib/Drupal/Component/Annotation/composer.json1
-rw-r--r--core/modules/views/tests/src/Unit/ViewsHandlerManagerTest.php1
-rw-r--r--core/tests/Drupal/Tests/Core/Field/BaseFieldDefinitionTestBase.php1
-rw-r--r--core/tests/Drupal/Tests/Core/Render/ElementInfoManagerTest.php2
-rw-r--r--core/tests/Drupal/Tests/UnitTestCase.php2
6 files changed, 37 insertions, 1 deletions
diff --git a/core/lib/Drupal/Component/Annotation/Plugin/Discovery/AnnotatedClassDiscovery.php b/core/lib/Drupal/Component/Annotation/Plugin/Discovery/AnnotatedClassDiscovery.php
index 50d8856..e564fd0 100644
--- a/core/lib/Drupal/Component/Annotation/Plugin/Discovery/AnnotatedClassDiscovery.php
+++ b/core/lib/Drupal/Component/Annotation/Plugin/Discovery/AnnotatedClassDiscovery.php
@@ -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;
@@ -49,6 +50,13 @@ class AnnotatedClassDiscovery implements DiscoveryInterface {
protected $annotationNamespaces = [];
/**
+ * The file cache object.
+ *
+ * @var \Drupal\Component\FileCache\FileCacheInterface
+ */
+ protected $fileCache;
+
+ /**
* Constructs a new instance.
*
* @param string[] $plugin_namespaces
@@ -64,6 +72,10 @@ class AnnotatedClassDiscovery implements DiscoveryInterface {
$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 @@ class AnnotatedClassDiscovery implements DiscoveryInterface {
);
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 @@ class AnnotatedClassDiscovery implements DiscoveryInterface {
/** @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]);
}
}
}
diff --git a/core/lib/Drupal/Component/Annotation/composer.json b/core/lib/Drupal/Component/Annotation/composer.json
index ac59021..76254ea 100644
--- a/core/lib/Drupal/Component/Annotation/composer.json
+++ b/core/lib/Drupal/Component/Annotation/composer.json
@@ -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"
},
diff --git a/core/modules/views/tests/src/Unit/ViewsHandlerManagerTest.php b/core/modules/views/tests/src/Unit/ViewsHandlerManagerTest.php
index d85fb43..efbbe49 100644
--- a/core/modules/views/tests/src/Unit/ViewsHandlerManagerTest.php
+++ b/core/modules/views/tests/src/Unit/ViewsHandlerManagerTest.php
@@ -42,6 +42,7 @@ class ViewsHandlerManagerTest extends UnitTestCase {
* {@inheritdoc}
*/
protected function setUp() {
+ parent::setUp();
$this->viewsData = $this->getMockBuilder('Drupal\views\ViewsData')
->disableOriginalConstructor()
->getMock();
diff --git a/core/tests/Drupal/Tests/Core/Field/BaseFieldDefinitionTestBase.php b/core/tests/Drupal/Tests/Core/Field/BaseFieldDefinitionTestBase.php
index e6a5de8..acb7346 100644
--- a/core/tests/Drupal/Tests/Core/Field/BaseFieldDefinitionTestBase.php
+++ b/core/tests/Drupal/Tests/Core/Field/BaseFieldDefinitionTestBase.php
@@ -24,6 +24,7 @@ abstract class BaseFieldDefinitionTestBase extends UnitTestCase {
* {@inheritdoc}
*/
protected function setUp() {
+ parent::setUp();
// getModuleAndPath() returns an array of the module name and directory.
list($module_name, $module_dir) = $this->getModuleAndPath();
diff --git a/core/tests/Drupal/Tests/Core/Render/ElementInfoManagerTest.php b/core/tests/Drupal/Tests/Core/Render/ElementInfoManagerTest.php
index 647d82d..0d5aec6 100644
--- a/core/tests/Drupal/Tests/Core/Render/ElementInfoManagerTest.php
+++ b/core/tests/Drupal/Tests/Core/Render/ElementInfoManagerTest.php
@@ -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');
diff --git a/core/tests/Drupal/Tests/UnitTestCase.php b/core/tests/Drupal/Tests/UnitTestCase.php
index 77a3d22..d8fbebc 100644
--- a/core/tests/Drupal/Tests/UnitTestCase.php
+++ b/core/tests/Drupal/Tests/UnitTestCase.php
@@ -43,6 +43,8 @@ abstract class UnitTestCase extends \PHPUnit_Framework_TestCase {
// 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__))));
}