1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Phossa Project |
4
|
|
|
* |
5
|
|
|
* PHP version 5.4 |
6
|
|
|
* |
7
|
|
|
* @category Library |
8
|
|
|
* @package Phossa2\Event |
9
|
|
|
* @copyright Copyright (c) 2016 phossa.com |
10
|
|
|
* @license http://mit-license.org/ MIT License |
11
|
|
|
* @link http://www.phossa.com/ |
12
|
|
|
*/ |
13
|
|
|
/*# declare(strict_types=1); */ |
14
|
|
|
|
15
|
|
|
namespace Phossa2\Event\Traits; |
16
|
|
|
|
17
|
|
|
use Phossa2\Event\Interfaces\EventInterface; |
18
|
|
|
use Phossa2\Event\Interfaces\CountableInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* CountableTrait |
22
|
|
|
* |
23
|
|
|
* Implmentation of CountableInterface |
24
|
|
|
* |
25
|
|
|
* @package Phossa2\Event |
26
|
|
|
* @author Hong Zhang <[email protected]> |
27
|
|
|
* @see CountableInterface |
28
|
|
|
* @version 2.0.0 |
29
|
|
|
* @since 2.0.0 added |
30
|
|
|
*/ |
31
|
|
|
trait CountableTrait |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* callable mapping |
35
|
|
|
* |
36
|
|
|
* @var callable[]; |
37
|
|
|
* @access protected |
38
|
|
|
*/ |
39
|
|
|
protected $callable_map = []; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritDoc} |
43
|
|
|
*/ |
44
|
|
|
public function many( |
45
|
|
|
/*# int */ $times, |
46
|
|
|
/*# string */ $eventName, |
47
|
|
|
callable $callable, |
48
|
|
|
/*# int */ $priority = 50 |
49
|
|
|
) { |
50
|
|
|
// wrap the callable |
51
|
|
|
$wrapper = function (EventInterface $event) use ($callable, $times) { |
52
|
|
|
static $cnt = 0; |
53
|
|
|
if ($cnt++ < $times) { |
54
|
|
|
call_user_func($callable, $event); |
55
|
|
|
} |
56
|
|
|
}; |
57
|
|
|
|
58
|
|
|
// mapping callable |
59
|
|
|
$oid = $this->hashCallable($callable); |
60
|
|
|
$this->callable_map[$eventName][$oid] = $wrapper; |
61
|
|
|
|
62
|
|
|
// bind wrapper instead of the $callable |
63
|
|
|
$this->on($eventName, $wrapper, $priority); |
|
|
|
|
64
|
|
|
|
65
|
|
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritDoc} |
70
|
|
|
*/ |
71
|
|
|
public function one( |
72
|
|
|
/*# string */ $eventName, |
73
|
|
|
callable $callable, |
74
|
|
|
/*# int */ $priority = 50 |
75
|
|
|
) { |
76
|
|
|
return $this->many(1, $eventName, $callable, $priority); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Override `off()` in EventManager |
81
|
|
|
* |
82
|
|
|
* Added support for countable callable |
83
|
|
|
* |
84
|
|
|
* {@inheritDoc} |
85
|
|
|
*/ |
86
|
|
|
public function off( |
87
|
|
|
/*# string */ $eventName = '', |
88
|
|
|
callable $callable = null |
89
|
|
|
) { |
90
|
|
|
if (null !== $callable) { |
91
|
|
|
$oid = $this->hashCallable($callable); |
92
|
|
|
if (isset($this->callable_map[$eventName][$oid])) { |
93
|
|
|
$callable = $this->callable_map[$eventName][$oid]; |
94
|
|
|
unset($this->callable_map[$eventName][$oid]); |
95
|
|
|
} |
96
|
|
|
} else { |
97
|
|
|
unset($this->callable_map[$eventName]); |
98
|
|
|
} |
99
|
|
|
return parent::off($eventName, $callable); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Returns a unique id for $callable with $eventName |
104
|
|
|
* |
105
|
|
|
* @param callable $callable |
106
|
|
|
* @return string |
107
|
|
|
* @access protected |
108
|
|
|
*/ |
109
|
|
|
protected function hashCallable(callable $callable)/*# string */ |
110
|
|
|
{ |
111
|
|
|
return spl_object_hash((object) $callable); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.