N°4756 - Ease extensibility for CRUD operations : Event Service - move event description to an object

This commit is contained in:
Eric Espie
2022-12-13 11:55:05 +01:00
parent 432579657c
commit deb6d1fd9a
7 changed files with 312 additions and 18 deletions

View File

@@ -0,0 +1,83 @@
<?php
/*
* @copyright Copyright (C) 2010-2022 Combodo SARL
* @license http://opensource.org/licenses/AGPL-3.0
*/
namespace Combodo\iTop\Service\Description;
/**
* Description of the data given with an event when registering
*
* @api
* @since 3.1.0
*/
class EventDataDescription
{
private string $sName;
private string $sDescription;
private string $sType;
/**
* Create a data description
*
* @api
* @param string $sName Name of the parameter
* @param string $sDescription Description of the parameter
* @param string $sType Type of the parameter
*/
public function __construct(string $sName, string $sDescription, string $sType)
{
$this->sName = $sName;
$this->sDescription = $sDescription;
$this->sType = $sType;
}
/**
* @return string
*/
public function GetName(): string
{
return $this->sName;
}
/**
* @param string $sName
*/
public function SetName(string $sName): void
{
$this->sName = $sName;
}
/**
* @return string
*/
public function GetDescription(): string
{
return $this->sDescription;
}
/**
* @param string $sDescription
*/
public function SetDescription(string $sDescription): void
{
$this->sDescription = $sDescription;
}
/**
* @return string
*/
public function GetType(): string
{
return $this->sType;
}
/**
* @param string $sType
*/
public function SetType(string $sType): void
{
$this->sType = $sType;
}
}

View File

@@ -0,0 +1,142 @@
<?php
/*
* @copyright Copyright (C) 2010-2022 Combodo SARL
* @license http://opensource.org/licenses/AGPL-3.0
*/
namespace Combodo\iTop\Service\Description;
/**
* Description of an event when registering
*
* @api
* @since 3.1.0
*/
class EventDescription
{
private string $sEventName;
/** @var string[]|string|null */
private $mEventSources;
private string $sDescription;
private string $sReplaces;
/** @var \Combodo\iTop\Service\Description\EventDataDescription[] */
private array $aEventDataDescription;
private string $sModule;
/**
* Create a description for an event
*
* @api
* @param string $sEventName Name of the described event
* @param string|string[]|null $mEventSources Source(s) for this event (can be the root class for CRUD events)
* @param string $sDescription Description of the event
* @param string $sReplaces In case this event obsoletes previous extensibility functions
* @param \Combodo\iTop\Service\Description\EventDataDescription[] $aEventDataDescription Description of the data associated with this event
* @param string $sModule iTop Module name where the event is defined
*/
public function __construct(string $sEventName, $mEventSources, string $sDescription, string $sReplaces, array $aEventDataDescription, string $sModule)
{
$this->sEventName = $sEventName;
$this->mEventSources = $mEventSources;
$this->sDescription = $sDescription;
$this->sReplaces = $sReplaces;
$this->aEventDataDescription = $aEventDataDescription;
$this->sModule = $sModule;
}
/**
* @return string
*/
public function GetEventName(): string
{
return $this->sEventName;
}
/**
* @param string $sEventName
*/
public function SetEventName(string $sEventName): void
{
$this->sEventName = $sEventName;
}
/**
* @return string
*/
public function GetDescription(): string
{
return $this->sDescription;
}
/**
* @param string $sDescription
*/
public function SetDescription(string $sDescription): void
{
$this->sDescription = $sDescription;
}
/**
* @return string
*/
public function GetReplaces(): string
{
return $this->sReplaces;
}
/**
* @param string $sReplaces
*/
public function SetReplaces(string $sReplaces): void
{
$this->sReplaces = $sReplaces;
}
/**
* @return array
*/
public function GetEventDataDescription(): array
{
return $this->aEventDataDescription;
}
/**
* @param \Combodo\iTop\Service\Description\EventDataDescription[]s $aEventDataDescription
*/
public function SetEventDataDescription(array $aEventDataDescription): void
{
$this->aEventDataDescription = $aEventDataDescription;
}
/**
* @return string
*/
public function GetModule(): string
{
return $this->sModule;
}
/**
* @param string $sModule
*/
public function SetModule(string $sModule): void
{
$this->sModule = $sModule;
}
/**
* @return string|string[]|null
*/
public function GetEventSources()
{
return $this->mEventSources;
}
/**
* @param string|string[]|null $mEventSources
*/
public function SetEventSources($mEventSources): void
{
$this->mEventSources = $mEventSources;
}
}

View File

@@ -7,6 +7,7 @@
namespace Combodo\iTop\Service;
use Closure;
use Combodo\iTop\Service\Description\EventDescription;
use ContextTag;
use CoreException;
use Exception;
@@ -281,24 +282,25 @@ class EventService
* This step is mandatory before firing an event.
*
* @api
* @param string $sEvent
* @param array $aDescription
* @param string $sModule
* @param \Combodo\iTop\Service\Description\EventDescription $oEventDescription
*
* @return void
*/
public static function RegisterEvent(string $sEvent, array $aDescription, string $sModule)
public static function RegisterEvent(EventDescription $oEventDescription)
{
$sEvent = $oEventDescription->GetEventName();
$sModule = $oEventDescription->GetModule();
if (self::IsEventRegistered($sEvent)) {
$sPrevious = self::$aEventDescription[$sEvent]['module'];
EventHelper::Warning("The Event $sEvent defined by $sModule has already been defined in $sPrevious, check your delta");
return;
}
self::$aEventDescription[$sEvent] = [
'name'=> $sEvent,
'description' => $aDescription,
'module' => $sModule,
'name' => $sEvent,
'description' => $oEventDescription,
'module' => $sModule,
];
}
@@ -307,8 +309,8 @@ class EventService
$aRes = [];
$oClass = new ReflectionClass($sClass);
foreach (self::$aEventDescription as $sEvent => $aEventInfo) {
if (isset($aEventInfo['description']['sources'])) {
foreach ($aEventInfo['description']['sources'] as $sSource) {
if (is_array($aEventInfo['description']->GetSources())) {
foreach ($aEventInfo['description']->GetSources() as $sSource) {
if ($sClass == $sSource || $oClass->isSubclassOf($sSource)) {
$aRes[$sEvent] = $aEventInfo;
}