|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Platine Event Dispatcher |
|
5
|
|
|
* |
|
6
|
|
|
* Platine Event Dispatcher is the minimal implementation of PSR 14 |
|
7
|
|
|
* |
|
8
|
|
|
* This content is released under the MIT License (MIT) |
|
9
|
|
|
* |
|
10
|
|
|
* Copyright (c) 2020 Platine Event Dispatcher |
|
11
|
|
|
* |
|
12
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
13
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
14
|
|
|
* in the Software without restriction, including without limitation the rights |
|
15
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
16
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
17
|
|
|
* furnished to do so, subject to the following conditions: |
|
18
|
|
|
* |
|
19
|
|
|
* The above copyright notice and this permission notice shall be included in all |
|
20
|
|
|
* copies or substantial portions of the Software. |
|
21
|
|
|
* |
|
22
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
23
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
24
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
25
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
26
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
27
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
28
|
|
|
* SOFTWARE. |
|
29
|
|
|
*/ |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @file CallableListener.php |
|
33
|
|
|
* |
|
34
|
|
|
* This class is used to manager listener using callable |
|
35
|
|
|
* |
|
36
|
|
|
* @package Platine\Event |
|
37
|
|
|
* @author Platine Developers Team |
|
38
|
|
|
* @copyright Copyright (c) 2020 |
|
39
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
|
40
|
|
|
* @link https://www.platine-php.com |
|
41
|
|
|
* @version 1.0.0 |
|
42
|
|
|
* @filesource |
|
43
|
|
|
*/ |
|
44
|
|
|
|
|
45
|
|
|
declare(strict_types=1); |
|
46
|
|
|
|
|
47
|
|
|
namespace Platine\Event\Listener; |
|
48
|
|
|
|
|
49
|
|
|
use Platine\Event\EventInterface; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @class CallableListener |
|
53
|
|
|
* @package Platine\Event\Listener |
|
54
|
|
|
*/ |
|
55
|
|
|
class CallableListener implements ListenerInterface |
|
56
|
|
|
{ |
|
57
|
|
|
/** |
|
58
|
|
|
* The callable |
|
59
|
|
|
* @var callable |
|
60
|
|
|
*/ |
|
61
|
|
|
protected $callable; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* The listeners |
|
65
|
|
|
* @var array<$this> |
|
66
|
|
|
*/ |
|
67
|
|
|
protected static $listeners = []; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Create new instance |
|
71
|
|
|
* @param callable $callable |
|
72
|
|
|
*/ |
|
73
|
|
|
public function __construct(callable $callable) |
|
74
|
|
|
{ |
|
75
|
|
|
$this->callable = $callable; |
|
76
|
|
|
static::$listeners[] = $this; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Return the callable |
|
81
|
|
|
* @return callable |
|
82
|
|
|
*/ |
|
83
|
|
|
public function getCallable(): callable |
|
84
|
|
|
{ |
|
85
|
|
|
return $this->callable; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Create new instance using the given callable |
|
90
|
|
|
* @param callable $callable the callable |
|
91
|
|
|
* @return self the new instance |
|
92
|
|
|
*/ |
|
93
|
|
|
public static function fromCallable(callable $callable): self |
|
94
|
|
|
{ |
|
95
|
|
|
$listener = new self($callable); |
|
96
|
|
|
|
|
97
|
|
|
return $listener; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Search the listener for given callable |
|
102
|
|
|
* @param callable $callable the callable |
|
103
|
|
|
* @return $this|false |
|
104
|
|
|
*/ |
|
105
|
|
|
public static function getListener(callable $callable): self|false |
|
106
|
|
|
{ |
|
107
|
|
|
foreach (static::$listeners as $listener) { |
|
108
|
|
|
if ($listener->getCallable() == $callable) { |
|
109
|
|
|
return $listener; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return false; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Removes all registered callable-listeners. |
|
118
|
|
|
* @return void |
|
119
|
|
|
*/ |
|
120
|
|
|
public static function clear(): void |
|
121
|
|
|
{ |
|
122
|
|
|
static::$listeners = []; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* {@inheritdoc} |
|
127
|
|
|
* |
|
128
|
|
|
* @return mixed |
|
129
|
|
|
*/ |
|
130
|
|
|
public function handle(EventInterface $event): mixed |
|
131
|
|
|
{ |
|
132
|
|
|
($this->callable)($event); |
|
133
|
|
|
|
|
134
|
|
|
return true; |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|