1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Rocketeer |
5
|
|
|
* |
6
|
|
|
* (c) Maxime Fabre <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
* |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Rocketeer\Services\Events; |
14
|
|
|
|
15
|
|
|
use League\Event\Emitter; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Assigns tags to listeners to allow |
19
|
|
|
* batch operations on subsets of them. |
20
|
|
|
*/ |
21
|
|
|
class TaggableEmitter extends Emitter |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* The currently active tag. |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $tag = '*'; |
29
|
|
|
|
30
|
|
|
//////////////////////////////////////////////////////////////////////////////// |
31
|
|
|
//////////////////////////////////// TAGS ////////////////////////////////////// |
32
|
|
|
//////////////////////////////////////////////////////////////////////////////// |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @return string |
36
|
|
|
*/ |
37
|
1 |
|
public function getTag() |
38
|
|
|
{ |
39
|
1 |
|
return $this->tag; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string $tag |
44
|
|
|
*/ |
45
|
3 |
|
public function setTag($tag) |
46
|
|
|
{ |
47
|
3 |
|
$this->tag = $tag; |
48
|
3 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param string $tags |
52
|
|
|
* @param callable $callable |
53
|
|
|
*/ |
54
|
2 |
|
public function onTag($tags, callable $callable) |
55
|
|
|
{ |
56
|
2 |
|
$previous = $this->tag; |
57
|
2 |
|
$this->setTag($tags); |
58
|
2 |
|
$callable(); |
59
|
2 |
|
$this->setTag($previous); |
60
|
2 |
|
} |
61
|
|
|
|
62
|
|
|
//////////////////////////////////////////////////////////////////////////////// |
63
|
|
|
////////////////////////////////// OVERRIDES /////////////////////////////////// |
64
|
|
|
//////////////////////////////////////////////////////////////////////////////// |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
3 |
|
protected function getSortedListeners($event) |
70
|
|
|
{ |
71
|
3 |
|
$listeners = parent::getSortedListeners($event); |
72
|
3 |
|
$listeners = array_filter($listeners, function (TaggedListener $listener) { |
73
|
3 |
|
return $listener->getTag() === $this->tag || $listener->getTag() === '*'; |
74
|
3 |
|
}); |
75
|
|
|
|
76
|
3 |
|
return $listeners; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@inheritdoc} |
81
|
|
|
*/ |
82
|
3 |
|
protected function ensureListener($listener) |
83
|
|
|
{ |
84
|
3 |
|
$listener = parent::ensureListener($listener); |
85
|
3 |
|
$listener = new TaggedListener($listener); |
86
|
3 |
|
$listener->setTag($this->tag); |
87
|
|
|
|
88
|
3 |
|
return $listener; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|