https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-14-event-dispatcher.md
namespace app\Events;
class UserLogin
{
public $user;
public function __construct($user)
{
$this->user = $user;
}
}
namespace app\Events;
class UserLogout
{
public $user;
public function __construct($user)
{
$this->user = $user;
}
}
use App\Events\UserLogin;
use ChangZee\EventDispatcher\EventSubscriber;
class UserLoginListener extends EventSubscriber
{
public function events(): array
{
return [
UserLogin::class => 'handleLogin',
UserLogout::class => ['handleLogout', 100], // 后面的数字为优先级
];
}
/**
* @param UserLogin $event
*/
public function handleLogin($event)
{
// pass
}
/**
* @param UserLogout $event
*/
public function handleLogout($event)
{
}
}
$provider = new JigsawPuzzles\EventDispatcher\ListenerProvider();
$listener = new UserLoginSubscriber();
$listener->attachSubscriber($provider);
$dispatcher = new \JigsawPuzzles\EventDispatcher\EventDispatcher($provider);
// 分发事件
$dispatcher->dispatch(new UserLogin());