Last active
August 29, 2015 14:00
-
-
Save wouterj/11367304 to your computer and use it in GitHub Desktop.
An Event Mediator that fits in a tweet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class M{function t($̀,$́){foreach(@$this->l[$̀]?:[]as$̃)$̃($́);}function a($̀,$́,$̃){$̂=&$this->l[$̀];while(@$̂[$̃])$̃.='a';$̂[$̃]=$́;krsort($̂,1);}} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /* | |
| Mediator can work with closures in PHP 5.3+ | |
| It can work with any valid callable in PHP 5.4+ | |
| */ | |
| $mediator = new M(); | |
| /* | |
| M#a() attaches a listener. Arguments: | |
| - event name | |
| - listener | |
| - priority | |
| */ | |
| $mediator->a('hello_message_requested', function ($name) { | |
| echo 'Hello '.$name.'!'; | |
| }, 0); | |
| /* | |
| M#t() triggers an event. Arguments: | |
| - event name | |
| - event class (or plain data) | |
| */ | |
| $mediator->t('hello_message_requested', 'world'); // "Hello world!" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class M | |
| { | |
| function t($e, $a) | |
| { | |
| foreach (@$this->l[$e] ?: [] as $l) | |
| $l($a); | |
| } | |
| function a($e, $l, $p) | |
| { | |
| $s = &$this->l[$e]; | |
| while (@$s[$p]) | |
| $p .= 'a'; | |
| $s[$p] = $l; | |
| krsort($s, 1); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment