Skip to content

Commit

Permalink
Extended Events handlig to allow onRender[]
Browse files Browse the repository at this point in the history
  • Loading branch information
juniwalk authored May 6, 2024
1 parent df855ed commit 2546040
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/Interfaces/EventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
interface EventHandler
{
public function when(string $event, callable $callback, ?int $priority = null): void;
public function isWatched(string $event): bool;
public function isWatched(string $event, bool $throw = false): bool;
}
61 changes: 44 additions & 17 deletions src/Traits/Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,59 @@

namespace JuniWalk\Utils\Traits;

use JuniWalk\Utils\Interfaces\EventHandler;
use JuniWalk\Utils\Format;
use Nette\InvalidStateException;
use UnexpectedValueException;

/**
* @phpstan-require-implements EventHandler
*/
trait Events
{
/** @var array<string, callable> */
private array $events = [];


/**
* @param callable(): void $callback
* @throws UnexpectedValueException
*/
public function &__get(string $name): array
{
if (!str_starts_with($name, 'on')) {
throw new UnexpectedValueException('Event name should use format on[EventName], '.$name.' given.');
}

$event = Format::kebabCase($name);
$event = substr($event, 3);

$this->isWatched($event, true);
return $this->events[$event];
}


/**
* @throws InvalidStateException
*/
public function when(string $event, callable $callback, ?int $priority = null): void
public function isWatched(string $event, bool $throw = false): bool
{
$priority ??= sizeof($this->events[$event] ?? []);
$event = Format::kebabCase($event);

if (!$this->isWatched($event)) {
throw new InvalidStateException('Event "'.$event.'" is not watched. Call '.static::class.'::watch($event) method.');
if ($throw && !$isWatched = isset($this->events[$event])) {
throw new InvalidStateException('Event "'.$event.'" is not being watched.');
}

return $isWatched;
}


public function when(string $event, callable $callback, ?int $priority = null): void
{
$event = Format::kebabCase($event);
$this->isWatched($event, true);

$priority ??= sizeof($this->events[$event] ?? []);

array_splice(
$this->events[$event],
$priority, 0,
Expand All @@ -35,17 +68,13 @@ public function when(string $event, callable $callback, ?int $priority = null):
}


public function isWatched(string $event): bool
{
return isset($this->events[$event]);
}


/**
* @throws InvalidStateException
*/
protected function watch(string $event, bool $clear = false): static
{
$event = Format::kebabCase($event);

if (!$clear && $this->isWatched($event)) {
throw new InvalidStateException('Event "'.$event.'" is already watched. Use $clear to re-register.');
}
Expand All @@ -60,9 +89,8 @@ protected function watch(string $event, bool $clear = false): static
*/
protected function unwatch(string $event): static
{
if (!$this->isWatched($event)) {
throw new InvalidStateException('Event "'.$event.'" is not watched.');
}
$event = Format::kebabCase($event);
$this->isWatched($event, true);

unset($this->events[$event]);
return $this;
Expand All @@ -74,9 +102,8 @@ protected function unwatch(string $event): static
*/
protected function trigger(string $event, mixed ...$args): void
{
if (!$this->isWatched($event)) {
throw new InvalidStateException('Event "'.$event.'" is not watched. Call '.static::class.'::listen($event) method.');
}
$event = Format::kebabCase($event);
$this->isWatched($event, true);

ksort($this->events[$event], SORT_NUMERIC);

Expand Down

0 comments on commit 2546040

Please sign in to comment.