From 52c984d5e71c6ec7a39ca943762316da97d9ea8a Mon Sep 17 00:00:00 2001 From: Eric Espie Date: Fri, 18 Nov 2022 10:49:59 +0100 Subject: [PATCH] =?UTF-8?q?N=C2=B04756=20-=20Fix=20Unit=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/dbobject.class.php | 14 +++++++----- setup/compiler.class.inc.php | 4 ++-- sources/Application/Service/EventHelper.php | 5 +++++ sources/Application/Service/EventService.php | 23 +++++++++++++++----- 4 files changed, 32 insertions(+), 14 deletions(-) diff --git a/core/dbobject.class.php b/core/dbobject.class.php index 36b1885ed..b90b0ca7e 100644 --- a/core/dbobject.class.php +++ b/core/dbobject.class.php @@ -5827,13 +5827,15 @@ abstract class DBObject implements iDisplay */ public function FireEvent($sEvent, $aEventData = array()) { - $aEventData['debug_info'] = 'from: '.get_class($this).':'.$this->GetKey(); - $aEventData['object'] = $this; - $aEventSources = [$this->m_sObjectUniqId]; - foreach (MetaModel::EnumParentClasses(get_class($this), ENUM_PARENT_CLASSES_ALL, false) as $sClass) { - $aEventSources[] = $sClass; + if (EventService::IsEventRegistered($sEvent)) { + $aEventData['debug_info'] = 'from: '.get_class($this).':'.$this->GetKey(); + $aEventData['object'] = $this; + $aEventSources = [$this->m_sObjectUniqId]; + foreach (MetaModel::EnumParentClasses(get_class($this), ENUM_PARENT_CLASSES_ALL, false) as $sClass) { + $aEventSources[] = $sClass; + } + EventService::FireEvent(new EventData($sEvent, $aEventSources, $aEventData)); } - EventService::FireEvent(new EventData($sEvent, $aEventSources, $aEventData)); } protected function EventInsertRequested() diff --git a/setup/compiler.class.inc.php b/setup/compiler.class.inc.php index e70211845..678a22dd5 100644 --- a/setup/compiler.class.inc.php +++ b/setup/compiler.class.inc.php @@ -1100,8 +1100,8 @@ EOF $sDescription = var_export($aEventDescription, true); $sConstant = $sName; - $sOutput = "define('$sConstant', '$sName');\n"; - $sOutput .= "Combodo\iTop\Service\EventService::RegisterEvent('$sName', $sDescription, '$sModuleName');\n"; + $sOutput = "const $sConstant = '$sName';\n"; + $sOutput .= "Combodo\iTop\Service\EventService::RegisterEvent($sName, $sDescription, '$sModuleName');\n"; return $sOutput; } diff --git a/sources/Application/Service/EventHelper.php b/sources/Application/Service/EventHelper.php index 52d3b8a5a..56beab787 100644 --- a/sources/Application/Service/EventHelper.php +++ b/sources/Application/Service/EventHelper.php @@ -70,6 +70,11 @@ class EventHelper IssueLog::Error($sMessage, LogChannels::EVENT_SERVICE); } + public static function Warning($sMessage) + { + IssueLog::Warning($sMessage, LogChannels::EVENT_SERVICE); + } + public static function MatchEventSource($srcRegistered, $srcEvent): bool { if (empty($srcRegistered)) { diff --git a/sources/Application/Service/EventService.php b/sources/Application/Service/EventService.php index 3029d55ef..27f8936c0 100644 --- a/sources/Application/Service/EventService.php +++ b/sources/Application/Service/EventService.php @@ -16,9 +16,9 @@ use utils; class EventService { - public static $aEventListeners; - private static $iEventIdCounter; - private static $aEventDescription; + public static $aEventListeners = []; + private static $iEventIdCounter = 0; + private static $aEventDescription = []; public static function InitService() { @@ -103,7 +103,7 @@ class EventService public static function FireEvent(EventData $oEventData) { $sEvent = $oEventData->GetEvent(); - if (!array_key_exists($sEvent, self::$aEventDescription)) { + if (!self::IsEventRegistered($sEvent)) { $sError = "Fire event error: Event $sEvent is not registered"; EventHelper::Error($sError); throw new CoreException($sError); @@ -267,9 +267,10 @@ class EventService */ public static function RegisterEvent(string $sEvent, array $aDescription, string $sModule) { - if (isset(self::$aEventDescription[$sEvent])) { + if (self::IsEventRegistered($sEvent)) { $sPrevious = self::$aEventDescription[$sEvent]['module']; - EventHelper::Error("The Event $sEvent defined by $sModule has already been defined in $sPrevious, check your delta"); + EventHelper::Warning("The Event $sEvent defined by $sModule has already been defined in $sPrevious, check your delta"); + return; } self::$aEventDescription[$sEvent] = [ @@ -309,6 +310,16 @@ class EventService return implode('_', $aRet); } + /** + * @param string $sEvent + * + * @return bool + */ + public static function IsEventRegistered(string $sEvent): bool + { + return array_key_exists($sEvent, self::$aEventDescription); + } + public static function GetDefinedEventsAsJSON() { return json_encode(self::$aEventDescription, JSON_PRETTY_PRINT);