Skip to content

Commit 1cb9ccf

Browse files
committed
feat: add trait HasPrePostActions
1 parent 7e44063 commit 1cb9ccf

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
44

55
The format is based on [Keep a Changelog][keepachangelog] and this project adheres to [Semantic Versioning][semver].
66

7+
## v4.5.0
8+
9+
### Added
10+
11+
- Add trait `HasPrePostActions`
12+
713
## v4.4.2
814

915
### Changed

src/Traits/HasPrePostActions.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Php\Support\Traits;
6+
7+
trait HasPrePostActions
8+
{
9+
/** @var array<string,array<callable>> */
10+
protected array $executeCallbacks = [];
11+
12+
public function addCallbackAction(string $key, callable $action): self
13+
{
14+
$this->executeCallbacks[$key][] = $action;
15+
16+
return $this;
17+
}
18+
19+
public function getCallbackActions(string $key): array
20+
{
21+
return (array)($this->executeCallbacks[$key] ?? []);
22+
}
23+
24+
protected function runActions(string $actionGroup, ...$arguments): bool
25+
{
26+
foreach ($this->getCallbackActions($actionGroup) as $action) {
27+
$res = $action(...$arguments);
28+
if ($res === false) {
29+
return false;
30+
}
31+
}
32+
33+
return true;
34+
}
35+
}

0 commit comments

Comments
 (0)