Skip to content

Instantly share code, notes, and snippets.

@BlackScorp
Created September 11, 2019 20:27
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BlackScorp/8c305774ec9938751a3a0ca981ba4fee to your computer and use it in GitHub Desktop.
Save BlackScorp/8c305774ec9938751a3a0ca981ba4fee to your computer and use it in GitHub Desktop.
<?php
function event(string $name, array $data = [],callable $action = null,int $priority = 0){
static $events = [];
if($action){
$events[$name][]=[
'priority' => $priority,
'action' => $action
];
return null;
}
if(!isset($events[$name])){
return null;
}
$eventData = $events[$name];
usort($eventData,function($a,$b){
return $b['priority'] <=> $a['priority'];
});
foreach($eventData as $event){
$event['action'](...$data);
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment