mirror of
https://github.com/e107inc/e107.git
synced 2025-08-05 06:07:32 +02: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:
@@ -14,6 +14,9 @@ class e_file_inspector_sqlphar extends e_file_inspector
|
|||||||
private $coreImage;
|
private $coreImage;
|
||||||
private $currentVersion;
|
private $currentVersion;
|
||||||
|
|
||||||
|
private $defaultDirsCache;
|
||||||
|
private $customDirsCache;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* e_file_inspector_sqlphar constructor.
|
* e_file_inspector_sqlphar constructor.
|
||||||
* @param $pharFilePath string Absolute path to the file inspector database
|
* @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)
|
private function pathToDefaultPath($path)
|
||||||
{
|
{
|
||||||
$defaultDirs = e107::getInstance()->defaultDirs();
|
if (!$this->customDirsCache)
|
||||||
foreach ($defaultDirs as $dirType => $defaultDir)
|
|
||||||
{
|
{
|
||||||
$customDir = e107::getFolder(preg_replace("/_DIRECTORY$/i", "", $dirType));
|
$this->defaultDirsCache = e107::getInstance()->defaultDirs();
|
||||||
$path = preg_replace("/^" . preg_quote($customDir, "/") . "/", $defaultDir, $path);
|
$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;
|
return $path;
|
||||||
}
|
}
|
||||||
|
@@ -62,4 +62,28 @@ class e_file_inspectorTest extends \Codeception\Test\Unit
|
|||||||
$result = $this->e_integrity->validate("file/does/not/exist.php");
|
$result = $this->e_integrity->validate("file/does/not/exist.php");
|
||||||
$this->assertEquals(0, $result & e_file_inspector::VALIDATED_PRESENCE);
|
$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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user