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\Interfaces; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Matching event name |
19
|
|
|
* |
20
|
|
|
* Returns matched names with $eventName. e.g. |
21
|
|
|
* |
22
|
|
|
* ```php |
23
|
|
|
* $names = ['*', 'u*.*', 'user.*', 'blog.*']; |
24
|
|
|
* |
25
|
|
|
* // returns ['*', 'u*.*', 'user.*'] |
26
|
|
|
* $matched = $this->globEventNames('user.login', $names); |
27
|
|
|
* ``` |
28
|
|
|
* |
29
|
|
|
* @package Phossa2\Event |
30
|
|
|
* @author Hong Zhang <[email protected]> |
31
|
|
|
* @version 2.0.0 |
32
|
|
|
* @since 2.0.0 added |
33
|
|
|
*/ |
34
|
|
|
trait NameGlobbingTrait |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* Returns all names matches with $eventName |
38
|
|
|
* |
39
|
|
|
* e.g. |
40
|
|
|
* 'user.login' matches '*', 'u*.*', 'user.*', 'user.l*', 'user.login' etc. |
41
|
|
|
* |
42
|
|
|
* @param string $eventName |
43
|
|
|
* @param array $names |
44
|
|
|
* @return array |
45
|
|
|
* @access protected |
46
|
|
|
*/ |
47
|
|
|
protected function globEventNames( |
48
|
|
|
/*# string */ $eventName, |
49
|
|
|
array $names |
50
|
|
|
)/*# : array */ { |
51
|
|
|
$result = []; |
52
|
|
|
foreach ($names as $name) { |
53
|
|
|
if ($this->matchEventName($name, $eventName)) { |
54
|
|
|
$result[] = $name; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
return $result; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Check to see if $name matches with $eventName |
62
|
|
|
* |
63
|
|
|
* e.g. |
64
|
|
|
* ```php |
65
|
|
|
* // true |
66
|
|
|
* $this->matchEventName('user.*', 'user.login'); |
67
|
|
|
* |
68
|
|
|
* // true |
69
|
|
|
* $this->matchEventName('*', 'user.login'); |
70
|
|
|
* |
71
|
|
|
* // false |
72
|
|
|
* $this->matchEventName('blog.*', 'user.login'); |
73
|
|
|
* ``` |
74
|
|
|
* |
75
|
|
|
* @param string $name |
76
|
|
|
* @param string $eventName |
77
|
|
|
* @return bool |
78
|
|
|
* @access protected |
79
|
|
|
*/ |
80
|
|
|
protected function matchEventName( |
81
|
|
|
/*# string */ $name, |
82
|
|
|
/*# string */ $eventName |
83
|
|
|
)/*# : bool */ { |
84
|
|
|
if ('*' === $name || $name === $eventName) { |
85
|
|
|
return true; |
86
|
|
|
} elseif (false !== strpos($name, '*')) { |
87
|
|
|
$pat = str_replace(array('.', '*'), array('[.]', '[^.]*+'), $name); |
88
|
|
|
return (bool) preg_match('~^' . $pat . '$~', $eventName); |
89
|
|
|
} else { |
90
|
|
|
return false; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|