N°7629 Deprecate utils::GetClassesForInterface in favor of InterfaceDiscovery::FindItopClasses

Improve caching strategy and robustness
This commit is contained in:
Romain Quetiez
2024-07-06 12:08:51 +02:00
parent 147aad9221
commit baf85e7a80
19 changed files with 845 additions and 112 deletions

View File

@@ -22,6 +22,7 @@ use Combodo\iTop\Application\UI\Base\iUIBlock;
use Combodo\iTop\Application\UI\Base\Layout\UIContentBlock;
use Combodo\iTop\Application\UI\Hook\iKeyboardShortcut;
use Combodo\iTop\Application\WebPage\WebPage;
use Combodo\iTop\Service\InterfaceDiscovery\InterfaceDiscovery;
use Combodo\iTop\Service\Module\ModuleService;
use ScssPhp\ScssPhp\Compiler;
use ScssPhp\ScssPhp\OutputStyle;
@@ -2792,95 +2793,12 @@ TXT
*
* @return array classes are returned in the same order as the module dependency tree, so core classes on top
* @since 3.0.0
* @deprecated 3.2.0 Use {@see InterfaceDiscovery::FindItopClasses()} instead
*/
public static function GetClassesForInterface(string $sInterface, string $sClassNameFilter = '', $aExcludedPath = []): array
{
$aMatchingClasses = [];
if (!utils::IsDevelopmentEnvironment()) {
// Try to read from cache
$aFilePath = explode("\\", $sInterface);
$sInterfaceName = end($aFilePath);
$sCacheFileName = utils::GetCachePath()."ImplementingInterfaces/$sInterfaceName.php";
if (is_file($sCacheFileName)) {
$aMatchingClasses = include $sCacheFileName;
}
}
if (empty($aMatchingClasses)) {
$aAutoloadClassMaps = [APPROOT.'lib/composer/autoload_classmap.php'];
// guess all the autoload class maps from the extensions
$aAutoloadClassMaps = array_merge($aAutoloadClassMaps, glob(APPROOT.'env-'.utils::GetCurrentEnvironment().'/*/vendor/composer/autoload_classmap.php'));
$aClassMap = [];
$aAutoloaderErrors = [];
foreach ($aAutoloadClassMaps as $sAutoloadFile) {
if (false === static::RealPath($sAutoloadFile, APPROOT)) {
// can happen when we still have the autoloader symlink in env-*, but it points to a file that no longer exists
$aAutoloaderErrors[] = $sAutoloadFile;
continue;
}
$aTmpClassMap = include $sAutoloadFile;
/** @noinspection SlowArrayOperationsInLoopInspection we are getting an associative array so the documented workarounds cannot be used */
$aClassMap = array_merge($aClassMap, $aTmpClassMap);
}
if (count($aAutoloaderErrors) > 0) {
IssueLog::Debug(
"\utils::GetClassesForInterface cannot load some of the autoloader files",
LogChannels::CORE,
['autoloader_errors' => $aAutoloaderErrors]
);
}
// Add already loaded classes
$aCurrentClasses = array_fill_keys(get_declared_classes(), '');
$aClassMap = array_merge($aCurrentClasses, $aClassMap);
foreach ($aClassMap as $sPHPClass => $sPHPFile) {
$bSkipped = false;
// Check if our class matches name filter, or is in an excluded path
if ($sClassNameFilter !== '' && strpos($sPHPClass, $sClassNameFilter) === false) {
$bSkipped = true;
}
// For some PHP classes we don't have their file path as they are already in memory, so we never filter on their paths
elseif (utils::IsNotNullOrEmptyString($sPHPFile)) {
$sPHPFile = self::LocalPath($sPHPFile);
if ($sPHPFile !== false) {
$sPHPFile = '/'.$sPHPFile; // for regex
foreach ($aExcludedPath as $sExcludedPath) {
// Note: We use '#' as delimiters as usual '/' is often used in paths.
if ($sExcludedPath !== '' && preg_match('#'.$sExcludedPath.'#', $sPHPFile) === 1) {
$bSkipped = true;
break;
}
}
} else {
$bSkipped = true; // file not found
}
}
if(!$bSkipped){
try {
$oRefClass = new ReflectionClass($sPHPClass);
if ($oRefClass->implementsInterface($sInterface) &&
!$oRefClass->isInterface() && !$oRefClass->isAbstract() && !$oRefClass->isTrait()) {
$aMatchingClasses[] = $sPHPClass;
}
} catch (Exception $e) {
}
}
}
if (!utils::IsDevelopmentEnvironment()) {
// Save to cache
$sCacheContent = "<?php\n\nreturn ".var_export($aMatchingClasses, true).";";
SetupUtils::builddir(dirname($sCacheFileName));
file_put_contents($sCacheFileName, $sCacheContent);
}
}
return $aMatchingClasses;
$oInterfaceDiscoveryService = InterfaceDiscovery::GetInstance();
return $oInterfaceDiscoveryService->FindItopClasses($sInterface);
}
/**
@@ -2894,9 +2812,9 @@ TXT
{
$aResultPref = [];
$aShortcutPrefs = appUserPreferences::GetPref('keyboard_shortcuts', []);
// Note: Mind the 4 blackslashes, see utils::GetClassesForInterface()
$aShortcutClasses = utils::GetClassesForInterface(iKeyboardShortcut::class, '', array('[\\\\/]lib[\\\\/]', '[\\\\/]node_modules[\\\\/]', '[\\\\/]test[\\\\/]', '[\\\\/]tests[\\\\/]'));
/** @var iKeyboardShortcut[] $aShortcutClasses */
$aShortcutClasses = InterfaceDiscovery::GetInstance()->FindItopClasses(iKeyboardShortcut::class);
foreach ($aShortcutClasses as $cShortcutPlugin) {
$sTriggeredElement = $cShortcutPlugin::GetShortcutTriggeredElementSelector();
foreach ($cShortcutPlugin::GetShortcutKeys() as $aShortcutKey) {

View File

@@ -492,6 +492,7 @@ return array(
'Combodo\\iTop\\Renderer\\RenderingOutput' => $baseDir . '/sources/Renderer/RenderingOutput.php',
'Combodo\\iTop\\Service\\Base\\ObjectRepository' => $baseDir . '/sources/Service/Base/ObjectRepository.php',
'Combodo\\iTop\\Service\\Base\\iDataPostProcessor' => $baseDir . '/sources/Service/Base/iDataPostProcessor.php',
'Combodo\\iTop\\Service\\Cache\\DataModelDependantCache' => $baseDir . '/sources/Service/Cache/DataModelDependantCache.php',
'Combodo\\iTop\\Service\\Events\\Description\\EventDataDescription' => $baseDir . '/sources/Service/Events/Description/EventDataDescription.php',
'Combodo\\iTop\\Service\\Events\\Description\\EventDescription' => $baseDir . '/sources/Service/Events/Description/EventDescription.php',
'Combodo\\iTop\\Service\\Events\\EventData' => $baseDir . '/sources/Service/Events/EventData.php',
@@ -501,6 +502,7 @@ return array(
'Combodo\\iTop\\Service\\Events\\EventServiceLog' => $baseDir . '/sources/Service/Events/EventServiceLog.php',
'Combodo\\iTop\\Service\\Events\\iEventServiceSetup' => $baseDir . '/sources/Service/Events/iEventServiceSetup.php',
'Combodo\\iTop\\Service\\Import\\CSVImportPageProcessor' => $baseDir . '/sources/Service/Import/CSVImportPageProcessor.php',
'Combodo\\iTop\\Service\\InterfaceDiscovery\\InterfaceDiscovery' => $baseDir . '/sources/Service/InterfaceDiscovery/InterfaceDiscovery.php',
'Combodo\\iTop\\Service\\Links\\LinkSetDataTransformer' => $baseDir . '/sources/Service/Links/LinkSetDataTransformer.php',
'Combodo\\iTop\\Service\\Links\\LinkSetModel' => $baseDir . '/sources/Service/Links/LinkSetModel.php',
'Combodo\\iTop\\Service\\Links\\LinkSetRepository' => $baseDir . '/sources/Service/Links/LinkSetRepository.php',

View File

@@ -872,6 +872,7 @@ class ComposerStaticInit7f81b4a2a468a061c306af5e447a9a9f
'Combodo\\iTop\\Renderer\\RenderingOutput' => __DIR__ . '/../..' . '/sources/Renderer/RenderingOutput.php',
'Combodo\\iTop\\Service\\Base\\ObjectRepository' => __DIR__ . '/../..' . '/sources/Service/Base/ObjectRepository.php',
'Combodo\\iTop\\Service\\Base\\iDataPostProcessor' => __DIR__ . '/../..' . '/sources/Service/Base/iDataPostProcessor.php',
'Combodo\\iTop\\Service\\Cache\\DataModelDependantCache' => __DIR__ . '/../..' . '/sources/Service/Cache/DataModelDependantCache.php',
'Combodo\\iTop\\Service\\Events\\Description\\EventDataDescription' => __DIR__ . '/../..' . '/sources/Service/Events/Description/EventDataDescription.php',
'Combodo\\iTop\\Service\\Events\\Description\\EventDescription' => __DIR__ . '/../..' . '/sources/Service/Events/Description/EventDescription.php',
'Combodo\\iTop\\Service\\Events\\EventData' => __DIR__ . '/../..' . '/sources/Service/Events/EventData.php',
@@ -881,6 +882,7 @@ class ComposerStaticInit7f81b4a2a468a061c306af5e447a9a9f
'Combodo\\iTop\\Service\\Events\\EventServiceLog' => __DIR__ . '/../..' . '/sources/Service/Events/EventServiceLog.php',
'Combodo\\iTop\\Service\\Events\\iEventServiceSetup' => __DIR__ . '/../..' . '/sources/Service/Events/iEventServiceSetup.php',
'Combodo\\iTop\\Service\\Import\\CSVImportPageProcessor' => __DIR__ . '/../..' . '/sources/Service/Import/CSVImportPageProcessor.php',
'Combodo\\iTop\\Service\\InterfaceDiscovery\\InterfaceDiscovery' => __DIR__ . '/../..' . '/sources/Service/InterfaceDiscovery/InterfaceDiscovery.php',
'Combodo\\iTop\\Service\\Links\\LinkSetDataTransformer' => __DIR__ . '/../..' . '/sources/Service/Links/LinkSetDataTransformer.php',
'Combodo\\iTop\\Service\\Links\\LinkSetModel' => __DIR__ . '/../..' . '/sources/Service/Links/LinkSetModel.php',
'Combodo\\iTop\\Service\\Links\\LinkSetRepository' => __DIR__ . '/../..' . '/sources/Service/Links/LinkSetRepository.php',

View File

@@ -25,6 +25,7 @@ use Combodo\iTop\Application\WebPage\ErrorPage;
use Combodo\iTop\Application\WebPage\iTopWebPage;
use Combodo\iTop\Application\WebPage\WebPage;
use Combodo\iTop\Controller\Notifications\NotificationsCenterController;
use Combodo\iTop\Service\InterfaceDiscovery\InterfaceDiscovery;
use Combodo\iTop\Service\Router\Router;
require_once('../approot.inc.php');
@@ -250,7 +251,8 @@ JS
//////////////////////////////////////////////////////////////////////////
$iCountProviders = 0;
$oUser = UserRights::GetUserObject();
$aProviders = utils::GetClassesForInterface('iNewsroomProvider', '', array('[\\\\/]lib[\\\\/]', '[\\\\/]node_modules[\\\\/]', '[\\\\/]test[\\\\/]', '[\\\\/]tests[\\\\/]'));
/** @var iNewsroomProvider[] $aProviders */
$aProviders = InterfaceDiscovery::GetInstance()->FindItopClasses(iNewsroomProvider::class);
foreach($aProviders as $cProvider)
{
$oProvider = new $cProvider();
@@ -831,8 +833,8 @@ try {
$oPage->add_header('Location: '.$sURL);
break;
case 'apply_keyboard_shortcuts':
// Note: Mind the 4 blackslashes, see utils::GetClassesForInterface()
$aShortcutClasses = utils::GetClassesForInterface('iKeyboardShortcut', '', array('[\\\\/]lib[\\\\/]', '[\\\\/]node_modules[\\\\/]', '[\\\\/]test[\\\\/]', '[\\\\/]tests[\\\\/]'));
/** @var iKeyboardShortcut[] $aShortcutClasses */
$aShortcutClasses = InterfaceDiscovery::GetInstance()->FindItopClasses(iKeyboardShortcut::class);
$aShortcutPrefs = [];
foreach ($aShortcutClasses as $cShortcutPlugin) {
foreach ($cShortcutPlugin::GetShortcutKeys() as $aShortcutKey) {
@@ -852,7 +854,8 @@ try {
case 'apply_newsroom_preferences':
$iCountProviders = 0;
$oUser = UserRights::GetUserObject();
$aProviders = utils::GetClassesForInterface('iNewsroomProvider', '', array('[\\\\/]lib[\\\\/]', '[\\\\/]node_modules[\\\\/]', '[\\\\/]test[\\\\/]', '[\\\\/]tests[\\\\/]'));
/** @var iNewsroomProvider[] $aProviders */
$aProviders = InterfaceDiscovery::GetInstance()->FindItopClasses(iNewsroomProvider::class);
foreach ($aProviders as $cProvider) {
$oProvider = new $cProvider();
if ($oProvider->IsApplicable($oUser)) {

View File

@@ -16,6 +16,8 @@
// You should have received a copy of the GNU Affero General Public License
// along with iTop. If not, see <http://www.gnu.org/licenses/>
use Combodo\iTop\Service\InterfaceDiscovery\InterfaceDiscovery;
require_once(APPROOT.'core/tar-itop.class.inc.php');
interface BackupArchive
@@ -272,7 +274,7 @@ class DBBackup
$aExtraFiles = MetaModel::GetModuleSetting('itop-backup', 'extra_files', []);
}
foreach (utils::GetClassesForInterface('iBackupExtraFilesExtension', '', ['[\\\\/]lib[\\\\/]', '[\\\\/]node_modules[\\\\/]', '[\\\\/]test[\\\\/]', '[\\\\/]tests[\\\\/]']) as $sExtensionClass)
foreach (InterfaceDiscovery::GetInstance()->FindItopClasses(iBackupExtraFilesExtension::class) as $sExtensionClass)
{
/** @var iBackupExtraFilesExtension $oExtensionInstance */
$oExtensionInstance = new $sExtensionClass();

View File

@@ -9,8 +9,8 @@ namespace Combodo\iTop\Application\TwigBase\UI;
use Combodo\iTop\Application\UI\Base\iUIBlockFactory;
use Combodo\iTop\Service\InterfaceDiscovery\InterfaceDiscovery;
use Twig\Extension\AbstractExtension;
use utils;
/**
* Class UIBlockExtension
@@ -28,9 +28,7 @@ class UIBlockExtension extends AbstractExtension
{
$aParsers = [];
$sInterface = iUIBlockFactory::class;
$aFactoryClasses = utils::GetClassesForInterface($sInterface, 'UIBlockFactory');
$aFactoryClasses = InterfaceDiscovery::GetInstance()->FindItopClasses(iUIBlockFactory::class);
foreach ($aFactoryClasses as $sFactoryClass) {
$aParsers[] = new UIBlockParser($sFactoryClass);
}

View File

@@ -20,6 +20,8 @@
namespace Combodo\iTop\Application\UI\Base\Component\PopoverMenu\NewsroomMenu;
use appUserPreferences;
use Combodo\iTop\Service\InterfaceDiscovery\InterfaceDiscovery;
use iNewsroomProvider;
use MetaModel;
use UserRights;
use utils;
@@ -62,10 +64,8 @@ class NewsroomMenuFactory
{
$aProviderParams=[];
$oUser = UserRights::GetUserObject();
/**
* @var \iNewsroomProvider[] $aProviders
*/
$aProviders = utils::GetClassesForInterface('iNewsroomProvider', '', array('[\\\\/]lib[\\\\/]', '[\\\\/]node_modules[\\\\/]', '[\\\\/]test[\\\\/]', '[\\\\/]tests[\\\\/]'));
/** @var iNewsroomProvider[] $aProviders */
$aProviders = InterfaceDiscovery::GetInstance()->FindItopClasses(iNewsroomProvider::class);
foreach($aProviders as $cProvider) {
$oProvider = new $cProvider();
$oConfig = MetaModel::GetConfig();

View File

@@ -3,6 +3,7 @@
namespace Combodo\iTop\Application\WelcomePopup;
use AttributeDateTime;
use Combodo\iTop\Service\InterfaceDiscovery\InterfaceDiscovery;
use DBObjectSearch;
use DBObjectSet;
use Exception;
@@ -12,7 +13,6 @@ use MetaModel;
use UserRights;
use WelcomePopupAcknowledge;
use iWelcomePopupExtension;
use utils;
/**
* Handling of the messages displayed in the "Welcome Popup"
@@ -175,7 +175,7 @@ class WelcomePopupService
if ($this->aMessagesProviders !== null) return;
$aProviders = [];
$aProviderClasses = utils::GetClassesForInterface(iWelcomePopupExtension::class, '', array('[\\\\/]lib[\\\\/]', '[\\\\/]node_modules[\\\\/]', '[\\\\/]test[\\\\/]', '[\\\\/]tests[\\\\/]'));
$aProviderClasses = InterfaceDiscovery::GetInstance()->FindItopClasses(iWelcomePopupExtension::class);
foreach($aProviderClasses as $sProviderClass) {
$aProviders[] = new $sProviderClass();
}

View File

@@ -32,7 +32,7 @@ class Validator extends CustomRegexpValidator
public function __construct($sRegExp = null, $sErrorMessage = null)
{
// cannot use DeprecatedCallsLog::NotifyDeprecatedFile as it would trigger an exception on dev env
// because all autoloader files are loaded during MetaModel::Startup (calling \Combodo\iTop\Service\Events\EventService::InitService calling \utils::GetClassesForInterface)
// because all autoloader files are loaded during MetaModel::Startup (calling \Combodo\iTop\Service\Events\EventService::InitService calling InterfaceDiscovery::FindItopClasses)
DeprecatedCallsLog::NotifyDeprecatedPhpMethod('3.1.0 N°6414 use '.CustomRegexpValidator::class.' instead');
parent::__construct($sRegExp, $sErrorMessage);

View File

@@ -20,11 +20,11 @@
namespace Combodo\iTop\Renderer;
use Combodo\iTop\Service\InterfaceDiscovery\InterfaceDiscovery;
use Exception;
use Combodo\iTop\Form\Form;
use Combodo\iTop\Form\Field\Field;
use iFieldRendererMappingsExtension;
use utils;
/**
* Description of FormRenderer
@@ -58,8 +58,8 @@ abstract class FormRenderer
$this->sBaseLayout = '';
$this->InitOutputs();
/** @var \iFieldRendererMappingsExtension $sImplementingClass */
foreach (utils::GetClassesForInterface(iFieldRendererMappingsExtension::class, '', ['[\\\\/]lib[\\\\/]', '[\\\\/]node_modules[\\\\/]', '[\\\\/]test[\\\\/]', '[\\\\/]tests[\\\\/]']) as $sImplementingClass) {
/** @var iFieldRendererMappingsExtension $sImplementingClass */
foreach (InterfaceDiscovery::GetInstance()->FindItopClasses(iFieldRendererMappingsExtension::class) as $sImplementingClass) {
$aFieldRendererMappings = $sImplementingClass::RegisterSupportedFields();
// For each mapping we need to check if it can be registered for the current form renderer or not
foreach ($aFieldRendererMappings as $aFieldRendererMapping) {

View File

@@ -0,0 +1,155 @@
<?php
namespace Combodo\iTop\Service\Cache;
use Exception;
use SetupUtils;
use utils;
/**
* A simple cache service that stores data in files
* - No TTL: automatically invalidated when iTop is recompiled
* - Concurrent access safe
*
* @since 3.2.0
* @experimental The API may change in future versions
* @internal DO NOT use in extensions until it is declared stable
*/
class DataModelDependantCache
{
private static DataModelDependantCache $oInstance;
public static function GetInstance(): DataModelDependantCache
{
if (!isset(self::$oInstance))
{
self::$oInstance = new DataModelDependantCache(utils::GetCachePath());
}
return self::$oInstance;
}
private ?string $sStorageRootDir; // Nullable for test purposes
private function __construct($sStorageRootDir)
{
$this->sStorageRootDir = $sStorageRootDir;
}
/**
* @param string $sPool
* @param string $sKey Any characters allowed, special characters equivalent to '_'
* @param mixed $value Any primitive type, immutable object, or array of such. NULL is not allowed.
* @param array $aMoreInfo Key-value pairs to store additional information about the cache entry
*
* @return void
* @throws \Exception
*/
public function Store(string $sPool, string $sKey, mixed $value, array $aMoreInfo = []): void
{
if(is_null($value)) {
// NULL cannot be stored as it collides with Fetch() returning NULL when the key does not exist
throw new Exception('Cannot store NULL in the cache');
}
$sCacheFileName = $this->MakeCacheFileName($sPool, $sKey);
SetupUtils::builddir(dirname($sCacheFileName));
$sMoreInfo = '';
foreach ($aMoreInfo as $sKey => $sValue) {
$sMoreInfo .= "\n// $sKey: $sValue";
}
$sCacheContent = "<?php $sMoreInfo\nreturn ".var_export($value, true).";";
file_put_contents($sCacheFileName, $sCacheContent, LOCK_EX);
}
/**
* Fetch the cached values for a given key, in the current pool
*
* @param string $sPool
* @param string $sKey
*
* @return mixed|null returns null if the key does not exist in the current pool
*/
public function Fetch(string $sPool, string $sKey): mixed
{
$sCacheFileName = $this->MakeCacheFileName($sPool, $sKey);
if (!is_file($sCacheFileName)) return null;
return include $sCacheFileName;
}
/**
* @param string $sPool
* @param string $sKey
*
* @return bool
*/
public function HasEntry(string $sPool, string $sKey): bool
{
return file_exists($this->MakeCacheFileName($sPool, $sKey));
}
/**
* Get the last modification time of a cache entry
*
* @param string $sPool
* @param string $sKey
*
* @return int|null Unix timestamp or null if the entry doesn't exist
*/
public function GetEntryModificationTime(string $sPool, string $sKey): int|null
{
$sCacheFileName = $this->MakeCacheFileName($sPool, $sKey);
if (!is_file($sCacheFileName)) return null;
return filemtime($sCacheFileName);
}
/**
* Remove an entry from the current pool
*
* @param string $sPool
* @param string $sKey
*
* @return void
*/
public function DeleteItem(string $sPool, string $sKey): void
{
$sCacheFileName = $this->MakeCacheFileName($sPool, $sKey);
if (is_file($sCacheFileName)) {
unlink($sCacheFileName);
}
}
/**
* Remove all entries from the current pool
*
* @param string $sPool
*
* @return void
* @throws Exception
*/
public function Clear(string $sPool): void
{
$sPoolDir = $this->MakePoolDirPath($sPool);
if (is_dir($sPoolDir)) {
SetupUtils::tidydir($sPoolDir);
}
}
private function MakeCacheFileName(string $sPool, string $sKey): string
{
// Replace all characters that are not alphanumeric by '_'
$sKey = preg_replace('/[^a-zA-Z0-9]/', '_', $sKey);
return $this->MakePoolDirPath($sPool).$sKey.'.php';
}
private function MakePoolDirPath(string $sPool): string
{
return $this->GetStorageRootDir()."/$sPool/";
}
/** Overridable for testing purposes */
protected function GetStorageRootDir(): string
{
// Could be forced by tests
return $this->sStorageRootDir ?? utils::GetCachePath();
}
}

View File

@@ -8,6 +8,7 @@ namespace Combodo\iTop\Service\Events;
use Closure;
use Combodo\iTop\Service\Events\Description\EventDescription;
use Combodo\iTop\Service\InterfaceDiscovery\InterfaceDiscovery;
use Combodo\iTop\Service\Module\ModuleService;
use ContextTag;
use CoreException;
@@ -43,9 +44,9 @@ final class EventService
*/
public static function InitService()
{
$aEventServiceSetup = utils::GetClassesForInterface(iEventServiceSetup::class, '', ['[\\\\/]lib[\\\\/]', '[\\\\/]node_modules[\\\\/]', '[\\\\/]test[\\\\/]']);
$aEventServiceSetup = InterfaceDiscovery::GetInstance()->FindItopClasses(iEventServiceSetup::class);
foreach ($aEventServiceSetup as $sEventServiceSetupClass) {
/** @var \Combodo\iTop\Service\Events\iEventServiceSetup $oEventServiceSetup */
/** @var iEventServiceSetup $oEventServiceSetup */
$oEventServiceSetup = new $sEventServiceSetupClass();
$oEventServiceSetup->RegisterEventsAndListeners();
}

View File

@@ -0,0 +1,206 @@
<?php
namespace Combodo\iTop\Service\InterfaceDiscovery;
use Combodo\iTop\Service\Cache\DataModelDependantCache;
use Exception;
use IssueLog;
use LogChannels;
use ReflectionClass;
use utils;
class InterfaceDiscovery
{
private static InterfaceDiscovery $oInstance;
private ?array $aForcedClassMap = null; // For testing purposes
private function __construct()
{
}
public static function GetInstance(): InterfaceDiscovery
{
if (!isset(self::$oInstance))
{
self::$oInstance = new InterfaceDiscovery();
}
return self::$oInstance;
}
/**
* Find the ITOP classes implementing a given interface. The returned classes have the following properties:
* - They can be instantiated
* - They are not aliases
*
* @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
*/
public function FindItopClasses(string $sInterface, ?array $aAdditionalExcludedPaths = null): array
{
if (is_null($aAdditionalExcludedPaths)) {
return $this->FindClasses($sInterface, ['/lib/', '/node_modules/', '/test/', '/tests/']);
}
$aExcludedPaths = array_merge(['/lib/', '/node_modules/', '/test/', '/tests/'], $aAdditionalExcludedPaths);
return $this->FindClasses($sInterface, $aExcludedPaths);
}
private function FindClasses(string $sInterface, array $aExcludedPaths = []): array
{
$sCacheUniqueKey = $this->MakeCacheKey($sInterface, $aExcludedPaths);
if ($this->IsCacheValid($sCacheUniqueKey)) {
return $this->ReadClassesFromCache($sCacheUniqueKey);
}
$sExcludedPathsRegExp = $this->GetExcludedPathsRegExp($aExcludedPaths);
$aMatchingClasses = [];
foreach ($this->GetCandidateClasses() as $sPHPClass => $sOptionalPHPFile) {
if (!$this->IsValidPHPFile($sOptionalPHPFile, $sExcludedPathsRegExp)) {
continue;
}
if ($this->IsInterfaceImplementation($sPHPClass, $sInterface)) {
$aMatchingClasses[] = $sPHPClass;
}
}
$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;
}
// 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);
return $aAutoloadClassMaps;
}
private function IsInterfaceImplementation(string $sPHPClass, string $sInterface): bool
{
try {
$oRefClass = new ReflectionClass($sPHPClass);
} 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
return true;
}
protected function GetCandidateClasses(): array
{
$aClassMap = [];
$aAutoloaderErrors = [];
if ($this->aForcedClassMap !== null) {
$aClassMap = $this->aForcedClassMap;
}
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
$aAutoloaderErrors[] = $sAutoloadFile;
continue;
}
$aTmpClassMap = include $sAutoloadFile;
/** @noinspection SlowArrayOperationsInLoopInspection we are getting an associative array so the documented workarounds cannot be used */
$aClassMap = array_merge($aClassMap, $aTmpClassMap);
}
if (count($aAutoloaderErrors) > 0) {
IssueLog::Debug(
__METHOD__." cannot load some of the autoloader files",
LogChannels::CORE,
['autoloader_errors' => $aAutoloaderErrors]
);
}
// Add already loaded classes
$aCurrentClasses = array_fill_keys(get_declared_classes(), '');
$aClassMap = array_merge($aCurrentClasses, $aClassMap);
}
return $aClassMap;
}
private function IsValidPHPFile(string $sOptionalPHPFile, ?string $sExcludedPathsRegExp): bool
{
if ($sOptionalPHPFile === '') return true;
$sOptionalPHPFile = utils::LocalPath($sOptionalPHPFile);
if ($sOptionalPHPFile === false) return false;
if (is_null($sExcludedPathsRegExp)) return true;
if (preg_match($sExcludedPathsRegExp, '/'.$sOptionalPHPFile) === 1) {
return false;
}
return true;
}
private function IsCacheValid(string $sKey): bool
{
if ($this->aForcedClassMap !== null) return false;
$oCacheService = DataModelDependantCache::GetInstance();
if (!$oCacheService->HasEntry('InterfaceDiscovery', $sKey)) return false;
if (!utils::IsDevelopmentEnvironment()) 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) {
$iSourceTime = filemtime($sSourceFile);
if ($iSourceTime > $iCacheTime) return false;
}
return true;
}
public function ReadClassesFromCache(string $sKey): array
{
$oCacheService = DataModelDependantCache::GetInstance();
return $oCacheService->Fetch('InterfaceDiscovery', $sKey);
}
protected function SaveClassesToCache(string $sKey, array $aMatchingClasses, array $aMoreInfo): void
{
if ($this->aForcedClassMap !== null) return;
$oCacheService = DataModelDependantCache::GetInstance();
$oCacheService->Store('InterfaceDiscovery', $sKey, $aMatchingClasses, $aMoreInfo);
}
private function GetExcludedPathsRegExp(array $aExcludedPaths) : ?string
{
if (count($aExcludedPaths) == 0) return null;
$aExcludedPathRegExps = array_map(function($sPath) {
return preg_quote($sPath, '#');
}, $aExcludedPaths);
return '#'.implode('|', $aExcludedPathRegExps).'#';
}
protected function MakeCacheKey(string $sInterface, array $aExcludedPaths): string
{
if (count($aExcludedPaths) == 0) return $sInterface;
return md5($sInterface.':'.implode(',', $aExcludedPaths));
}
}

View File

@@ -6,6 +6,8 @@
namespace Combodo\iTop\Service\Router;
use Combodo\iTop\Controller\iController;
use Combodo\iTop\Service\InterfaceDiscovery\InterfaceDiscovery;
use Combodo\iTop\Service\Router\Exception\RouteNotFoundException;
use ReflectionClass;
use ReflectionMethod;
@@ -174,7 +176,7 @@ class Router
// If no cache, force to re-scan for routes
if (count($aRoutes) === 0) {
foreach (utils::GetClassesForInterface('Combodo\iTop\Controller\iController', '', ['[\\\\/]lib[\\\\/]', '[\\\\/]node_modules[\\\\/]', '[\\\\/]test[\\\\/]']) as $sControllerFQCN) {
foreach (InterfaceDiscovery::GetInstance()->FindItopClasses(iController::class) as $sControllerFQCN) {
$sRouteNamespace = $sControllerFQCN::ROUTE_NAMESPACE;
// Ignore controller with no namespace
if (is_null($sRouteNamespace)) {

View File

@@ -501,4 +501,26 @@ abstract class ItopTestCase extends TestCase
}
closedir($dir);
}
/**
* An alternative to assertEquals in case the order of the elements in the array is not important
*
* @since 3.2.0
*/
protected function AssertArraysHaveSameItems(array $aExpectedClasses, array $aClasses, string $sMessage = ''): void
{
sort($aClasses);
sort($aExpectedClasses);
$sExpected = implode("\n", $aExpectedClasses);
$sActual = implode("\n", $aClasses);
if ($sExpected === $sActual) {
$this->assertTrue(true);
return;
}
$sMessage .= "\nExpected:\n$sExpected\nActual:\n$sActual";
var_export($aClasses);
$this->fail($sMessage);
}
}

View File

@@ -6,6 +6,8 @@
namespace Combodo\iTop\Test\UnitTest\Application;
use Combodo\iTop\Application\UI\Base\iUIBlockFactory;
use Combodo\iTop\Service\InterfaceDiscovery\InterfaceDiscovery;
use Combodo\iTop\Test\UnitTest\ItopCustomDatamodelTestCase;
use MetaModel;
use utils;
@@ -26,6 +28,186 @@ class ApplicationExtensionTest extends ItopCustomDatamodelTestCase
return __DIR__ . '/Delta/application-extension-usages-in-snippets.xml';
}
/**
* TODO: remove when the refactoring is done
*/
public function testInterfaceDiscovery(): void
{
$oInterfaceDiscoveryService = InterfaceDiscovery::GetInstance();
$this->AssertArraysHaveSameItems(
[
0 => 'Combodo\iTop\Application\UI\Base\Component\Alert\AlertUIBlockFactory',
1 => 'Combodo\iTop\Application\UI\Base\Component\ButtonGroup\ButtonGroupUIBlockFactory',
2 => 'Combodo\iTop\Application\UI\Base\Component\Button\ButtonUIBlockFactory',
3 => 'Combodo\iTop\Application\UI\Base\Component\CollapsibleSection\CollapsibleSectionUIBlockFactory',
4 => 'Combodo\iTop\Application\UI\Base\Component\DataTable\DataTableUIBlockFactory',
5 => 'Combodo\iTop\Application\UI\Base\Component\FieldBadge\FieldBadgeUIBlockFactory',
6 => 'Combodo\iTop\Application\UI\Base\Component\FieldSet\FieldSetUIBlockFactory',
7 => 'Combodo\iTop\Application\UI\Base\Component\Field\FieldUIBlockFactory',
8 => 'Combodo\iTop\Application\UI\Base\Component\Form\FormUIBlockFactory',
9 => 'Combodo\iTop\Application\UI\Base\Component\Input\FileSelect\FileSelectUIBlockFactory',
10 => 'Combodo\iTop\Application\UI\Base\Component\Input\InputUIBlockFactory',
11 => 'Combodo\iTop\Application\UI\Base\Component\Input\Select\SelectOptionUIBlockFactory',
12 => 'Combodo\iTop\Application\UI\Base\Component\Input\Select\SelectUIBlockFactory',
13 => 'Combodo\iTop\Application\UI\Base\Component\Input\Set\SetUIBlockFactory',
14 => 'Combodo\iTop\Application\UI\Base\Component\Panel\PanelUIBlockFactory',
15 => 'Combodo\iTop\Application\UI\Base\Component\Spinner\SpinnerUIBlockFactory',
16 => 'Combodo\iTop\Application\UI\Base\Component\Template\TemplateUIBlockFactory',
17 => 'Combodo\iTop\Application\UI\Base\Component\Title\TitleUIBlockFactory',
18 => 'Combodo\iTop\Application\UI\Base\Component\Toolbar\Separator\ToolbarSeparatorUIBlockFactory',
19 => 'Combodo\iTop\Application\UI\Base\Component\Toolbar\ToolbarSpacer\ToolbarSpacerUIBlockFactory',
20 => 'Combodo\iTop\Application\UI\Base\Component\Toolbar\ToolbarUIBlockFactory',
21 => 'Combodo\iTop\Application\UI\Base\Layout\MultiColumn\Column\ColumnUIBlockFactory',
22 => 'Combodo\iTop\Application\UI\Base\Layout\MultiColumn\MultiColumnUIBlockFactory',
23 => 'Combodo\iTop\Application\UI\Base\Layout\UIContentBlockUIBlockFactory',
24 => 'Combodo\iTop\Application\UI\Links\Set\LinkSetUIBlockFactory',
],
$oInterfaceDiscoveryService->FindItopClasses(iUIBlockFactory::class)
);
}
/**
* Protection test for Refactoring
* TODO: This test should be removed when the refactoring is done
* @--data--Provider HardcodedImplementationsProvider
*/
public function testGetClassesForInterfaceReturnsExactlySomething()
{
foreach ($this->InterfaceToExpectedClasses() as $sInterface => $aExpectedClasses) {
$aClasses = utils::GetClassesForInterface($sInterface, '', ['[\\\\/]lib[\\\\/]', '[\\\\/]node_modules[\\\\/]', '[\\\\/]test[\\\\/]', '[\\\\/]tests[\\\\/]']);
$this->AssertArraysHaveSameItems($aExpectedClasses, $aClasses, "Found unexpected classes extending the '$sInterface' API");
}
}
public function InterfaceToExpectedClasses(): array
{
return [
\iFieldRendererMappingsExtension::class => [
'ExampleFor_iFieldRendererMappingsExtension',
'Combodo\iTop\Renderer\Bootstrap\BsFieldRendererMappings',
'Combodo\iTop\Renderer\Console\ConsoleFieldRendererMappings',
],
\iNewsroomProvider::class => [
'ExampleFor_iNewsroomProvider',
'HubNewsroomProvider',
'Combodo\iTop\Application\Newsroom\iTopNewsroomProvider',
],
\iBackupExtraFilesExtension::class => [
'ExampleFor_iBackupExtraFilesExtension',
],
\Combodo\iTop\Application\UI\Base\iUIBlockFactory::class => [
0 => 'Combodo\iTop\Application\UI\Base\Component\Input\Select\SelectUIBlockFactory',
2 => 'Combodo\iTop\Application\UI\Base\Component\Alert\AlertUIBlockFactory',
3 => 'Combodo\iTop\Application\UI\Base\Component\ButtonGroup\ButtonGroupUIBlockFactory',
4 => 'Combodo\iTop\Application\UI\Base\Component\Button\ButtonUIBlockFactory',
5 => 'Combodo\iTop\Application\UI\Base\Component\CollapsibleSection\CollapsibleSectionUIBlockFactory',
6 => 'Combodo\iTop\Application\UI\Base\Component\DataTable\DataTableUIBlockFactory',
7 => 'Combodo\iTop\Application\UI\Base\Component\FieldBadge\FieldBadgeUIBlockFactory',
8 => 'Combodo\iTop\Application\UI\Base\Component\FieldSet\FieldSetUIBlockFactory',
9 => 'Combodo\iTop\Application\UI\Base\Component\Field\FieldUIBlockFactory',
10 => 'Combodo\iTop\Application\UI\Base\Component\Form\FormUIBlockFactory',
11 => 'Combodo\iTop\Application\UI\Base\Component\Input\FileSelect\FileSelectUIBlockFactory',
12 => 'Combodo\iTop\Application\UI\Base\Component\Input\InputUIBlockFactory',
13 => 'Combodo\iTop\Application\UI\Base\Component\Input\Select\SelectOptionUIBlockFactory',
14 => 'Combodo\iTop\Application\UI\Base\Component\Input\Set\SetUIBlockFactory',
15 => 'Combodo\iTop\Application\UI\Base\Component\Panel\PanelUIBlockFactory',
16 => 'Combodo\iTop\Application\UI\Base\Component\Spinner\SpinnerUIBlockFactory',
17 => 'Combodo\iTop\Application\UI\Base\Component\Template\TemplateUIBlockFactory',
18 => 'Combodo\iTop\Application\UI\Base\Component\Title\TitleUIBlockFactory',
19 => 'Combodo\iTop\Application\UI\Base\Component\Toolbar\Separator\ToolbarSeparatorUIBlockFactory',
20 => 'Combodo\iTop\Application\UI\Base\Component\Toolbar\ToolbarSpacer\ToolbarSpacerUIBlockFactory',
21 => 'Combodo\iTop\Application\UI\Base\Component\Toolbar\ToolbarUIBlockFactory',
22 => 'Combodo\iTop\Application\UI\Base\Layout\MultiColumn\Column\ColumnUIBlockFactory',
23 => 'Combodo\iTop\Application\UI\Base\Layout\MultiColumn\MultiColumnUIBlockFactory',
24 => 'Combodo\iTop\Application\UI\Base\Layout\UIContentBlockUIBlockFactory',
25 => 'Combodo\iTop\Application\UI\Links\Set\LinkSetUIBlockFactory',
],
\Combodo\iTop\Controller\iController::class => [
0 => 'Combodo\iTop\CoreUpdate\Controller\UpdateController',
1 => 'Combodo\iTop\CoreUpdate\Controller\AjaxController',
2 => 'Combodo\iTop\Controller\Base\Layout\ActivityPanelController',
3 => 'Combodo\iTop\Controller\Base\Layout\ObjectController',
4 => 'Combodo\iTop\Controller\Links\LinkSetController',
5 => 'Combodo\iTop\Controller\Newsroom\iTopNewsroomController',
6 => 'Combodo\iTop\Controller\Notifications\ActionController',
7 => 'Combodo\iTop\Controller\Notifications\NotificationsCenterController',
8 => 'Combodo\iTop\Controller\OAuth\OAuthLandingController',
9 => 'Combodo\iTop\Controller\PreferencesController',
10 => 'Combodo\iTop\Controller\TemporaryObjects\TemporaryObjectController',
11 => 'Combodo\iTop\Controller\WelcomePopupController',
12 => 'Combodo\iTop\OAuthClient\Controller\AjaxOauthClientController',
13 => 'Combodo\iTop\OAuthClient\Controller\OAuthClientController',
],
\Combodo\iTop\Application\UI\Hook\iKeyboardShortcut::class => [
0 => 'Combodo\iTop\Application\UI\Base\Component\GlobalSearch\GlobalSearch',
1 => 'Combodo\iTop\Application\UI\Base\Component\QuickCreate\QuickCreate',
2 => 'Combodo\iTop\Application\UI\Base\Layout\NavigationMenu\NavigationMenu',
3 => 'Combodo\iTop\Application\UI\Base\Layout\Object\ObjectDetails',
4 => 'Combodo\iTop\Application\UI\Base\Layout\Object\ObjectSummary',
],
\Combodo\iTop\Service\Events\iEventServiceSetup::class => [
0 => 'Combodo\iTop\Application\EventRegister\ApplicationEvents',
1 => 'Combodo\iTop\Core\EventListener\AttributeBlobEventListener',
2 => 'Combodo\iTop\Service\TemporaryObjects\TemporaryObjectsEvents',
3 => 'Combodo\iTop\Attachments\Hook\EventListener',
],
\iWelcomePopupExtension::class => [
'Combodo\iTop\Application\WelcomePopup\Provider\DefaultProvider'
],
\iProcess::class => [
0 => 'BackupExec',
1 => 'BulkExportResultGC',
2 => 'CheckStopWatchThresholds',
3 => 'Combodo\iTop\Service\Notification\Event\EventNotificationNewsroomGC',
4 => 'Combodo\iTop\Service\TemporaryObjects\TemporaryObjectGC',
5 => 'Combodo\iTop\SessionTracker\SessionGC',
6 => 'ExecAsyncTask',
7 => 'InlineImageGC',
8 => 'LogFileRotationProcess',
9 => 'ObsolescenceDateUpdater',
]
];
}
/**
* TODO: This test should be removed when the refactoring is done
* Protection of the use case UIBlockExtension::getTokenParsers(), which uses a filter on the class, and does not exclude any directories
*/
public function testVerySpecificCallToGetClassesForInterfaces()
{
$this->AssertArraysHaveSameItems(
[
0 => 'Combodo\iTop\Application\UI\Base\Component\Alert\AlertUIBlockFactory',
1 => 'Combodo\iTop\Application\UI\Base\Component\ButtonGroup\ButtonGroupUIBlockFactory',
2 => 'Combodo\iTop\Application\UI\Base\Component\Button\ButtonUIBlockFactory',
3 => 'Combodo\iTop\Application\UI\Base\Component\CollapsibleSection\CollapsibleSectionUIBlockFactory',
4 => 'Combodo\iTop\Application\UI\Base\Component\DataTable\DataTableUIBlockFactory',
5 => 'Combodo\iTop\Application\UI\Base\Component\FieldBadge\FieldBadgeUIBlockFactory',
6 => 'Combodo\iTop\Application\UI\Base\Component\FieldSet\FieldSetUIBlockFactory',
7 => 'Combodo\iTop\Application\UI\Base\Component\Field\FieldUIBlockFactory',
8 => 'Combodo\iTop\Application\UI\Base\Component\Form\FormUIBlockFactory',
9 => 'Combodo\iTop\Application\UI\Base\Component\Input\FileSelect\FileSelectUIBlockFactory',
10 => 'Combodo\iTop\Application\UI\Base\Component\Input\InputUIBlockFactory',
11 => 'Combodo\iTop\Application\UI\Base\Component\Input\Select\SelectOptionUIBlockFactory',
12 => 'Combodo\iTop\Application\UI\Base\Component\Input\Select\SelectUIBlockFactory',
13 => 'Combodo\iTop\Application\UI\Base\Component\Input\Set\SetUIBlockFactory',
14 => 'Combodo\iTop\Application\UI\Base\Component\Panel\PanelUIBlockFactory',
15 => 'Combodo\iTop\Application\UI\Base\Component\Spinner\SpinnerUIBlockFactory',
16 => 'Combodo\iTop\Application\UI\Base\Component\Template\TemplateUIBlockFactory',
17 => 'Combodo\iTop\Application\UI\Base\Component\Title\TitleUIBlockFactory',
18 => 'Combodo\iTop\Application\UI\Base\Component\Toolbar\Separator\ToolbarSeparatorUIBlockFactory',
19 => 'Combodo\iTop\Application\UI\Base\Component\Toolbar\ToolbarSpacer\ToolbarSpacerUIBlockFactory',
20 => 'Combodo\iTop\Application\UI\Base\Component\Toolbar\ToolbarUIBlockFactory',
21 => 'Combodo\iTop\Application\UI\Base\Layout\MultiColumn\Column\ColumnUIBlockFactory',
22 => 'Combodo\iTop\Application\UI\Base\Layout\MultiColumn\MultiColumnUIBlockFactory',
23 => 'Combodo\iTop\Application\UI\Base\Layout\UIContentBlockUIBlockFactory',
24 => 'Combodo\iTop\Application\UI\Links\Set\LinkSetUIBlockFactory',
],
utils::GetClassesForInterface(iUIBlockFactory::class, 'UIBlockFactory')
);
}
/**
* This test ensures that APIs are discovered / registered / called.
*
@@ -44,7 +226,7 @@ class ApplicationExtensionTest extends ItopCustomDatamodelTestCase
if ($sCallMethod === static::ENUM_API_CALL_METHOD_ENUMPLUGINS) {
$iExtendingClassesCount = count(MetaModel::EnumPlugins($sAPIFQCN));
} else {
$iExtendingClassesCount = count(utils::GetClassesForInterface($sAPIFQCN, '', ['[\\\\/]lib[\\\\/]', '[\\\\/]node_modules[\\\\/]', '[\\\\/]test[\\\\/]', '[\\\\/]tests[\\\\/]']));
$iExtendingClassesCount = count(InterfaceDiscovery::GetInstance()->FindItopClasses($sAPIFQCN));
}
$this->assertGreaterThan(0, $iExtendingClassesCount, "Found no class extending the $sAPIFQCN API");
}

View File

@@ -0,0 +1,166 @@
<?php
namespace Combodo\iTop\Test\UnitTest\Service\Cache;
use Combodo\iTop\Service\Cache\DataModelDependantCache;
use Combodo\iTop\Test\UnitTest\ItopTestCase;
use Exception;
class DataModelDependantCacheTest extends ItopTestCase
{
private DataModelDependantCache $oCacheService;
private string $sCacheRootDir;
protected function setUp(): void
{
parent::setUp();
$this->RequireOnceItopFile('setup/setuputils.class.inc.php');
$this->sCacheRootDir = self::CreateTmpdir();
$this->oCacheService = DataModelDependantCache::GetInstance();
$this->SetNonPublicProperty($this->oCacheService, 'sStorageRootDir', $this->sCacheRootDir);
}
protected function tearDown(): void
{
$this->SetNonPublicProperty($this->oCacheService, 'sStorageRootDir', null);
self::RecurseRmdir($this->sCacheRootDir);
parent::tearDown();
}
public function testShouldStoreAndFetchVariousDataTypes(): void
{
$this->oCacheService->Store('pool-A', 'key-array', ['value1', 'value2']);
$this->oCacheService->Store('pool-A', 'key-string', 'foo');
$this->oCacheService->Store('pool-A', 'key-int', 1971);
$this->assertEquals(['value1', 'value2'], $this->oCacheService->Fetch('pool-A', 'key-array'));
$this->assertEquals('foo', $this->oCacheService->Fetch('pool-A', 'key-string'));
$this->assertEquals(1971, $this->oCacheService->Fetch('pool-A', 'key-int'));
}
public function testShouldNotAllowToStoreNull(): void
{
$this->ExpectExceptionMessage('Cannot store NULL in the cache');
$this->oCacheService->Store('pool-A', 'key', null);
}
public function testShouldStoreInADirectoryRebuiltOnCompilation(): void
{
// Given the storage is reset to the default
$this->SetNonPublicProperty($this->oCacheService, 'sStorageRootDir', null);
// Then
$sFilePath = $this->InvokeNonPublicMethod(DataModelDependantCache::class, 'MakeCacheFileName', $this->oCacheService, ['pool-A', 'key']);
$this->assertStringContainsString('data/cache-', $sFilePath);
}
public function testPoolShouldSeparateEntriesHavingTheSameKey(): void
{
// Given
$this->oCacheService->Store('pool-A', 'key', ['data-default']);
$this->oCacheService->Store('pool-B', 'key', ['data-pool-B']);
// Then
$this->assertEquals(['data-default'], $this->oCacheService->Fetch('pool-A', 'key'));
$this->assertEquals(['data-pool-B'], $this->oCacheService->Fetch('pool-B', 'key'));
$this->assertEquals(null, $this->oCacheService->Fetch('pool-C-unknown', 'key'));
}
public function testPoolsShouldBeVisibleInThePath(): void
{
$sFilePath = $this->InvokeNonPublicMethod(DataModelDependantCache::class, 'MakeCacheFileName', $this->oCacheService, ['pool-B', 'key']);
$this->assertStringContainsString('pool-B', $sFilePath);
}
public function testFetchShouldReturnNullForNonExistingKey(): void
{
$this->assertNull($this->oCacheService->Fetch('pool-A', 'non-existing-key'));
}
public function testAnUnknownPoolShouldFailSilently(): void
{
$this->assertNull($this->oCacheService->Fetch('unknown-pool', 'non-existing-key'));
}
public function testDeleteItemAndHasEntry()
{
// Given an empy cache
// Then
$this->assertFalse($this->oCacheService->HasEntry('pool-A', 'key'), 'HasEntry should return false for non-existing key');
// When
$this->oCacheService->Store('pool-A', 'key', 'some data...');
// Then
$this->assertTrue($this->oCacheService->HasEntry('pool-A', 'key'), 'HasEntry should return true for newly created key');
// When
$this->oCacheService->DeleteItem('pool-A', 'key');
// Then
$this->assertFalse($this->oCacheService->HasEntry('pool-A', 'key'), 'HasEntry should return true for a removed key');
}
public function testDeleteItemShouldPreserveOtherEntries()
{
// Given
$this->oCacheService->Store('pool-A', 'key', 'some data...');
$this->oCacheService->Store('pool-A', 'key2', 'some data...');
// Then
$this->oCacheService->DeleteItem('pool-A', 'key');
// When
$this->assertTrue($this->oCacheService->HasEntry('pool-A', 'key2'));
}
public function testDeleteItemShouldPreserveHomonymsFromDifferentPools()
{
// Given
$this->oCacheService->Store('pool-A', 'key', 'some data...');
$this->oCacheService->Store('pool-B', 'key', 'some data...');
// When
$this->oCacheService->DeleteItem('pool-B', 'key');
// Then
$this->assertTrue($this->oCacheService->HasEntry('pool-A', 'key'), 'DeleteItem should not have altered the pool "default"');
}
public function testClearShouldRemoveAllEntriesFromTheCurrentPool()
{
// Given
$this->oCacheService->Store('pool-A', 'key', 'some data...');
$this->oCacheService->Store('pool-A', 'key2', 'some data...');
$this->oCacheService->Store('pool-B', 'key', 'some data...');
// When
$this->oCacheService->Clear('pool-A');
// Then
$this->assertFalse($this->oCacheService->HasEntry('pool-A', 'key'), 'DeleteItem should remove all entries from the current pool');
$this->assertFalse($this->oCacheService->HasEntry('pool-A', 'key2'), 'DeleteItem should remove all entries from the current pool');
$this->assertTrue($this->oCacheService->HasEntry('pool-B', 'key'), 'DeleteItem should not alter entries from other pools');
}
public function testGetEntryModificationTime()
{
// Given an entry created at a specific time
$this->oCacheService->Store('pool-A', 'key', 'some data...');
$iRefTime = time();
$sFilePath = $this->InvokeNonPublicMethod(DataModelDependantCache::class, 'MakeCacheFileName', $this->oCacheService, ['pool-A', 'key']);
touch($sFilePath, $iRefTime);
// Then
$this->assertEquals($iRefTime, $this->oCacheService->GetEntryModificationTime('pool-A', 'key'), 'GetEntryModificationTime should return the modification time of the cache file');
$this->assertEquals(null, $this->oCacheService->GetEntryModificationTime('pool-A', 'non-existing-key'), 'GetEntryModificationTime should return null for an invalid key');
}
public function testKeyUndesiredCharactersShouldBeTransformedToUnderscore()
{
$sUglyKey = 'key with ugly characters:\{&"#@ç^²/,;[(|🤔';
$sFilePath = $this->InvokeNonPublicMethod(DataModelDependantCache::class, 'MakeCacheFileName', $this->oCacheService, ['pool-A', $sUglyKey]);
$this->assertEquals('key_with_ugly_characters______________________.php', basename($sFilePath));
}
}

View File

@@ -0,0 +1,73 @@
<?php
namespace Combodo\iTop\Test\UnitTest\Service\InterfaceDiscovery;
use Combodo\iTop\Application\UI\Base\iUIBlockFactory;
use Combodo\iTop\Service\InterfaceDiscovery\InterfaceDiscovery;
use Combodo\iTop\Test\UnitTest\ItopDataTestCase;
class InterfaceDiscoveryTest extends ItopDataTestCase
{
protected function tearDown(): void
{
$this->SetNonPublicProperty(InterfaceDiscovery::GetInstance(), 'aForcedClassMap', null);
parent::tearDown();
}
public function testShouldSelectTheRequestedItopClasses()
{
$oInterfaceDiscoveryService = InterfaceDiscovery::GetInstance();
$this->GivenClassMap($oInterfaceDiscoveryService, [
'Combodo\iTop\Application\UI\Base\Component\Alert\Alert' => APPROOT . '/sources/Application/UI/Base/Component/Alert/Alert.php',
'Combodo\iTop\Application\UI\Base\Component\Alert\AlertUIBlockFactory' => APPROOT . '/sources/Application/UI/Base/Component/Alert/AlertUIBlockFactory.php',
'Combodo\iTop\Application\UI\Base\Component\ButtonGroup\ButtonGroupUIBlockFactory' => APPROOT . '/sources/Application/UI/Base/Component/ButtonGroup/ButtonGroupUIBlockFactory.php',
]);
$this->AssertArraysHaveSameItems(
[
'Combodo\iTop\Application\UI\Base\Component\Alert\AlertUIBlockFactory',
'Combodo\iTop\Application\UI\Base\Component\ButtonGroup\ButtonGroupUIBlockFactory',
],
$oInterfaceDiscoveryService->FindItopClasses(iUIBlockFactory::class)
);
}
public function testShouldExcludeSpecifiedDirectories()
{
$oInterfaceDiscoveryService = InterfaceDiscovery::GetInstance();
$this->GivenClassMap($oInterfaceDiscoveryService, [
'Combodo\iTop\Application\UI\Base\Component\Alert\AlertUIBlockFactory' => APPROOT . '/sources/Application/UI/Base/Component/Alert/AlertUIBlockFactory.php',
'Combodo\iTop\Application\UI\Base\Component\ButtonGroup\ButtonGroupUIBlockFactory' => APPROOT . '/sources/Application/UI/Base/Component/ButtonGroup/ButtonGroupUIBlockFactory.php',
]);
$this->AssertArraysHaveSameItems(
[],
$oInterfaceDiscoveryService->FindItopClasses(iUIBlockFactory::class, ['Component/ButtonGroup', '/Alert/'])
);
}
public function testShouldExcludeAliases()
{
$oInterfaceDiscoveryService = InterfaceDiscovery::GetInstance();
$this->GivenClassMap($oInterfaceDiscoveryService, [
'Combodo\iTop\Application\UI\Base\Component\Alert\AlertUIBlockFactory' => APPROOT . '/sources/Application/UI/Base/Component/Alert/AlertUIBlockFactory.php',
'AlbertIsBlockingTheFactory' => APPROOT . '/sources/Application/UI/Base/Component/Alert/AlertUIBlockFactory.php',
]);
class_alias('Combodo\iTop\Application\UI\Base\Component\Alert\AlertUIBlockFactory', 'AlbertIsBlockingTheFactory');
$this->AssertArraysHaveSameItems(
[
'Combodo\iTop\Application\UI\Base\Component\Alert\AlertUIBlockFactory',
],
$oInterfaceDiscoveryService->FindItopClasses(iUIBlockFactory::class)
);
}
private function GivenClassMap(InterfaceDiscovery $oInterfaceDiscoveryService, array $aClassMap): void
{
$this->SetNonPublicProperty($oInterfaceDiscoveryService, 'aForcedClassMap', $aClassMap);
}
}

View File

@@ -20,6 +20,7 @@
use Combodo\iTop\Application\WebPage\CLIPage;
use Combodo\iTop\Application\WebPage\Page;
use Combodo\iTop\Application\WebPage\WebPage;
use Combodo\iTop\Service\InterfaceDiscovery\InterfaceDiscovery;
require_once(__DIR__.'/../approot.inc.php');
@@ -408,7 +409,7 @@ function ReSyncProcesses($oP, $bVerbose, $bDebug)
$oNow = new DateTime();
$aProcesses = array();
foreach (utils::GetClassesForInterface('iProcess', '', ['[\\\\/]lib[\\\\/]', '[\\\\/]node_modules[\\\\/]', '[\\\\/]test[\\\\/]', '[\\\\/]tests[\\\\/]']) as $sTaskClass)
foreach (InterfaceDiscovery::GetInstance()->FindItopClasses(iProcess::class) as $sTaskClass)
{
$oProcess = new $sTaskClass;
$aProcesses[$sTaskClass] = $oProcess;