├── .gitignore ├── .travis.yml ├── README.md ├── README_en.md ├── composer.json ├── lib └── Pagon │ ├── Event.php │ └── EventEmitter.php ├── phpunit.xml └── tests └── EventEmitterTest.php /.gitignore: -------------------------------------------------------------------------------- 1 | vendor/ 2 | tmp/ 3 | .idea/ -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | php: 3 | - 5.3 4 | - 5.4 5 | - 5.5 6 | 7 | before_script: 8 | - wget http://getcomposer.org/composer.phar 9 | - php composer.phar install --dev --prefer-source 10 | 11 | script: 12 | - phpunit -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Other Languages:[English](./README_en.md) 2 | 3 | # 介绍 [![Build Status](https://travis-ci.org/hfcorriez/php-eventemitter.png)](https://travis-ci.org/hfcorriez/php-eventemitter) 4 | 5 | 简单的事件管理器 6 | 7 | # 安装 8 | 9 | 添加 `"pagon/eventemitter": "*"` 到 `composer.json` 10 | 11 | ``` 12 | composer.phar install 13 | ``` 14 | 15 | # 使用方式 16 | 17 | ## 触发和绑定 18 | 19 | ### 简单方式 20 | 21 | 很简单的使用方式 22 | 23 | ```php 24 | $event = new EventEmitter(); 25 | 26 | // 绑定事件 27 | $event->on('new', function () { 28 | echo 'A new client is coming' . PHP_EOL; 29 | }); 30 | 31 | // 触发 32 | $event->emit('new'); 33 | ``` 34 | 35 | ### 单次绑定 36 | 37 | 绑定的事件只会被触发一次,适合第一次做一些事情的时候去使用 38 | 39 | ```php 40 | $event = new EventEmitter(); 41 | 42 | // 绑定单次事件 43 | $event->once('new', function () { 44 | echo 'A new client is coming' . PHP_EOL; 45 | }); 46 | 47 | // 触发 48 | $event->emit('new'); 49 | 50 | // 不触发 51 | $event->emit('new'); 52 | ``` 53 | 54 | ### 多次绑定 55 | 56 | 绑定的事件被触发N次,适合一些有固定用途的事件 57 | 58 | ```php 59 | $event = new EventEmitter(); 60 | 61 | // 绑定2次事件 62 | $event->many('new', 2, function () { 63 | echo 'A new client is coming' . PHP_EOL; 64 | }); 65 | 66 | $event->emit('new'); // 触发 67 | $event->emit('new'); // 触发 68 | $event->emit('new'); // 不触发 69 | ``` 70 | 71 | ### 模糊绑定 72 | 73 | 绑定一个模糊的事件名称,可以匹配上的事件都会被触发 74 | 75 | ```php 76 | $event = new EventEmitter(); 77 | 78 | // 绑定模糊事件 79 | $event->on('news.*', function($id){ 80 | echo $id . ' is comming..., ID is '; 81 | }); 82 | 83 | $event->emit('news.1'); 84 | $event->emit('news.2'); 85 | $event->emit('news.3'); 86 | ``` 87 | 88 | ## 删除 89 | 90 | ### 注销事件 91 | 92 | 当事件不用的时候可以注销掉 93 | 94 | ```php 95 | $event = new EventEmitter(); 96 | 97 | // 闭包回调 98 | $operator = function () { 99 | echo 'A new client is coming' . PHP_EOL; 100 | }; 101 | 102 | $event->on('new', $operator); 103 | 104 | $event->emit('new'); // 触发 105 | 106 | $event->off('new', $operator); // 解除绑定 107 | $event->emit('new'); // 不触发 108 | ``` 109 | 110 | ### 删除事件 111 | 112 | 删除指定类型的所有事件 113 | 114 | ```php 115 | $event = new EventEmitter(); 116 | 117 | $event->on('new', function () { 118 | echo 'A new client is coming' . PHP_EOL; 119 | }); 120 | 121 | $event->removeAllListeners('new'); 122 | ``` 123 | 124 | ### 删除所有事件 125 | 126 | 清空事件 127 | 128 | ```php 129 | $event = new EventEmitter(); 130 | 131 | $event->on('new', function () { 132 | echo 'A new client is coming' . PHP_EOL; 133 | }); 134 | 135 | $event->on('close', function () { 136 | echo 'The client has closed' . PHP_EOL; 137 | }); 138 | 139 | $event->removeAllListeners(); 140 | ``` 141 | 142 | ### 继承事件功能 143 | 144 | 适用于为自己的对象增加事件功能 145 | 146 | ```php 147 | class MyClass extends EventEmitter { 148 | } 149 | 150 | $my = new MyClass; 151 | $my->on('create', function($data){ 152 | $db->save($data); 153 | }); 154 | ``` 155 | 156 | ## 全局管理器 157 | 158 | ### 全局事件 159 | 160 | 可以作为默认是件管理器使用 161 | 162 | ```php 163 | // 注册事件 164 | Event::on('save', function ($arg) { 165 | echo '1 saved: ' . $arg . PHP_EOL; 166 | }); 167 | 168 | // 触发事件 169 | Event::emit('save', 'test'); 170 | ``` 171 | 172 | ### 更换全局事件管理器 173 | 174 | ```php 175 | $emitter = new EventEmitter(); 176 | // 更换触发器 177 | Event::emitter($emitter); 178 | 179 | // 绑定事件 180 | $event->on('save', function ($arg) { 181 | echo '1 saved: ' . $arg . PHP_EOL; 182 | }); 183 | 184 | // 触发save 185 | $event->emit('save', 'test'); 186 | 187 | // 效果同上 188 | $emitter->emit('save', 'test'); 189 | ``` 190 | 191 | License 192 | ============= 193 | 194 | (The MIT License) 195 | 196 | Copyright (c) 2012 hfcorriez <hfcorriez@gmail.com> 197 | 198 | Permission is hereby granted, free of charge, to any person obtaining 199 | a copy of this software and associated documentation files (the 200 | 'Software'), to deal in the Software without restriction, including 201 | without limitation the rights to use, copy, modify, merge, publish, 202 | distribute, sublicense, and/or sell copies of the Software, and to 203 | permit persons to whom the Software is furnished to do so, subject to 204 | the following conditions: 205 | 206 | The above copyright notice and this permission notice shall be 207 | included in all copies or substantial portions of the Software. 208 | 209 | THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 210 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 211 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 212 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 213 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 214 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 215 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README_en.md: -------------------------------------------------------------------------------- 1 | # Intro [![Build Status](https://travis-ci.org/hfcorriez/php-eventemitter.png)](https://travis-ci.org/hfcorriez/php-eventemitter) 2 | 3 | A simple EventEmitter 4 | 5 | # Install 6 | 7 | Add `"pagon/eventemitter": "*"` to `composer.json` 8 | 9 | ``` 10 | composer.phar install 11 | ``` 12 | 13 | # Usage 14 | 15 | ## Bind and Emit 16 | 17 | ### Simple 18 | 19 | ```php 20 | $event = new EventEmitter(); 21 | 22 | // Bind event 23 | $event->on('new', function () { 24 | echo 'A new client is coming' . PHP_EOL; 25 | }); 26 | 27 | // Trigger 28 | $event->emit('new'); 29 | ``` 30 | 31 | ### Bind once 32 | 33 | ```php 34 | $event = new EventEmitter(); 35 | 36 | // Bind event once 37 | $event->once('new', function () { 38 | echo 'A new client is coming' . PHP_EOL; 39 | }); 40 | 41 | // Trigger 42 | $event->emit('new'); 43 | 44 | // Not trigger 45 | $event->emit('new'); 46 | ``` 47 | 48 | ### Bind many times 49 | 50 | ```php 51 | $event = new EventEmitter(); 52 | 53 | // Bind event twice 54 | $event->many('new', 2, function () { 55 | echo 'A new client is coming' . PHP_EOL; 56 | }); 57 | 58 | $event->emit('new'); // Trigger 59 | $event->emit('new'); // Trigger 60 | $event->emit('new'); // No trigger 61 | ``` 62 | 63 | ### Bind fuzzy 64 | 65 | ```php 66 | $event = new EventEmitter(); 67 | 68 | // Bind fuzzy event 69 | $event->on('news.*', function($id){ 70 | echo $id . ' is comming..., ID is '; 71 | }); 72 | 73 | $event->emit('news.1'); 74 | $event->emit('news.2'); 75 | $event->emit('news.3'); 76 | ``` 77 | 78 | ## Remove 79 | 80 | ### Unbind Event 81 | 82 | ```php 83 | $event = new EventEmitter(); 84 | 85 | // Closure 86 | $operator = function () { 87 | echo 'A new client is coming' . PHP_EOL; 88 | }; 89 | 90 | $event->on('new', $operator); 91 | 92 | $event->emit('new'); // Trigger 93 | 94 | $event->off('new', $operator); // Unbind 95 | $event->emit('new'); // No Trigger 96 | ``` 97 | 98 | ### Remove Events 99 | 100 | ```php 101 | $event = new EventEmitter(); 102 | 103 | $event->on('new', function () { 104 | echo 'A new client is coming' . PHP_EOL; 105 | }); 106 | 107 | $event->removeAllListeners('new'); 108 | ``` 109 | 110 | ### Remove all events 111 | 112 | ```php 113 | $event = new EventEmitter(); 114 | 115 | $event->on('new', function () { 116 | echo 'A new client is coming' . PHP_EOL; 117 | }); 118 | 119 | $event->on('close', function () { 120 | echo 'The client has closed' . PHP_EOL; 121 | }); 122 | 123 | $event->removeAllListeners(); 124 | ``` 125 | 126 | ### Extends class 127 | 128 | Add event feature for your class 129 | 130 | ```php 131 | class MyClass extends EventEmitter { 132 | } 133 | 134 | $my = new MyClass; 135 | $my->on('create', function($data){ 136 | $db->save($data); 137 | }); 138 | ``` 139 | 140 | ## Global Events 141 | 142 | ### Global 143 | 144 | Use as default event manager 145 | 146 | ```php 147 | // Register event listener 148 | Event::on('save', function ($arg) { 149 | echo '1 saved: ' . $arg . PHP_EOL; 150 | }); 151 | 152 | // Emit the event 153 | Event::emit('save', 'test'); 154 | ``` 155 | 156 | ### Change default event manager 157 | 158 | ```php 159 | $emitter = new EventEmitter(); 160 | // Change default event manager 161 | Event::emitter($emitter); 162 | 163 | $event->on('save', function ($arg) { 164 | echo '1 saved: ' . $arg . PHP_EOL; 165 | }); 166 | 167 | // Emit the save 168 | $event->emit('save', 'test'); 169 | 170 | // This is same as above 171 | $emitter->emit('save'); 172 | ``` 173 | 174 | License 175 | ============= 176 | 177 | (The MIT License) 178 | 179 | Copyright (c) 2012 hfcorriez <hfcorriez@gmail.com> 180 | 181 | Permission is hereby granted, free of charge, to any person obtaining 182 | a copy of this software and associated documentation files (the 183 | 'Software'), to deal in the Software without restriction, including 184 | without limitation the rights to use, copy, modify, merge, publish, 185 | distribute, sublicense, and/or sell copies of the Software, and to 186 | permit persons to whom the Software is furnished to do so, subject to 187 | the following conditions: 188 | 189 | The above copyright notice and this permission notice shall be 190 | included in all copies or substantial portions of the Software. 191 | 192 | THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 193 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 194 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 195 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 196 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 197 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 198 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "pagon/eventemitter", 3 | "description": "Event Emitter for PHP", 4 | "keywords": ["event", "event emitter", "hook"], 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "Corrie Zhao", 9 | "email": "hfcorriez@gmail.com" 10 | } 11 | ], 12 | "require": { 13 | "php": ">=5.3.0" 14 | }, 15 | "autoload": { 16 | "psr-0": { 17 | "Pagon\\Event": "lib/" 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /lib/Pagon/Event.php: -------------------------------------------------------------------------------- 1 | on($event, $listener); 34 | } 35 | 36 | /** 37 | * Register event 38 | * 39 | * @param array|string $event 40 | * @param callable $listener 41 | * @return EventEmitter 42 | */ 43 | public static function once($event, \Closure $listener) 44 | { 45 | return static::emitter()->once($event, $listener); 46 | } 47 | 48 | /** 49 | * Attach a listener to emit many times 50 | * 51 | * @param array|string $event 52 | * @param int $times 53 | * @param callable $listener 54 | * @return EventEmitter 55 | */ 56 | public static function many($event, $times = 1, \Closure $listener) 57 | { 58 | return static::emitter()->many($event, $times, $listener); 59 | } 60 | 61 | /** 62 | * Remove listener of giving event 63 | * 64 | * @param array|string $event 65 | * @param callable $listener 66 | * @return EventEmitter 67 | */ 68 | public static function off($event, \Closure $listener) 69 | { 70 | return static::emitter()->off($event, $listener); 71 | } 72 | 73 | /** 74 | * Get all listeners of giving event 75 | * 76 | * @param string $event 77 | * @return array 78 | */ 79 | public static function listeners($event) 80 | { 81 | return static::emitter()->listeners($event); 82 | } 83 | 84 | /** 85 | * Remove listener 86 | * 87 | * @param string $event 88 | * @param callable $listener 89 | * @return EventEmitter 90 | */ 91 | public static function removeListener($event, \Closure $listener) 92 | { 93 | return static::emitter()->removeListener($event, $listener); 94 | } 95 | 96 | /** 97 | * Remove all of event listeners 98 | * 99 | * @param $event 100 | * @return EventEmitter 101 | */ 102 | public static function removeAllListeners($event) 103 | { 104 | return static::emitter()->removeAllListeners($event); 105 | } 106 | 107 | /** 108 | * Set or get emitter 109 | * 110 | * @param EventEmitter $emitter 111 | * @return EventEmitter 112 | */ 113 | public static function emitter(EventEmitter $emitter = null) 114 | { 115 | if ($emitter) { 116 | static::$default_emitter = $emitter; 117 | } 118 | return static::$default_emitter; 119 | } 120 | } -------------------------------------------------------------------------------- /lib/Pagon/EventEmitter.php: -------------------------------------------------------------------------------- 1 | listeners as $name => $listeners) { 40 | if (strpos($name, '*') === false || !self::match($name, $event)) { 41 | continue; 42 | } 43 | 44 | foreach ($this->listeners[$name] as &$listener) { 45 | $all_listeners[$name][] = & $listener; 46 | } 47 | } 48 | 49 | if (!empty($this->listeners[$event])) { 50 | foreach ($this->listeners[$event] as &$listener) { 51 | $all_listeners[$event][] = & $listener; 52 | } 53 | } 54 | 55 | $emitted = false; 56 | 57 | // Loop listeners for callback 58 | foreach ($all_listeners as $name => $listeners) { 59 | $this_args = $args; 60 | if (strpos($name, '*') !== false) { 61 | array_push($this_args, $event); 62 | } 63 | foreach ($listeners as &$listener) { 64 | if ($listener instanceof \Closure) { 65 | // Closure Listener 66 | call_user_func_array($listener, $this_args); 67 | $emitted = true; 68 | } elseif (is_array($listener) && $listener[0] instanceof \Closure) { 69 | if ($listener[1]['times'] > 0) { 70 | // Closure Listener 71 | call_user_func_array($listener[0], $this_args); 72 | $emitted = true; 73 | $listener[1]['times']--; 74 | } 75 | } 76 | } 77 | } 78 | 79 | return $emitted; 80 | } 81 | 82 | /** 83 | * Attach a event listener 84 | * 85 | * @static 86 | * @param array|string $event 87 | * @param \Closure $listener 88 | * @return $this 89 | */ 90 | public function on($event, \Closure $listener) 91 | { 92 | foreach ((array)$event as $e) { 93 | $this->listeners[strtolower($e)][] = $listener; 94 | } 95 | return $this; 96 | } 97 | 98 | /** 99 | * Attach a listener to emit once 100 | * 101 | * @param array|string $event 102 | * @param callable $listener 103 | * @return $this 104 | */ 105 | public function once($event, \Closure $listener) 106 | { 107 | foreach ((array)$event as $e) { 108 | $this->listeners[strtolower($e)][] = array($listener, array('times' => 1)); 109 | } 110 | return $this; 111 | } 112 | 113 | /** 114 | * Attach a listener to emit many times 115 | * 116 | * @param array|string $event 117 | * @param int $times 118 | * @param callable $listener 119 | * @return $this 120 | */ 121 | public function many($event, $times = 1, \Closure $listener) 122 | { 123 | foreach ((array)$event as $e) { 124 | $this->listeners[strtolower($e)][] = array($listener, array('times' => $times)); 125 | } 126 | return $this; 127 | } 128 | 129 | /** 130 | * Alias for removeListener 131 | * 132 | * @param array|string $event 133 | * @param callable $listener 134 | * @return $this 135 | */ 136 | public function off($event, \Closure $listener) 137 | { 138 | foreach ((array)$event as $e) { 139 | $e = strtolower($e); 140 | if (!empty($this->listeners[$e])) { 141 | // Find Listener index 142 | if (($key = array_search($listener, $this->listeners[$e])) !== false) { 143 | // Remove it 144 | unset($this->listeners[$e][$key]); 145 | } 146 | } 147 | } 148 | return $this; 149 | } 150 | 151 | /** 152 | * Get listeners of given event 153 | * 154 | * @param string $event 155 | * @return array 156 | */ 157 | public function listeners($event) 158 | { 159 | if (!empty($this->listeners[$event])) { 160 | return $this->listeners[$event]; 161 | } 162 | return array(); 163 | } 164 | 165 | /** 166 | * Attach a event listener 167 | * 168 | * @static 169 | * @param array|string $event 170 | * @param \Closure $listener 171 | * @return $this 172 | */ 173 | public function addListener($event, \Closure $listener) 174 | { 175 | return $this->on($event, $listener); 176 | } 177 | 178 | /** 179 | * Detach a event listener 180 | * 181 | * @static 182 | * @param string $event 183 | * @param \Closure $listener 184 | * @return $this 185 | */ 186 | public function removeListener($event, \Closure $listener) 187 | { 188 | return $this->off($event, $listener); 189 | } 190 | 191 | /** 192 | * Remove all listeners of given event 193 | * 194 | * @param string $event 195 | * @return $this 196 | */ 197 | public function removeAllListeners($event = null) 198 | { 199 | if ($event === null) { 200 | $this->listeners = array(); 201 | } else if (($event = strtolower($event)) && !empty($this->listeners[$event])) { 202 | $this->listeners[$event] = array(); 203 | } 204 | return $this; 205 | } 206 | 207 | /** 208 | * Match the pattern 209 | * 210 | * @param string $pattern 211 | * @param string $string 212 | * @return bool|int 213 | */ 214 | protected static function match($pattern, $string) 215 | { 216 | if (FNMATCH) { 217 | return fnmatch($pattern, $string); 218 | } else { 219 | return preg_match("#^" . strtr(preg_quote($pattern, '#'), array('\*' => '.*', '\?' => '.')) . "$#i", $string); 220 | } 221 | } 222 | } -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 15 | 16 | 17 | ./tests/ 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /tests/EventEmitterTest.php: -------------------------------------------------------------------------------- 1 | event = new EventEmitter(); 14 | } 15 | 16 | public function tearDown() 17 | { 18 | $GLOBALS = array(); 19 | } 20 | 21 | public function testSimple() 22 | { 23 | $closure = function () { 24 | }; 25 | 26 | $this->event->on('test', $closure); 27 | 28 | $this->assertEquals(array($closure), $this->event->listeners('test')); 29 | } 30 | 31 | public function testEmit() 32 | { 33 | $GLOBALS['emit_result'] = 0; 34 | $GLOBALS['emit_arg'] = ''; 35 | $closure = function ($arg) { 36 | $GLOBALS['emit_result'] = 1; 37 | $GLOBALS['emit_arg'] = $arg; 38 | }; 39 | 40 | $this->event->on('test', $closure); 41 | 42 | $this->assertEquals(0, $GLOBALS['emit_result']); 43 | $this->assertEquals('', $GLOBALS['emit_arg']); 44 | $this->event->emit('test', 'go'); 45 | $this->assertEquals(1, $GLOBALS['emit_result']); 46 | $this->assertEquals('go', $GLOBALS['emit_arg']); 47 | } 48 | 49 | public function testOnce() 50 | { 51 | $GLOBALS['emit_result'] = 0; 52 | $closure = function () { 53 | $GLOBALS['emit_result']++; 54 | }; 55 | 56 | $this->event->once('test', $closure); 57 | 58 | $this->assertEquals(0, $GLOBALS['emit_result']); 59 | $this->event->emit('test'); 60 | $this->assertEquals(1, $GLOBALS['emit_result']); 61 | $this->event->emit('test'); 62 | $this->assertEquals(1, $GLOBALS['emit_result']); 63 | } 64 | 65 | public function testMany() 66 | { 67 | $GLOBALS['emit_result'] = 0; 68 | $closure = function () { 69 | $GLOBALS['emit_result']++; 70 | }; 71 | 72 | $this->event->many('test', 2, $closure); 73 | 74 | $this->assertEquals(0, $GLOBALS['emit_result']); 75 | $this->event->emit('test'); 76 | $this->assertEquals(1, $GLOBALS['emit_result']); 77 | $this->event->emit('test'); 78 | $this->assertEquals(2, $GLOBALS['emit_result']); 79 | $this->event->emit('test'); 80 | $this->assertEquals(2, $GLOBALS['emit_result']); 81 | } 82 | 83 | public function testPattern() 84 | { 85 | $GLOBALS['emit_result'] = ''; 86 | $GLOBALS['emit_count'] = 0; 87 | $closure = function ($event) { 88 | $GLOBALS['emit_result'] = $event; 89 | $GLOBALS['emit_count']++; 90 | }; 91 | 92 | $this->event->on('test.*', $closure); 93 | 94 | $this->assertEquals('', $GLOBALS['emit_result']); 95 | $this->assertEquals(0, $GLOBALS['emit_count']); 96 | $this->event->emit('test.1'); 97 | $this->assertEquals('test.1', $GLOBALS['emit_result']); 98 | $this->assertEquals(1, $GLOBALS['emit_count']); 99 | $this->event->emit('test.2'); 100 | $this->assertEquals('test.2', $GLOBALS['emit_result']); 101 | $this->assertEquals(2, $GLOBALS['emit_count']); 102 | $this->event->emit('test'); 103 | $this->assertEquals('test.2', $GLOBALS['emit_result']); 104 | $this->assertEquals(2, $GLOBALS['emit_count']); 105 | } 106 | 107 | public function testMulti() 108 | { 109 | $GLOBALS['emit_result'] = 0; 110 | $closure = function () { 111 | $GLOBALS['emit_result']++; 112 | }; 113 | 114 | $this->event->on(array('a', 'b', 'c'), $closure); 115 | 116 | $this->assertEquals(0, $GLOBALS['emit_result']); 117 | $this->event->emit('a'); 118 | $this->assertEquals(1, $GLOBALS['emit_result']); 119 | $this->event->emit('c'); 120 | $this->assertEquals(2, $GLOBALS['emit_result']); 121 | $this->event->emit('b'); 122 | $this->assertEquals(3, $GLOBALS['emit_result']); 123 | } 124 | 125 | public function testOff() 126 | { 127 | $closure = function () { 128 | }; 129 | $closure1 = function () { 130 | }; 131 | 132 | $this->event->addListener('test', $closure); 133 | $this->event->addListener('test', $closure1); 134 | 135 | $this->assertEquals(array($closure, $closure1), $this->event->listeners('test')); 136 | 137 | $this->event->removeListener('test', $closure); 138 | 139 | $this->assertEquals(array(1 => $closure1), $this->event->listeners('test')); 140 | } 141 | 142 | public function testRemoveAll() 143 | { 144 | $closure = function () { 145 | }; 146 | $closure1 = function () { 147 | }; 148 | 149 | $this->event->on('test', $closure); 150 | $this->event->on('test', $closure1); 151 | $this->event->on('test1', $closure1); 152 | 153 | $this->assertEquals(array($closure, $closure1), $this->event->listeners('test')); 154 | $this->assertEquals(array($closure1), $this->event->listeners('test1')); 155 | 156 | $this->event->removeAllListeners('test'); 157 | 158 | $this->assertEquals(array(), $this->event->listeners('test')); 159 | 160 | $this->event->removeAllListeners(); 161 | 162 | $this->assertEquals(array(), $this->event->listeners('test1')); 163 | } 164 | 165 | public function testOnceMulti() 166 | { 167 | $GLOBALS['emit_result'] = 0; 168 | $closure = function () { 169 | $GLOBALS['emit_result']++; 170 | }; 171 | 172 | $this->event->once(array('a', 'b'), $closure); 173 | 174 | $this->assertEquals(0, $GLOBALS['emit_result']); 175 | $this->event->emit('a'); 176 | $this->assertEquals(1, $GLOBALS['emit_result']); 177 | $this->event->emit('a'); 178 | $this->assertEquals(1, $GLOBALS['emit_result']); 179 | $this->event->emit('b'); 180 | $this->assertEquals(2, $GLOBALS['emit_result']); 181 | $this->event->emit('b'); 182 | $this->assertEquals(2, $GLOBALS['emit_result']); 183 | } 184 | 185 | public function testManyMulti() 186 | { 187 | $GLOBALS['emit_result'] = 0; 188 | $closure = function () { 189 | $GLOBALS['emit_result']++; 190 | }; 191 | 192 | $this->event->many(array('a', 'b'), 2, $closure); 193 | 194 | $this->assertEquals(0, $GLOBALS['emit_result']); 195 | $this->event->emit('a'); 196 | $this->assertEquals(1, $GLOBALS['emit_result']); 197 | $this->event->emit('a'); 198 | $this->assertEquals(2, $GLOBALS['emit_result']); 199 | $this->event->emit('a'); 200 | $this->assertEquals(2, $GLOBALS['emit_result']); 201 | $this->event->emit('b'); 202 | $this->assertEquals(3, $GLOBALS['emit_result']); 203 | $this->event->emit('b'); 204 | $this->assertEquals(4, $GLOBALS['emit_result']); 205 | } 206 | 207 | public function testOffMulti() 208 | { 209 | $closure = function () { 210 | }; 211 | $closure1 = function () { 212 | }; 213 | 214 | $this->event->on(array('a', 'b'), $closure); 215 | $this->event->addListener(array('a', 'b'), $closure1); 216 | 217 | $this->assertEquals(array($closure, $closure1), $this->event->listeners('a')); 218 | $this->assertEquals(array($closure, $closure1), $this->event->listeners('b')); 219 | 220 | $this->event->off(array('a', 'b'), $closure); 221 | 222 | $this->assertEquals(array(1 => $closure1), $this->event->listeners('a')); 223 | $this->assertEquals(array(1 => $closure1), $this->event->listeners('b')); 224 | } 225 | } 226 | --------------------------------------------------------------------------------