| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- <?php
- namespace App\Module\Notification\Services;
- use App\Module\Notification\Contracts\NotificationHookInterface;
- use Illuminate\Support\Collection;
- class HookManager
- {
- protected Collection $hooks;
- public function __construct()
- {
- $this->hooks = collect();
- }
- /**
- * 注册Hook
- *
- * @param NotificationHookInterface $hook
- * @return void
- */
- public function register(NotificationHookInterface $hook): void
- {
- $this->hooks->push($hook);
- }
- /**
- * 执行发送前钩子
- *
- * @param array $data
- * @return array
- */
- public function executeBeforeSend(array $data): array
- {
- foreach ($this->hooks as $hook) {
- $data = $hook->beforeSend($data);
- }
- return $data;
- }
- /**
- * 执行发送后钩子
- *
- * @param array $data
- * @param bool $result
- * @return void
- */
- public function executeAfterSend(array $data, bool $result): void
- {
- foreach ($this->hooks as $hook) {
- $hook->afterSend($data, $result);
- }
- }
- /**
- * 执行错误钩子
- *
- * @param array $data
- * @param \Exception $exception
- * @return void
- */
- public function executeOnError(array $data, \Exception $exception): void
- {
- foreach ($this->hooks as $hook) {
- $hook->onError($data, $exception);
- }
- }
- }
|