2022-08-29 20:26:05 +00:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
class MTTNotificationCenter
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* @var array<string, MTTNotificationObserverInterface[]>
|
|
|
|
|
*/
|
|
|
|
|
private static $observers = [];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $notification
|
|
|
|
|
* @param MTTNotificationObserverInterface $observer
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public static function addObserverForNotification(string $notification, MTTNotificationObserverInterface $observer)
|
|
|
|
|
{
|
|
|
|
|
if (!isset(self::$observers[$notification])) {
|
|
|
|
|
self::$observers[$notification] = [];
|
|
|
|
|
}
|
2022-09-05 19:28:59 +00:00
|
|
|
if (!in_array($observer, self::$observers[$notification])) {
|
|
|
|
|
// do not duplicate same observer
|
|
|
|
|
self::$observers[$notification][] = $observer;
|
|
|
|
|
}
|
2022-08-29 20:26:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string[] $notifications
|
|
|
|
|
* @param MTTNotificationObserverInterface $observer
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public static function addObserverForNotifications(array $notifications, MTTNotificationObserverInterface $observer)
|
|
|
|
|
{
|
|
|
|
|
foreach ($notifications as $notification) {
|
|
|
|
|
self::addObserverForNotification($notification, $observer);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static function postNotification(string $notification, $object)
|
|
|
|
|
{
|
2022-09-05 19:28:59 +00:00
|
|
|
if (!isset(self::$observers[$notification])) {
|
|
|
|
|
return; // No observers for this notification
|
|
|
|
|
}
|
|
|
|
|
foreach (self::$observers[$notification] as $observer) {
|
|
|
|
|
$observer->notification($notification, $object);
|
|
|
|
|
}
|
2022-08-29 20:26:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Run this near exit()
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2022-09-05 19:28:59 +00:00
|
|
|
public static function postDidFinishRequestNotification()
|
2022-08-29 20:26:05 +00:00
|
|
|
{
|
2022-09-05 19:28:59 +00:00
|
|
|
if ( ! isset(self::$observers[MTTNotification::didFinishRequest]) ) {
|
|
|
|
|
return; // No observers for didFinishRequest
|
2022-08-29 20:26:05 +00:00
|
|
|
}
|
|
|
|
|
if (function_exists('fastcgi_finish_request')) {
|
2022-09-05 19:28:59 +00:00
|
|
|
if (session_status() == PHP_SESSION_ACTIVE) {
|
|
|
|
|
session_write_close(); // Close active session
|
2022-08-29 20:26:05 +00:00
|
|
|
}
|
2022-09-05 19:28:59 +00:00
|
|
|
fastcgi_finish_request();
|
2022-08-29 20:26:05 +00:00
|
|
|
}
|
2022-09-05 19:28:59 +00:00
|
|
|
self::postNotification(MTTNotification::didFinishRequest, null);
|
2022-08-29 20:26:05 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface MTTNotificationObserverInterface
|
|
|
|
|
{
|
|
|
|
|
function notification(string $notification, $object);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Enum
|
|
|
|
|
abstract class MTTNotification
|
|
|
|
|
{
|
2022-09-05 19:28:59 +00:00
|
|
|
const didFinishRequest = 'didFinishRequest';
|
2022-08-29 20:26:05 +00:00
|
|
|
const didCreateTask = 'didCreateTask';
|
|
|
|
|
const didCreateList = 'didCreateList';
|
|
|
|
|
}
|
|
|
|
|
|