2022-08-29 20:26:05 +00:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
|
2022-09-06 12:45:00 +00:00
|
|
|
/*
|
|
|
|
|
This file is a part of myTinyTodo.
|
|
|
|
|
(C) Copyright 2022 Max Pozdeev <maxpozdeev@gmail.com>
|
|
|
|
|
Licensed under the GNU GPL version 2 or any later. See file COPYRIGHT for details.
|
|
|
|
|
*/
|
|
|
|
|
|
2022-08-29 20:26:05 +00:00
|
|
|
class MTTNotificationCenter
|
|
|
|
|
{
|
|
|
|
|
/**
|
2022-11-13 11:56:56 +00:00
|
|
|
* @var array<string, MTTNotificationObserverInterface[]|callable[]>
|
2022-08-29 20:26:05 +00:00
|
|
|
*/
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-13 11:56:56 +00:00
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param string $notification
|
|
|
|
|
* @param callable $callback
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public static function addCallbackForNotification(string $notification, callable $callback)
|
|
|
|
|
{
|
|
|
|
|
if (!isset(self::$observers[$notification])) {
|
|
|
|
|
self::$observers[$notification] = [];
|
|
|
|
|
}
|
|
|
|
|
self::$observers[$notification][] = $callback;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-29 20:26:05 +00:00
|
|
|
|
|
|
|
|
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) {
|
2022-11-13 11:56:56 +00:00
|
|
|
if ($observer instanceof MTTNotificationObserverInterface) {
|
|
|
|
|
$observer->notification($notification, $object);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$observer($object);
|
|
|
|
|
}
|
2022-09-05 19:28:59 +00:00
|
|
|
}
|
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';
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-13 11:56:56 +00:00
|
|
|
function add_action(string $notification, callable $callback)
|
|
|
|
|
{
|
|
|
|
|
MTTNotificationCenter::addCallbackForNotification($notification, $callback);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function do_action(string $notification, $object = null)
|
|
|
|
|
{
|
|
|
|
|
MTTNotificationCenter::postNotification($notification, $object);
|
|
|
|
|
}
|