Skip to content

Commit

Permalink
feat(events): event sequences have a unique ID
Browse files Browse the repository at this point in the history
  • Loading branch information
jeabakker committed Nov 25, 2022
1 parent 614ea84 commit 0febd63
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
9 changes: 9 additions & 0 deletions engine/classes/Elgg/Event.php
Expand Up @@ -160,4 +160,13 @@ public function getUserParam(): ?\ElggUser {
public function elgg(): PublicContainer {
return $this->dic;
}

/**
* When the event is part of a sequence a unique ID is set for each sequence
*
* @return string|null
*/
public function getSequenceID(): ?string {
return elgg_extract('_elgg_sequence_id', $this->params);
}
}
18 changes: 17 additions & 1 deletion engine/classes/Elgg/EventsService.php
Expand Up @@ -87,7 +87,15 @@ public function trigger(string $name, string $type, $object = null, array $optio
$this->beginTimer(["[{$name},{$type}]", $handler_description]);
}

list($success, $return, $event) = $this->handlers->call($handler, $event, [$name, $type, null, ['object' => $object]]);
list($success, $return, $event) = $this->handlers->call($handler, $event, [
$name,
$type,
null,
[
'object' => $object,
'_elgg_sequence_id' => elgg_extract('_elgg_sequence_id', $options),
],
]);

if ($handler_description) {
$this->endTimer(["[{$name},{$type}]", $handler_description]);
Expand Down Expand Up @@ -202,6 +210,9 @@ public function triggerAfter(string $name, string $type, $object = null, array $
* @return bool
*/
public function triggerSequence(string $name, string $type, $object = null, callable $callable = null, array $options = []): bool {
// generate a unique ID to identify this sequence
$options['_elgg_sequence_id'] = uniqid("{$name}{$type}", true);

if (!$this->triggerBefore($name, $type, $object, $options)) {
return false;
}
Expand Down Expand Up @@ -235,6 +246,11 @@ public function triggerSequence(string $name, string $type, $object = null, call
* @return mixed
*/
public function triggerResultsSequence(string $name, string $type, array $params = [], $value = null, callable $callable = null, array $options = []) {
// generate a unique ID to identify this sequence
$unique_id = uniqid("{$name}{$type}results", true);
$options['_elgg_sequence_id'] = $unique_id;
$params['_elgg_sequence_id'] = $unique_id;

if (!$this->triggerBefore($name, $type, $params, $options)) {
return false;
}
Expand Down
38 changes: 38 additions & 0 deletions engine/tests/phpunit/unit/Elgg/EventsServiceUnitTest.php
Expand Up @@ -56,6 +56,44 @@ public function testUnstoppableEventsCantBeStoppedAndReturnTrue() {
)));
$this->assertEquals($this->counter, 1);
}

public function testIndividualTriggersHaveNoSquenceID() {
$calls = 0;
$this->events->registerHandler('all', 'non_sequence', function(\Elgg\Event $event) use (&$calls) {
$calls++;
$this->assertNull($event->getSequenceID());
});

$this->events->triggerBefore('foo', 'non_sequence');
$this->events->trigger('foo', 'non_sequence');
$this->events->triggerAfter('foo', 'non_sequence');
$this->events->triggerDeprecated('foo:deprecated', 'non_sequence');

$this->events->triggerResults('results', 'non_sequence');
$this->events->triggerDeprecatedResults('results:deprecated', 'non_sequence');

$this->assertEquals(6, $calls);
}

public function testTriggerSequenceHasSquenceID() {
$calls = 0;
$sequence_ids = [];
$this->events->registerHandler('all', 'sequence', function(\Elgg\Event $event) use (&$calls, &$sequence_ids) {
$calls++;
$this->assertNotNull($event->getSequenceID());
$sequence_ids[] = $event->getSequenceID();
});

$this->events->triggerSequence('foo', 'sequence');
$this->events->triggerSequence('foo', 'sequence'); // this should give a new unique sequence ID
$this->events->triggerResultsSequence('results', 'sequence');
$this->events->triggerResultsSequence('results', 'sequence'); // this should give a new unique sequence ID

$sequence_ids = array_unique($sequence_ids);

$this->assertEquals(12, $calls);
$this->assertCount(4, $sequence_ids);
}

public function testUncallableHandlersAreLogged() {
$this->events->registerHandler('foo', 'bar', array(new \stdClass(), 'uncallableMethod'));
Expand Down

0 comments on commit 0febd63

Please sign in to comment.