Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 34 additions & 11 deletions src/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,14 @@ class Database
'*' => [],
];

protected bool $silentEvents = false;
/**
* Array in which the keys are the names of databse listeners that
* should be skipped when dispatching events. null $silentListeners
* will skip all listeners.
*
* @var ?array<string, bool>
*/
protected ?array $silentListeners = [];

protected ?\DateTime $timestamp = null;

Expand Down Expand Up @@ -372,31 +379,41 @@ function (?string $value) {
* @param callable $callback
* @return self
*/
public function on(string $event, callable $callback): self
public function on(string $event, string $name, callable $callback): self
{
if (!isset($this->listeners[$event])) {
$this->listeners[$event] = [];
}
$this->listeners[$event][] = $callback;
$this->listeners[$event][$name] = $callback;
return $this;
}

/**
* Silent event generation for all the calls inside the callback
* Silent event generation for calls inside the callback
*
* @template T
* @param callable(): T $callback
* @param array<string>|null $listeners List of listeners to silence; if null, all listeners will be silenced
* @return T
*/
public function silent(callable $callback): mixed
public function silent(callable $callback, array $listeners = null): mixed
{
$previous = $this->silentEvents;
$this->silentEvents = true;
$previous = $this->silentListeners;

if (is_null($listeners)) {
$this->silentListeners = null;
} else {
$silentListeners = [];
foreach ($listeners as $listener) {
$silentListeners[$listener] = true;
}
$this->silentListeners = $silentListeners;
}

try {
return $callback();
} finally {
$this->silentEvents = $previous;
$this->silentListeners = $previous;
}
}

Expand Down Expand Up @@ -428,14 +445,20 @@ public function skipRelationships(callable $callback): mixed
*/
protected function trigger(string $event, mixed $args = null): void
{
if ($this->silentEvents) {
if (\is_null($this->silentListeners)) {
return;
}
foreach ($this->listeners[self::EVENT_ALL] as $callback) {
foreach ($this->listeners[self::EVENT_ALL] as $name => $callback) {
if (isset($this->silentListeners[$name])) {
continue;
}
call_user_func($callback, $event, $args);
}

foreach (($this->listeners[$event] ?? []) as $callback) {
foreach (($this->listeners[$event] ?? []) as $name => $callback) {
if (isset($this->silentListeners[$name])) {
continue;
}
call_user_func($callback, $event, $args);
}
}
Expand Down
27 changes: 18 additions & 9 deletions tests/Database/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -11225,7 +11225,7 @@ public function testEvents(): void
Database::EVENT_DATABASE_DELETE,
];

$database->on(Database::EVENT_ALL, function ($event, $data) use (&$events) {
$database->on(Database::EVENT_ALL, 'test', function ($event, $data) use (&$events) {
$shifted = array_shift($events);

$this->assertEquals($shifted, $event);
Expand Down Expand Up @@ -11261,14 +11261,23 @@ public function testEvents(): void
],
]));

$database->updateDocument($collectionId, 'doc1', $document->setAttribute('attr1', 15));
$database->getDocument($collectionId, 'doc1');
$database->find($collectionId);
$database->findOne($collectionId);
$database->count($collectionId);
$database->sum($collectionId, 'attr1');
$database->increaseDocumentAttribute($collectionId, $document->getId(), 'attr1');
$database->decreaseDocumentAttribute($collectionId, $document->getId(), 'attr1');
$executed = false;
$database->on(Database::EVENT_ALL, 'should-not-execute', function ($event, $data) use (&$executed) {
$executed = true;
});

$database->silent(function () use ($database, $collectionId, $document) {
$database->updateDocument($collectionId, 'doc1', $document->setAttribute('attr1', 15));
$database->getDocument($collectionId, 'doc1');
$database->find($collectionId);
$database->findOne($collectionId);
$database->count($collectionId);
$database->sum($collectionId, 'attr1');
$database->increaseDocumentAttribute($collectionId, $document->getId(), 'attr1');
$database->decreaseDocumentAttribute($collectionId, $document->getId(), 'attr1');
}, ['should-not-execute']);

$this->assertFalse($executed);

$database->deleteIndex($collectionId, $indexId1);
$database->deleteDocument($collectionId, 'doc1');
Expand Down