12345678910111213141516171819202122232425262728293031323334 |
- <?php
- namespace Juju;
- trait Events {
- private $events = [];
- public function on(string $name, callable $fn){
- if(!isset($this->events[$name])){
- $this->events[$name] = [];
- }
- $this->events[$name][] = $fn;
- return $this;
- }
- public function off(string $name, callable $fn = null){
- if(isset($this->events[$name])){
- $a = $this->events[$name];
- foreach($a as $k => $f){
- if(is_null($fn) || $f == $fn){
- array_splice($a, $k, 1);
- }
- }
- }
- return $this;
- }
- public function fire(string $name, ...$args){
- if(isset($this->events[$name])){
- foreach($this->events[$name] as $fn){
- if($fn(...$args) === false){
- return false;
- }
- }
- }
- return true;
- }
- }
- ?>
|