https://github.com/lastguest/ev
A tweet-sized PHP Event emitter
https://github.com/lastguest/ev
event-emitter lightweight microframework php
Last synced: 9 months ago
JSON representation
A tweet-sized PHP Event emitter
- Host: GitHub
- URL: https://github.com/lastguest/ev
- Owner: lastguest
- License: mit
- Created: 2014-10-17T23:28:25.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2015-02-11T21:41:37.000Z (almost 11 years ago)
- Last Synced: 2024-11-18T09:47:20.138Z (about 1 year ago)
- Topics: event-emitter, lightweight, microframework, php
- Language: PHP
- Size: 142 KB
- Stars: 42
- Watchers: 4
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ∑
A tweet-sized PHP Event emitter in only 103 bytes.
[](https://scrutinizer-ci.com/g/lastguest/ev/build-status/master) [](https://packagist.org/packages/lastguest/ev) [](https://packagist.org/packages/lastguest/ev) [](https://packagist.org/packages/lastguest/ev) [](https://packagist.org/packages/lastguest/ev)
```php
function ∑($n,$c=0){static$r;is_callable($c)?$r[$n][]=$c:@array_walk($r[$n],'call_user_func',$c?:[]);}
```
**Warning: This is a pure proof of concept of a tweet sized event emitter**
**DO NOT USE IT IN PRODUCTION!**
### How to use
**Bind handlers to event:**
```php
// Bind callback to event "init"
∑('init', function(){
echo "Init event triggered!\n";
});
// Bind another callback to event "init"
∑('init', function(){
echo "Second handler for init triggered!\n";
});
// Bind callback to event "debug.event" and listen to passed parameters
∑('debug.event', function($idx,$params){
echo "Called debug.event[$idx] with parameters : ",print_r($params,true),"\n";
});
```
**Trigger Events:**
Calling an event with no parameters triggers all binded event handlers.
```php
// Trigger `init`
∑("init");
```
Output:
```html
Init event triggered!
Second handler for init triggered!
```
You can pass parameters to all handlers as an array :
```php
// Trigger `debug.event` passing parameters
∑('debug.event',[1,2,'Hello, Friend.']);
```
Output:
```html
Called debug.event[0] with parameters : Array
(
[0] => 1
[1] => 2
[2] => Hello, Friend.
)
```
## Commented source
```php
function ∑ ($event_name, $callback=null){
// The event handlers repository.
static $event_handlers;
if (is_callable($callback)) {
// We are binding a callback to an event, add $callback to the
// events handler repository.
$event_handlers[$event_name][] = $callback;
} else {
// When $callback is not a callable, then we are triggering the event.
// In this case, $callback will be our array of parameters to pass to event handlers
$params = $callback ? $callback : [];
// Walk all handlers binded to event $event_name and invoke them with the
// ($event_index, $params) parameters.
array_walk($event_handlers[$event_name],'call_user_func',$params);
}
}
```
## License (MIT)
Copyright (c) 2014 Stefano Azzolini
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
[](https://bitdeli.com/free "Bitdeli Badge")