Skip to content

Instantly share code, notes, and snippets.

@ArrayIterator
Created June 9, 2021 08:39
Show Gist options
  • Select an option

  • Save ArrayIterator/60b2ac6f736a889936b7cfce86b663e9 to your computer and use it in GitHub Desktop.

Select an option

Save ArrayIterator/60b2ac6f736a889936b7cfce86b663e9 to your computer and use it in GitHub Desktop.
<?php
class Event
{
protected static $events = [];
public static function register(
$name,
$callable,
$priority = 10
) {
self::$events[$name][] = [$priority, $callable];
uasort(
self::$events[$name],
fn ($a, $b) => $a[0] === $b[0] ? 0 : ($a[0] < $b[0] ? -1 : 1)
);
}
public static function dispatch($name)
{
if (($ev = self::$events[$name]??null)) {
array_map(
'call_user_func',
array_map(fn ($e) => $e[1], $ev)
);
return;
}
throw new RuntimeException(
'Unregistered Event'
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment