Merge remote-tracking branch 'origin/support/3.2' into develop

This commit is contained in:
Eric Espie
2024-10-14 09:27:25 +02:00
9 changed files with 262 additions and 86 deletions

View File

@@ -23,16 +23,16 @@ class DataModelDependantCache
{
if (!isset(self::$oInstance))
{
self::$oInstance = new DataModelDependantCache(utils::GetCachePath());
self::$oInstance = new DataModelDependantCache();
}
return self::$oInstance;
}
private ?string $sStorageRootDir; // Nullable for test purposes
private function __construct($sStorageRootDir)
private function __construct()
{
$this->sStorageRootDir = $sStorageRootDir;
$this->sStorageRootDir = null;
}
/**
@@ -146,10 +146,25 @@ class DataModelDependantCache
return $this->GetStorageRootDir()."/$sPool/";
}
/** Overridable for testing purposes */
/**
* for test purpose
*
* @return string
*/
protected function GetStorageRootDir(): string
{
// Could be forced by tests
return $this->sStorageRootDir ?? utils::GetCachePath();
}
/**
* for test purpose
*
* @param string|null $sStorageRootDir if null the current cache path is used
*/
public function SetStorageRootDir(?string $sStorageRootDir): void
{
$this->sStorageRootDir = $sStorageRootDir;
}
}

View File

@@ -6,24 +6,38 @@ use Combodo\iTop\Service\Cache\DataModelDependantCache;
use Exception;
use IssueLog;
use LogChannels;
use MetaModel;
use ReflectionClass;
use utils;
/**
* Enumerate classes implementing given interfaces
*
* @api
*
* @since 3.2.0
*/
class InterfaceDiscovery
{
private static InterfaceDiscovery $oInstance;
private DataModelDependantCache $oCacheService;
private ?array $aForcedClassMap = null; // For testing purposes
const CACHE_NONE = 'CACHE_NONE';
const CACHE_DYNAMIC = 'CACHE_DYNAMIC'; // rebuild cache when files changes
const CACHE_STATIC = 'CACHE_STATIC'; // Built once at setup
private function __construct()
{
$this->oCacheService = DataModelDependantCache::GetInstance();
}
public static function GetInstance(): InterfaceDiscovery
{
if (!isset(self::$oInstance))
{
if (!isset(self::$oInstance)) {
self::$oInstance = new InterfaceDiscovery();
}
return self::$oInstance;
}
@@ -31,27 +45,38 @@ class InterfaceDiscovery
* Find the ITOP classes implementing a given interface. The returned classes have the following properties:
* - They can be instantiated
* - They are not aliases
* - Their path relative to iTop does not contain /lib/, /node_modules/, /test/ or /tests/
*
* @param string $sInterface Fully qualified interface name
* @param array|null $aAdditionalExcludedPaths Optional list of paths to exclude from the search (partial names allowed, case sensitive, use / as separator)
*
* @return array of fully qualified class names
* @throws \ReflectionException when $sInterface is not an interface
*
* @api
*
* @since 3.2.0
*/
public function FindItopClasses(string $sInterface, ?array $aAdditionalExcludedPaths = null): array
public function FindItopClasses(string $sInterface): array
{
if (is_null($aAdditionalExcludedPaths)) {
return $this->FindClasses($sInterface, ['/lib/', '/node_modules/', '/test/', '/tests/']);
}
$aExcludedPaths = ['/lib/', '/node_modules/', '/test/', '/tests/'];
$aExcludedPaths = array_merge(['/lib/', '/node_modules/', '/test/', '/tests/'], $aAdditionalExcludedPaths);
return $this->FindClasses($sInterface, $aExcludedPaths);
}
/**
* @param string $sInterface
* @param array $aExcludedPaths
*
* @return array
* @throws \ReflectionException
*/
private function FindClasses(string $sInterface, array $aExcludedPaths = []): array
{
$sCacheUniqueKey = $this->MakeCacheKey($sInterface, $aExcludedPaths);
if ($this->IsCacheValid($sCacheUniqueKey)) {
return $this->ReadClassesFromCache($sCacheUniqueKey);
if ($this->GetCacheMode() !== self::CACHE_NONE) {
$sCacheUniqueKey = $this->MakeCacheKey($sInterface, $aExcludedPaths);
if ($this->IsCacheValid($sCacheUniqueKey)) {
return $this->ReadClassesFromCache($sCacheUniqueKey);
}
}
$sExcludedPathsRegExp = $this->GetExcludedPathsRegExp($aExcludedPaths);
@@ -66,40 +91,65 @@ class InterfaceDiscovery
}
}
$this->SaveClassesToCache($sCacheUniqueKey, $aMatchingClasses, ['Interface' => $sInterface, 'Excluded paths' => implode(',', $aExcludedPaths)]);
if ($this->GetCacheMode() !== self::CACHE_NONE) {
$this->SaveClassesToCache($sCacheUniqueKey, $aMatchingClasses, ['Interface' => $sInterface, 'Excluded paths' => implode(',', $aExcludedPaths)]);
}
return $aMatchingClasses;
}
private function GetAutoloadClassMaps(): array
{
$oCacheService = DataModelDependantCache::GetInstance();
$aAutoloadClassMaps = $oCacheService->Fetch('InterfaceDiscovery', 'autoload_classmaps');
if ($aAutoloadClassMaps !== null) {
return $aAutoloadClassMaps;
if ($this->GetCacheMode() === self::CACHE_DYNAMIC) {
$aAutoloadClassMaps = $this->oCacheService->Fetch('InterfaceDiscovery', 'autoload_classmaps');
if ($aAutoloadClassMaps !== null) {
return $aAutoloadClassMaps;
}
}
// guess all the autoload class maps from the extensions
$aAutoloadClassMaps = glob(APPROOT.'env-'.utils::GetCurrentEnvironment().'/*/vendor/composer/autoload_classmap.php');
$aAutoloadClassMaps[] = APPROOT.'lib/composer/autoload_classmap.php';
$oCacheService->Store('InterfaceDiscovery', 'autoload_classmaps', $aAutoloadClassMaps);
if ($this->GetCacheMode() === self::CACHE_DYNAMIC) {
$this->oCacheService->Store('InterfaceDiscovery', 'autoload_classmaps', $aAutoloadClassMaps);
}
return $aAutoloadClassMaps;
}
/**
* @param string $sPHPClass
* @param string $sInterface
*
* @return bool
*
* @throws \ReflectionException
*/
private function IsInterfaceImplementation(string $sPHPClass, string $sInterface): bool
{
try {
$oRefClass = new ReflectionClass($sPHPClass);
} catch (Exception $e) {
}
catch (Exception $e) {
return false;
}
if (!$oRefClass->implementsInterface($sInterface)) return false;
if ($oRefClass->isInterface()) return false;
if ($oRefClass->isAbstract()) return false;
if ($oRefClass->isTrait()) return false;
if ($oRefClass->getName() !== $sPHPClass) return false; // Skip aliases
if (!$oRefClass->implementsInterface($sInterface)) {
return false;
}
if ($oRefClass->isInterface()) {
return false;
}
if ($oRefClass->isAbstract()) {
return false;
}
if ($oRefClass->isTrait()) {
return false;
}
if ($oRefClass->getName() !== $sPHPClass) {
return false;
} // Skip aliases
return true;
}
@@ -110,8 +160,7 @@ class InterfaceDiscovery
$aAutoloaderErrors = [];
if ($this->aForcedClassMap !== null) {
$aClassMap = $this->aForcedClassMap;
}
else {
} else {
foreach ($this->GetAutoloadClassMaps() as $sAutoloadFile) {
if (false === utils::RealPath($sAutoloadFile, APPROOT)) {
// can happen when we still have the autoloader symlink in env-*, but it points to a file that no longer exists
@@ -140,12 +189,18 @@ class InterfaceDiscovery
private function IsValidPHPFile(string $sOptionalPHPFile, ?string $sExcludedPathsRegExp): bool
{
if ($sOptionalPHPFile === '') return true;
if ($sOptionalPHPFile === '') {
return true;
}
$sOptionalPHPFile = utils::LocalPath($sOptionalPHPFile);
if ($sOptionalPHPFile === false) return false;
if ($sOptionalPHPFile === false) {
return false;
}
if (is_null($sExcludedPathsRegExp)) return true;
if (is_null($sExcludedPathsRegExp)) {
return true;
}
if (preg_match($sExcludedPathsRegExp, '/'.$sOptionalPHPFile) === 1) {
return false;
@@ -156,42 +211,51 @@ class InterfaceDiscovery
private function IsCacheValid(string $sKey): bool
{
if ($this->aForcedClassMap !== null) return false;
if ($this->aForcedClassMap !== null) {
return false;
}
$oCacheService = DataModelDependantCache::GetInstance();
if (!$this->oCacheService->HasEntry('InterfaceDiscovery', $sKey)) {
return false;
}
if (!$oCacheService->HasEntry('InterfaceDiscovery', $sKey)) return false;
if (!utils::IsDevelopmentEnvironment()) return true;
if ($this->GetCacheMode() === self::CACHE_STATIC) {
return true;
}
// On development environment, we check the cache validity by comparing the cache file with the autoload_classmap files
$iCacheTime = $oCacheService->GetEntryModificationTime('InterfaceDiscovery', $sKey);
foreach($this->GetAutoloadClassMaps() as $sSourceFile) {
$iCacheTime = $this->oCacheService->GetEntryModificationTime('InterfaceDiscovery', $sKey);
foreach ($this->GetAutoloadClassMaps() as $sSourceFile) {
$iSourceTime = filemtime($sSourceFile);
if ($iSourceTime > $iCacheTime) return false;
if ($iSourceTime > $iCacheTime) {
return false;
}
}
return true;
}
public function ReadClassesFromCache(string $sKey): array
{
$oCacheService = DataModelDependantCache::GetInstance();
return $oCacheService->Fetch('InterfaceDiscovery', $sKey);
return $this->oCacheService->Fetch('InterfaceDiscovery', $sKey);
}
protected function SaveClassesToCache(string $sKey, array $aMatchingClasses, array $aMoreInfo): void
{
if ($this->aForcedClassMap !== null) return;
if ($this->aForcedClassMap !== null) {
return;
}
$oCacheService = DataModelDependantCache::GetInstance();
$oCacheService->Store('InterfaceDiscovery', $sKey, $aMatchingClasses, $aMoreInfo);
$this->oCacheService->Store('InterfaceDiscovery', $sKey, $aMatchingClasses, $aMoreInfo);
}
private function GetExcludedPathsRegExp(array $aExcludedPaths) : ?string
private function GetExcludedPathsRegExp(array $aExcludedPaths): ?string
{
if (count($aExcludedPaths) == 0) return null;
if (count($aExcludedPaths) == 0) {
return null;
}
$aExcludedPathRegExps = array_map(function($sPath) {
$aExcludedPathRegExps = array_map(function ($sPath) {
return preg_quote($sPath, '#');
}, $aExcludedPaths);
@@ -200,7 +264,36 @@ class InterfaceDiscovery
protected function MakeCacheKey(string $sInterface, array $aExcludedPaths): string
{
if (count($aExcludedPaths) == 0) return $sInterface;
return md5($sInterface.':'.implode(',', $aExcludedPaths));
if (count($aExcludedPaths) == 0) {
$sKey = $sInterface;
} else {
$sKey = $sInterface.':'.implode(',', $aExcludedPaths);
}
$iPos = strrpos($sInterface, '\\');
$sInterfaceDisplayName = $iPos === false ? $sInterface : substr($sInterface, $iPos + 1);
return md5($sKey)."-$sInterfaceDisplayName";
}
/**
* @param \Combodo\iTop\Service\Cache\DataModelDependantCache $this->oCacheService
*/
public function SetCacheService(DataModelDependantCache $oCacheService): void
{
$this->oCacheService = $oCacheService;
}
protected function GetCacheMode(): string
{
if (!utils::IsDevelopmentEnvironment()) {
return self::CACHE_STATIC;
}
if (!is_null(MetaModel::GetConfig()) && MetaModel::GetConfig()->Get('developer_mode.interface_cache.enabled')) {
return self::CACHE_DYNAMIC;
}
return self::CACHE_NONE;
}
}