-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Introduce the event object, update to hooks * Bugfix and two additional filters * Small content update * Enhancement, fixed tests, wrote more hook tests * Modified how we track event completeness * Make PHPStan happy
- Loading branch information
1 parent
829aa72
commit 1e9e596
Showing
12 changed files
with
230 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,4 @@ src/php_error.log | |
/reports | ||
/src/Themes/Default/Assets/build | ||
/tmp | ||
/src/Plugins/Example |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
<?php | ||
|
||
namespace AntCMS; | ||
|
||
use DateTime; | ||
|
||
class Event | ||
{ | ||
private readonly string $associatedHook; | ||
private readonly DateTime $firedAt; | ||
private readonly float|int $hrStart; | ||
private int|float $timeElapsed; | ||
|
||
private int $paramUpdateCount = 0; | ||
private int $paramReadCount = 0; | ||
private int $lastCallback = 0; | ||
|
||
/** | ||
* @param string $associatedHook The hook that this event is associated with. Hook must exist. | ||
* | ||
* @param mixed[] $parameters | ||
*/ | ||
public function __construct(string $associatedHook, private array $parameters, private readonly int $totalCallbacks) | ||
{ | ||
if (!HookController::isRegistered($associatedHook)) { | ||
throw new \Exception("Hook $associatedHook is not registered!"); | ||
} | ||
|
||
$this->associatedHook = $associatedHook; | ||
$this->firedAt = new DateTime(); | ||
$this->hrStart = hrtime(true); | ||
} | ||
|
||
/** | ||
* Indicates if the hook has completed | ||
*/ | ||
public function isDone(): bool | ||
{ | ||
return $this->lastCallback === $this->totalCallbacks; | ||
} | ||
|
||
/** | ||
* Called by the hook after each callback is complete. | ||
* Tracks if the event is completed & fires 'onHookFireComplete' when needed. | ||
* | ||
* Callbacks should not call this function. | ||
* | ||
* @return Event | ||
*/ | ||
public function next(): Event | ||
{ | ||
if (!$this->isDone()) { | ||
// We just completed a callback, increment the last callback number. | ||
++$this->lastCallback; | ||
|
||
// Check if that was the last callback & we are done | ||
if ($this->isDone()) { | ||
// Set the timing | ||
$this->timeElapsed = hrtime(true) - $this->hrStart; | ||
|
||
// Fire `onHookFireComplete` | ||
if ($this->associatedHook !== 'onHookFireComplete') { | ||
HookController::fire('onHookFireComplete', [ | ||
'name' => $this->associatedHook, | ||
'firedAt' => $this->firedAt, | ||
'timeElapsed' => $this->timeElapsed, | ||
'parameterReadCount' => $this->paramReadCount, | ||
'parameterUpdateCount' => $this->paramUpdateCount, | ||
]); | ||
} | ||
} | ||
} else { | ||
// We shouldn't run into this situation, but it's non-fatal so only trigger a warning. | ||
trigger_error("The 'next' event was called too many times for the '$this->associatedHook' event. Event timing may be inaccurate and 'onHookFireComplete' would have been fired too soon.", E_USER_WARNING); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Indicates when the event was originally fired at | ||
*/ | ||
public function firedAt(): DateTime | ||
{ | ||
return $this->firedAt; | ||
} | ||
|
||
/** | ||
* Returns the total time spent for this event in nanoseconds | ||
*/ | ||
public function timeElapsed(): int|float | ||
{ | ||
return $this->timeElapsed; | ||
} | ||
|
||
/** | ||
* Gets the associated hook name | ||
*/ | ||
public function getHookName(): string | ||
{ | ||
return $this->associatedHook; | ||
} | ||
|
||
/** | ||
* Gets the event parameters | ||
* | ||
* @return mixed[] | ||
*/ | ||
public function getParameters(): array | ||
{ | ||
$this->paramReadCount++; | ||
return $this->parameters; | ||
} | ||
|
||
/** | ||
* Updates the parameters | ||
* | ||
* @param mixed[] $parameters | ||
* | ||
* @return Event | ||
*/ | ||
public function setParameters(array $parameters): Event | ||
{ | ||
$this->parameters = $parameters; | ||
$this->paramUpdateCount++; | ||
return $this; | ||
} | ||
|
||
/** | ||
* Returns the number of times the event parameters were read from | ||
*/ | ||
public function getReadCount(): int | ||
{ | ||
return $this->paramReadCount; | ||
} | ||
|
||
/** | ||
* Returns the number of times the event parameters were updated. | ||
*/ | ||
public function getUpdateCount(): int | ||
{ | ||
return $this->paramUpdateCount; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
// Load the standard AntCMS Bootstrap file | ||
require_once dirname(__DIR__) . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'bootstrap.php'; | ||
|
||
// Register plugins so hook tests can function correctly | ||
AntCMS\PluginController::init(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters