mirror of
https://github.com/e107inc/e107.git
synced 2025-01-17 12:48:24 +01:00
Performance of e_file_inspector_sqlphar::pathToDefaultPath()
Optimized performance of e_file_inspector_sqlphar::pathToDefaultPath() by eliminating expensive method calls
This commit is contained in:
parent
e10c3230f4
commit
90bdc88d55
@ -14,6 +14,9 @@ class e_file_inspector_sqlphar extends e_file_inspector
|
||||
private $coreImage;
|
||||
private $currentVersion;
|
||||
|
||||
private $defaultDirsCache;
|
||||
private $customDirsCache;
|
||||
|
||||
/**
|
||||
* e_file_inspector_sqlphar constructor.
|
||||
* @param $pharFilePath string Absolute path to the file inspector database
|
||||
@ -86,11 +89,21 @@ class e_file_inspector_sqlphar extends e_file_inspector
|
||||
|
||||
private function pathToDefaultPath($path)
|
||||
{
|
||||
$defaultDirs = e107::getInstance()->defaultDirs();
|
||||
foreach ($defaultDirs as $dirType => $defaultDir)
|
||||
if (!$this->customDirsCache)
|
||||
{
|
||||
$customDir = e107::getFolder(preg_replace("/_DIRECTORY$/i", "", $dirType));
|
||||
$path = preg_replace("/^" . preg_quote($customDir, "/") . "/", $defaultDir, $path);
|
||||
$this->defaultDirsCache = e107::getInstance()->defaultDirs();
|
||||
$customDirs = e107::getInstance()->e107_dirs ? e107::getInstance()->e107_dirs : [];
|
||||
$this->customDirsCache = array_diff_assoc($customDirs, $this->defaultDirsCache);
|
||||
}
|
||||
foreach ($this->customDirsCache as $dirType => $customDir)
|
||||
{
|
||||
if (!isset($this->defaultDirsCache[$dirType])) continue;
|
||||
|
||||
$defaultDir = $this->defaultDirsCache[$dirType];
|
||||
if ($customDir === $defaultDir) continue;
|
||||
|
||||
if (substr($path, 0, strlen($customDir)) === $customDir)
|
||||
$path = $defaultDir . substr($path, strlen($customDir));
|
||||
}
|
||||
return $path;
|
||||
}
|
||||
|
@ -62,4 +62,28 @@ class e_file_inspectorTest extends \Codeception\Test\Unit
|
||||
$result = $this->e_integrity->validate("file/does/not/exist.php");
|
||||
$this->assertEquals(0, $result & e_file_inspector::VALIDATED_PRESENCE);
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO: Create a stable interface for pathToDefaultPath()
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
public function testPathToDefaultPath()
|
||||
{
|
||||
$object = new e_file_inspector_sqlphar();
|
||||
$class = new ReflectionClass(get_class($object));
|
||||
$method = $class->getMethod('pathToDefaultPath');
|
||||
$method->setAccessible(true);
|
||||
$method->invoke($object, 'populate_cache');
|
||||
$member = $class->getProperty('customDirsCache');
|
||||
$member->setAccessible(true);
|
||||
$customDirs = $member->getValue($object);
|
||||
$customDirs['ADMIN_DIRECTORY'] = 'e963_admin/';
|
||||
$member->setValue($object, $customDirs);
|
||||
|
||||
$input = "e963_admin/index.php";
|
||||
$expected = "e107_admin/index.php";
|
||||
$actual = $method->invoke($object, $input);
|
||||
|
||||
$this->assertEquals($expected, $actual);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user