Commit bd6c449c authored by catch's avatar catch
Browse files

Issue #3116858 by alexpott, daffie, catch, andypost, Mile23, neclimdul: Don't...

Issue #3116858 by alexpott, daffie, catch, andypost, Mile23, neclimdul: Don't cache whole extension objects in FileCache
parent 952c0866
......@@ -436,50 +436,56 @@ protected function scanDirectory($dir, $include_tests) {
continue;
}
if ($this->fileCache && $cached_extension = $this->fileCache->get($fileinfo->getPathName())) {
$files[$cached_extension->getType()][$key] = $cached_extension;
continue;
}
// Determine extension type from info file.
$type = FALSE;
$file = $fileinfo->openFile('r');
while (!$type && !$file->eof()) {
preg_match('@^type:\s*(\'|")?(\w+)\1?\s*$@', $file->fgets(), $matches);
if (isset($matches[2])) {
$type = $matches[2];
$extension_arguments = $this->fileCache ? $this->fileCache->get($fileinfo->getPathName()) : FALSE;
// Ensure $extension_arguments is an array. Previously, the Extension
// object was cached and now needs to be replaced with the array.
if (empty($extension_arguments) || !is_array($extension_arguments)) {
// Determine extension type from info file.
$type = FALSE;
$file = $fileinfo->openFile('r');
while (!$type && !$file->eof()) {
preg_match('@^type:\s*(\'|")?(\w+)\1?\s*$@', $file->fgets(), $matches);
if (isset($matches[2])) {
$type = $matches[2];
}
}
}
if (empty($type)) {
continue;
}
$name = $fileinfo->getBasename('.info.yml');
$pathname = $dir_prefix . $fileinfo->getSubPathname();
if (empty($type)) {
continue;
}
$name = $fileinfo->getBasename('.info.yml');
$pathname = $dir_prefix . $fileinfo->getSubPathname();
// Determine whether the extension has a main extension file.
// For theme engines, the file extension is .engine.
if ($type == 'theme_engine') {
$filename = $name . '.engine';
}
// For profiles/modules/themes, it is the extension type.
else {
$filename = $name . '.' . $type;
}
if (!file_exists($this->root . '/' . dirname($pathname) . '/' . $filename)) {
$filename = NULL;
// Determine whether the extension has a main extension file.
// For theme engines, the file extension is .engine.
if ($type == 'theme_engine') {
$filename = $name . '.engine';
}
// For profiles/modules/themes, it is the extension type.
else {
$filename = $name . '.' . $type;
}
if (!file_exists($this->root . '/' . dirname($pathname) . '/' . $filename)) {
$filename = NULL;
}
$extension_arguments = [
'type' => $type,
'pathname' => $pathname,
'filename' => $filename,
'subpath' => $fileinfo->getSubPath(),
];
if ($this->fileCache) {
$this->fileCache->set($fileinfo->getPathName(), $extension_arguments);
}
}
$extension = new Extension($this->root, $type, $pathname, $filename);
$extension = new Extension($this->root, $extension_arguments['type'], $extension_arguments['pathname'], $extension_arguments['filename']);
// Track the originating directory for sorting purposes.
$extension->subpath = $fileinfo->getSubPath();
$extension->subpath = $extension_arguments['subpath'];
$extension->origin = $dir;
$files[$type][$key] = $extension;
if ($this->fileCache) {
$this->fileCache->set($fileinfo->getPathName(), $extension);
}
$files[$extension_arguments['type']][$key] = $extension;
}
return $files;
}
......
......@@ -2,6 +2,7 @@
namespace Drupal\Tests\Core\Extension;
use Drupal\Component\FileCache\FileCacheFactory;
use Drupal\Core\Extension\Extension;
use Drupal\Core\Extension\ExtensionDiscovery;
use Drupal\Tests\UnitTestCase;
......@@ -63,6 +64,40 @@ public function testExtensionDiscoveryVfs() {
$this->assertEquals($extension_expected, $extensions_by_type['theme_engine']['twig'], 'twig');
}
/**
* Tests changing extension discovery file cache objects to arrays.
*
* @covers ::scan
* @runInSeparateProcess
*/
public function testExtensionDiscoveryCache() {
// Set up an extension object in the cache to mimic site prior to changing
// \Drupal\Core\Extension\ExtensionDiscovery::scanDirectory() to cache an
// array instead of an object. Note we cannot use the VFS file system
// because FileCache does not support stream wrappers.
$extension = new Extension($this->root, 'module', 'core/modules/user/user.info.yml', 'user.module');
$extension->subpath = 'modules/user';
$extension->origin = 'core';
// Undo \Drupal\Tests\UnitTestCase::setUp() so FileCache works.
FileCacheFactory::setConfiguration([]);
$file_cache = FileCacheFactory::get('extension_discovery');
$file_cache->set($this->root . '/core/modules/user/user.info.yml', $extension);
// Create an ExtensionDiscovery object to test.
$extension_discovery = new ExtensionDiscovery($this->root, TRUE, [], 'sites/default');
$modules = $extension_discovery->scan('module', FALSE);
$this->assertArrayHasKey('user', $modules);
$this->assertEquals((array) $extension, (array) $modules['user']);
$this->assertNotSame($extension, $modules['user']);
// FileCache item should now be an array.
$this->assertSame([
'type' => 'module',
'pathname' => 'core/modules/user/user.info.yml',
'filename' => 'user.module',
'subpath' => 'modules/user',
], $file_cache->get($this->root . '/core/modules/user/user.info.yml'));
}
/**
* Adds example files to the filesystem structure.
*
......
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