Merge pull request #22808 from colemanw/searchKitMailingTask
[civicrm-core.git] / Civi / Core / Event / HookStyleListener.php
CommitLineData
b90e57be
TO
1<?php
2
3namespace Civi\Core\Event;
4
5/**
6 * This is an adapter which allows you to attach hook-style functions directly to the dispatcher.
7 * Example:
8 *
9 * ```php
10 * function listen_to_hook_foo($arg1, &$arg2, $arg3) { ... }
11 * Civi::dispatcher()->addListener('hook_civicrm_foo', new HookStyleListener('listen_to_hook_foo'));
12 * ```
13 *
14 * @package Civi\Core\Event
15 */
16class HookStyleListener {
17
18 /**
19 * @var array
20 * Ex: ['SomeClass', 'someMethod']
21 */
22 private $callback = NULL;
23
24 /**
25 * @param array $callback
26 * Ex: ['SomeClass', 'someMethod']
27 */
28 public function __construct($callback) {
29 $this->callback = $callback;
30 }
31
32 public function __invoke(GenericHookEvent $e) {
65bc6543
TO
33 $result = call_user_func_array($this->callback, $e->getHookValues());
34 $e->addReturnValues($result);
b90e57be
TO
35 }
36
65bc6543 37 public function __toString(): string {
b90e57be
TO
38 $name = EventPrinter::formatName($this->callback);
39 return preg_replace('/\(\$?e?\)$/', '(&...)', $name);
40 }
41
42}