|
1
|
|
|
<?php namespace Anomaly\Streams\Platform\Traits; |
|
2
|
|
|
|
|
3
|
|
|
use Illuminate\Contracts\Bus\SelfHandling; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class Hookable |
|
7
|
|
|
* |
|
8
|
|
|
* @link http://anomaly.is/streams-platform |
|
9
|
|
|
* @author AnomalyLabs, Inc. <[email protected]> |
|
10
|
|
|
* @author Ryan Thompson <[email protected]> |
|
11
|
|
|
* @package Anomaly\Streams\Platform\Traits |
|
12
|
|
|
*/ |
|
13
|
|
|
trait Hookable |
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* The registered hooks. |
|
18
|
|
|
* |
|
19
|
|
|
* @var array |
|
20
|
|
|
*/ |
|
21
|
|
|
protected static $hooks = []; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Register a new hook. |
|
25
|
|
|
* |
|
26
|
|
|
* @param $hook |
|
27
|
|
|
* @param $callback |
|
28
|
|
|
* @param bool $bind |
|
29
|
|
|
* @return $this |
|
30
|
|
|
*/ |
|
31
|
|
|
public function hook($hook, $callback, $bind = false) |
|
32
|
|
|
{ |
|
33
|
|
|
$owner = get_class($this); |
|
34
|
|
|
|
|
35
|
|
|
self::$hooks[$hook][] = compact('owner', 'callback', 'bind'); |
|
36
|
|
|
|
|
37
|
|
|
return $this; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Bind a new hook. This is a shortcut |
|
42
|
|
|
* for hooks with the bind option. It's |
|
43
|
|
|
* more descriptive for IDE hinting. |
|
44
|
|
|
* |
|
45
|
|
|
* @param $hook |
|
46
|
|
|
* @param $callback |
|
47
|
|
|
* @return $this |
|
48
|
|
|
*/ |
|
49
|
|
|
public function bind($hook, $callback) |
|
50
|
|
|
{ |
|
51
|
|
|
return $this->hook($hook, $callback, true); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Call a hook. |
|
56
|
|
|
* |
|
57
|
|
|
* @param $hook |
|
58
|
|
|
* @param array $parameters |
|
59
|
|
|
* @return mixed |
|
60
|
|
|
*/ |
|
61
|
|
|
public function call($hook, array $parameters = []) |
|
62
|
|
|
{ |
|
63
|
|
|
if (!$hook = $this->getHook($hook)) { |
|
64
|
|
|
throw new \Exception('The hook [' . $hook . '] does not exist for [' . get_class($this) . '].'); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
if ($hook['bind']) { |
|
68
|
|
|
$hook['callback'] = \Closure::bind($hook['callback'], $this); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return app()->call($hook['callback'], $parameters); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Return if the hook exists. |
|
76
|
|
|
* |
|
77
|
|
|
* @param $hook |
|
78
|
|
|
* @return bool |
|
79
|
|
|
*/ |
|
80
|
|
|
public function hasHook($hook) |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->getHook($hook) !== null; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Get a hook. |
|
87
|
|
|
* |
|
88
|
|
|
* @param $hook |
|
89
|
|
|
* @return bool |
|
90
|
|
|
*/ |
|
91
|
|
|
public function getHook($hook) |
|
92
|
|
|
{ |
|
93
|
|
|
if (!isset(self::$hooks[$hook])) { |
|
94
|
|
|
return null; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
foreach (self::$hooks[$hook] as $hook) { |
|
98
|
|
|
if ($this instanceof $hook['owner']) { |
|
99
|
|
|
return $hook; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return null; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|