Skip to content

Commit

Permalink
Fluent hook calls
Browse files Browse the repository at this point in the history
  • Loading branch information
inxilpro committed Jan 21, 2024
1 parent 85c1928 commit 306585e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ format. This project adheres to [Semantic Versioning](https://semver.org/spec/v2

## [Unreleased]

### Added

- Made hook calls fluent

## [0.0.2] - 2024-01-21

### Changed
Expand Down
12 changes: 9 additions & 3 deletions src/Hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,31 @@ public function __construct(
) {
}

public function __call(string $name, array $arguments)
public function __call(string $name, array $arguments): static
{
$hook = $arguments[0];
$priority = $arguments[1] ?? Hook::DEFAULT_PRIORITY;

$this->on($name, $hook, $priority);

return $this;
}

public function default(Closure|Hook $hook, int $priority = Hook::DEFAULT_PRIORITY)
public function default(Closure|Hook $hook, int $priority = Hook::DEFAULT_PRIORITY): static
{
$this->on(static::DEFAULT, $hook, $priority);

return $this;
}

public function on(string $name, Closure|Hook $hook, int $priority = Hook::DEFAULT_PRIORITY)
public function on(string $name, Closure|Hook $hook, int $priority = Hook::DEFAULT_PRIORITY): static
{
if ($hook instanceof Closure) {
$hook = new Hook($hook, $priority);
}

$this->registry->register($hook, $this->target, $name);

return $this;
}
}

0 comments on commit 306585e

Please sign in to comment.