802 lines
27 KiB
PHP
802 lines
27 KiB
PHP
<?php
|
|
|
|
/*
|
|
* SPDX-FileCopyrightText: Copyright 2020 - 2026 grommunio GmbH
|
|
* SPDX-FileCopyrightText: Copyright 2016 Kopano and its licensors
|
|
* SPDX-FileCopyrightText: Copyright 2005 - 2016 Zarafa B.V. and its licensors
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
|
*/
|
|
|
|
require_once __DIR__ . '/../exceptions/class.BusException.php';
|
|
|
|
/**
|
|
* Generic event bus with subscribe/notify architecture.
|
|
*
|
|
* Every notifier registers itself by this class so that it can receive events. The events represent changes in the underlying
|
|
* MAPI store like changes to folders, e-mails, calendar items, etc.
|
|
*
|
|
* A notifier can register itself for the following events:
|
|
* - OBJECT_SAVE
|
|
* - OBJECT_DELETE
|
|
*
|
|
* - TABLE_SAVE
|
|
* - TABLE_DELETE
|
|
*
|
|
* - REQUEST_START
|
|
* - REQUEST_END
|
|
*
|
|
* To use the REQUEST_* events, make sure you register your notifier with REQUEST_ENTRYID
|
|
* as the dummy entryid.
|
|
*
|
|
* Events:
|
|
*
|
|
* (The parameters that are discussed are passed to notify() and sent to the update() methods of the notifiers)
|
|
*
|
|
* <b>OBJECT_SAVE</b>
|
|
* This event is triggered when a folder object is created or modified. The entryid parameter is the entryid
|
|
* of the object that has been created or modified. The data parameter contains an array of properties of the
|
|
* object, with the following properties: PR_ENTRYID, PR_STORE_ENTRYID
|
|
*
|
|
* <b>OBJECT_DELETE</b>
|
|
* This event is triggered when a folder is deleted. The entryid parameter is the entryid of the object
|
|
* that has been deleted. The data parameter contains an array of properties of the deleted object, with
|
|
* the following properties: PR_ENTRYID, PR_STORE_ENTRYID
|
|
*
|
|
* <b>TABLE_SAVE</b>
|
|
* This event is triggered when a message item is created or modified. The entryid parameter is the entryid of the modified
|
|
* object. The data parameter contains the following properties of the modified message: PR_ENTRYID, PR_PARENT_ENTRYID,
|
|
* PR_STORE_ENTRYID
|
|
*
|
|
* <b>TABLE_DELETE</b>
|
|
* This event is triggered when a message item is deleted. The entryid parameter is the entryid of the deleted object. The
|
|
* data parameter contains the following properties of the deleted message: PR_ENTRYID, PR_PARENT_ENTRYID, PR_STORE_ENTRYID
|
|
*
|
|
* <b>REQUEST_START</b>
|
|
* This event is triggered for EVERY XML request that is done to the backend, before processing of the XML or other events.
|
|
* The entryid parameter must be the fixed value REQUEST_ENTRYID. The $data parameter is not passed.
|
|
*
|
|
* <b>REQUEST_END</b>
|
|
* This event is triggereed for EVERY XML request that is done to the backend, after all notifiers processing and will be the last
|
|
* event triggered. The entryid parameter must be that fixed value REQUEST_ENTRYID. The $data parameter is not passed.
|
|
*
|
|
* @todo The event names are rather misleading and should be renamed to FOLDER_CHANGE, FOLDER_DELETE, MESSAGE_CHANGE, MESSAGE_DELETE
|
|
* @todo The entryid is passed in $entryid AND in $data in almost all cases, which is rather wasteful
|
|
*/
|
|
class Bus {
|
|
/**
|
|
* data which is sent back to the client.
|
|
*/
|
|
private $responseData;
|
|
|
|
/**
|
|
* all registered notifiers.
|
|
*/
|
|
private $registeredNotifiers;
|
|
|
|
/**
|
|
* all registered store notifiers.
|
|
*/
|
|
private $registeredStoreNotifiers;
|
|
|
|
/**
|
|
* all notifier objects.
|
|
*/
|
|
private $notifiers;
|
|
|
|
/**
|
|
* Monotonic versions for notifier state persisted independently of the Bus.
|
|
*/
|
|
private $notifierVersions;
|
|
|
|
/**
|
|
* Registration generation observed by this request.
|
|
*/
|
|
private $synchronizedRegistrationVersion;
|
|
private $registrationStateDirty;
|
|
|
|
/**
|
|
* Constructor.
|
|
*/
|
|
public function __construct() {
|
|
$this->responseData = [];
|
|
|
|
$this->registeredNotifiers = [];
|
|
$this->registeredStoreNotifiers = [];
|
|
$this->notifiers = [];
|
|
$this->notifierVersions = [];
|
|
$this->synchronizedRegistrationVersion = null;
|
|
$this->registrationStateDirty = false;
|
|
}
|
|
|
|
/**
|
|
* Register a notifier on the bus.
|
|
*
|
|
* This will check if a instance for the given $notifierName already exists, and if
|
|
* not will instantiate it through the dispatcher. It will then use $this->registerEvent
|
|
* to register the instance for all correct events on the given $entryid(s)
|
|
*
|
|
* After a notifier is registered on the bus, it will receive updates to objects generated by the modules
|
|
* which are handling the requests. For example a notifier is registered on the inbox can receive changes
|
|
* of the item count of the inbox by specifying the entryid of the inbox here and the event
|
|
*
|
|
* @param string $notifierName class name of the notifier to register
|
|
* @param string|string[] $entryid entry ID or entry IDs on which to register the notifier
|
|
* @param bool $store whether the entry ID points to a store whose events should bubble
|
|
*/
|
|
public function registerNotifier($notifierName, $entryid, $store = false) {
|
|
if (!isset($this->notifiers[$notifierName])) {
|
|
$this->notifiers[$notifierName] = $GLOBALS["dispatcher"]->loadNotifier($notifierName);
|
|
$this->registrationStateDirty = true;
|
|
}
|
|
|
|
// Obtain the events which are supported by this notifier
|
|
$events = $this->notifiers[$notifierName]->getEvents();
|
|
|
|
if (is_array($entryid)) {
|
|
foreach ($entryid as $id) {
|
|
$this->registerEvent($notifierName, $id, $events, $store);
|
|
}
|
|
}
|
|
else {
|
|
$this->registerEvent($notifierName, $entryid, $events, $store);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Register notifier events on the bus.
|
|
*
|
|
* Called by $registerNotifier after the notifier instance was created. This function will then
|
|
* register the given $notifierName to the provided $entryid and will mark the $events for which
|
|
* this notifier should be fired.
|
|
*
|
|
* Valid values for $events are OBJECT_SAVE, OBJECT_DELETE, TABLE_SAVE, TABLE_DELETE, REQUEST_START and REQUEST_END
|
|
*
|
|
* @param string $notifierName The classname of the notifier to register
|
|
* @param string $entryid EntryID of an object coupled to the notifier
|
|
* @param number $events Bitmask of events that the notifier should receive
|
|
* @param bool $store optional If true, the $entryid points to the store and all events within the store are
|
|
* bubbled to this notifier object
|
|
*/
|
|
public function registerEvent($notifierName, $entryid, $events, $store = false) {
|
|
if (!self::isRegistrationEntryid($entryid)) {
|
|
return;
|
|
}
|
|
$entryidCmp = $GLOBALS["entryid"];
|
|
|
|
if ($store) {
|
|
$found = false;
|
|
|
|
foreach ($this->registeredStoreNotifiers as $key => &$storeNotifier) {
|
|
if ($entryidCmp->compareEntryIds($storeNotifier['entryid'], $entryid)) {
|
|
if (!isset($storeNotifier[$notifierName]['events']) || $storeNotifier[$notifierName]['events'] !== $events) {
|
|
$this->registrationStateDirty = true;
|
|
}
|
|
$storeNotifier[$notifierName] = ['events' => $events];
|
|
$found = true;
|
|
break;
|
|
}
|
|
}
|
|
unset($storeNotifier);
|
|
|
|
if (!$found) {
|
|
$this->registeredStoreNotifiers[] = [
|
|
'entryid' => $entryid,
|
|
$notifierName => ['events' => $events],
|
|
];
|
|
$this->registrationStateDirty = true;
|
|
}
|
|
}
|
|
else {
|
|
$found = false;
|
|
|
|
foreach ($this->registeredNotifiers as $key => &$folderNotifier) {
|
|
if ($entryidCmp->compareEntryIds($folderNotifier['entryid'], $entryid)) {
|
|
if (!isset($folderNotifier[$notifierName]['events']) || $folderNotifier[$notifierName]['events'] !== $events) {
|
|
$this->registrationStateDirty = true;
|
|
}
|
|
$folderNotifier[$notifierName] = ['events' => $events];
|
|
$found = true;
|
|
break;
|
|
}
|
|
}
|
|
unset($folderNotifier);
|
|
|
|
if (!$found) {
|
|
$this->registeredNotifiers[] = [
|
|
'entryid' => $entryid,
|
|
$notifierName => ['events' => $events],
|
|
];
|
|
$this->registrationStateDirty = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Broadcast an update to notifiers attached to bus.
|
|
*
|
|
* This function is used to send an update to other notifiers that have been attached to the bus via register().
|
|
*
|
|
* @param string $entryID Entryid of the object for which the event has happened
|
|
* @param int $event Event which should be fired. Can be any of OBJECT_SAVE, OBJECT_DELETE, TABLE_SAVE, TABLE_DELETE, REQUEST_START and REQUEST_END.
|
|
* @param array $data the data which is used to execute the event, which differs for each event type
|
|
*/
|
|
public function notify($entryID, $event, $data = null) {
|
|
if (!$this->synchronizePersistentState() && $event === REQUEST_START) {
|
|
throw new RuntimeException('Unable to synchronize notifier state');
|
|
}
|
|
$entryidCmp = $GLOBALS["entryid"];
|
|
|
|
// Notifiers must be get only ONE update
|
|
$updatedNotifiers = [];
|
|
|
|
// Check if there are bubbled notifiers, and if a PR_STORE_ENTRYID was provided
|
|
// on which the notifiers should be executed.
|
|
if ($entryID !== REQUEST_ENTRYID && $data && isset($data[PR_STORE_ENTRYID])) {
|
|
$storeEntryid = bin2hex($data[PR_STORE_ENTRYID]);
|
|
|
|
// Update the store notifier
|
|
foreach ($this->registeredStoreNotifiers as &$storeNotifier) {
|
|
if ($entryidCmp->compareEntryIds($storeNotifier['entryid'], $storeEntryid)) {
|
|
foreach ($storeNotifier as $notifierName => $notifier) {
|
|
if (isset($notifier['events']) && ($notifier['events'] & $event)) {
|
|
if (!isset($updatedNotifiers[$notifierName])) {
|
|
if (isset($this->notifiers[$notifierName]) && is_object($this->notifiers[$notifierName])) {
|
|
$this->updateNotifier($notifierName, $event, $entryID, $data);
|
|
$updatedNotifiers[$notifierName] = true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
unset($storeNotifier);
|
|
}
|
|
|
|
// Update the notifier
|
|
foreach ($this->registeredNotifiers as &$folderNotifier) {
|
|
if (($entryID === REQUEST_ENTRYID && $folderNotifier['entryid'] === REQUEST_ENTRYID) ||
|
|
$entryidCmp->compareEntryIds($folderNotifier['entryid'], $entryID)) {
|
|
foreach ($folderNotifier as $notifierName => $notifier) {
|
|
if (isset($notifier['events']) && ($notifier['events'] & $event)) {
|
|
if (!isset($updatedNotifiers[$notifierName])) {
|
|
if (isset($this->notifiers[$notifierName]) && is_object($this->notifiers[$notifierName])) {
|
|
$this->updateNotifier($notifierName, $event, $entryID, $data);
|
|
$updatedNotifiers[$notifierName] = true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
unset($folderNotifier);
|
|
}
|
|
|
|
/**
|
|
* Update one notifier against its latest persistent state.
|
|
*
|
|
* @param mixed $notifierName
|
|
* @param mixed $event
|
|
* @param mixed $entryID
|
|
* @param mixed $data
|
|
*/
|
|
private function updateNotifier($notifierName, $event, $entryID, $data) {
|
|
$notifier = $this->notifiers[$notifierName] ?? null;
|
|
if (!is_object($notifier)) {
|
|
return;
|
|
}
|
|
if (method_exists($notifier, 'usePersistentStateLock') && !$notifier->usePersistentStateLock($event)) {
|
|
$notifier->update($event, $entryID, $data);
|
|
|
|
return;
|
|
}
|
|
|
|
$stateId = $GLOBALS['request_state_id'] ?? null;
|
|
if (!is_string($stateId) || $stateId === '' || ($GLOBALS['bus'] ?? null) !== $this) {
|
|
$notifier->update($event, $entryID, $data);
|
|
|
|
return;
|
|
}
|
|
|
|
// A notifier that notifies itself must not wait for its own lock
|
|
static $updating = [];
|
|
if (isset($updating[$notifierName])) {
|
|
$notifier->update($event, $entryID, $data);
|
|
|
|
return;
|
|
}
|
|
$updating[$notifierName] = true;
|
|
|
|
try {
|
|
$this->updateNotifierLocked($notifierName, $notifier, $event, $entryID, $data, $stateId);
|
|
}
|
|
finally {
|
|
unset($updating[$notifierName]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Run one notifier update under its persistent state lock.
|
|
*
|
|
* @param mixed $notifierName
|
|
* @param mixed $notifier
|
|
* @param mixed $event
|
|
* @param mixed $entryID
|
|
* @param mixed $data
|
|
* @param mixed $stateId
|
|
*/
|
|
private function updateNotifierLocked($notifierName, $notifier, $event, $entryID, $data, $stateId) {
|
|
$notifierState = new State('bus-notifier-' . hash('sha256', $stateId . "\0" . $notifierName));
|
|
if (!$notifierState->open()) {
|
|
if ($event === REQUEST_START) {
|
|
throw new RuntimeException('Unable to lock notifier state');
|
|
}
|
|
$notifier->update($event, $entryID, $data);
|
|
|
|
return;
|
|
}
|
|
|
|
try {
|
|
$version = isset($this->notifierVersions[$notifierName]) ? (int) $this->notifierVersions[$notifierName] : 0;
|
|
$storedNotifier = $notifierState->read('notifier');
|
|
$storedVersion = (int) $notifierState->read('version');
|
|
if (is_object($storedNotifier) && get_class($storedNotifier) === get_class($notifier) && $storedVersion > $version) {
|
|
$notifier = $this->mergeNotifierPersistentState($notifier, $storedNotifier);
|
|
$version = $storedVersion;
|
|
$this->notifiers[$notifierName] = $notifier;
|
|
}
|
|
|
|
$before = serialize($this->persistentNotifierCopy($notifier));
|
|
|
|
try {
|
|
$notifier->update($event, $entryID, $data);
|
|
}
|
|
finally {
|
|
$persistentNotifier = $this->persistentNotifierCopy($notifier);
|
|
if (serialize($persistentNotifier) !== $before) {
|
|
++$version;
|
|
$notifierState->write('notifier', $persistentNotifier, false);
|
|
$notifierState->write('version', $version, false);
|
|
$notifierState->flush();
|
|
}
|
|
$this->notifierVersions[$notifierName] = $version;
|
|
}
|
|
}
|
|
finally {
|
|
$notifierState->close();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Clone a notifier in the same reset form stored with the Bus.
|
|
*
|
|
* @param mixed $notifier
|
|
*/
|
|
private function persistentNotifierCopy($notifier) {
|
|
if (!is_object($notifier)) {
|
|
return $notifier;
|
|
}
|
|
$copy = unserialize(serialize($notifier));
|
|
$copy->reset();
|
|
|
|
return $copy;
|
|
}
|
|
|
|
/**
|
|
* Import reset-form state without replacing request-local notifier data.
|
|
*
|
|
* @param mixed $local
|
|
* @param mixed $persistent
|
|
*/
|
|
private function mergeNotifierPersistentState($local, $persistent) {
|
|
if (!is_object($local) || !is_object($persistent) || get_class($local) !== get_class($persistent)) {
|
|
return $local;
|
|
}
|
|
|
|
$resetLocal = $this->persistentNotifierCopy($local);
|
|
$reflection = new ReflectionObject($local);
|
|
do {
|
|
foreach ($reflection->getProperties() as $property) {
|
|
if ($property->getDeclaringClass()->getName() !== $reflection->getName() ||
|
|
$property->isStatic() || $property->isReadOnly()) {
|
|
continue;
|
|
}
|
|
$localInitialized = $property->isInitialized($local);
|
|
$resetInitialized = $property->isInitialized($resetLocal);
|
|
if ($localInitialized !== $resetInitialized ||
|
|
($localInitialized && serialize($property->getValue($local)) !== serialize($property->getValue($resetLocal)))) {
|
|
continue;
|
|
}
|
|
if ($property->isInitialized($persistent)) {
|
|
$property->setValue($local, $property->getValue($persistent));
|
|
}
|
|
}
|
|
$reflection = $reflection->getParentClass();
|
|
}
|
|
while ($reflection !== false);
|
|
|
|
return $local;
|
|
}
|
|
|
|
/**
|
|
* Add response data to collected XML response.
|
|
*
|
|
* This function is called by all modules when they want to output some XML response data
|
|
* to the client. It simply copies the XML response data to an internal structure. The data
|
|
* is later collected via getData() when all modules are done processing, after which the
|
|
* data is sent to the client.
|
|
*
|
|
* The data added is not checked for validity, and is therefore completely free-form. However,
|
|
*
|
|
* @param array $data data to be added
|
|
*/
|
|
public function addData($data) {
|
|
foreach ($data as $moduleName => $module) {
|
|
if (isset($this->responseData[$moduleName])) {
|
|
foreach ($module as $moduleId => $action) {
|
|
if (isset($this->responseData[$moduleName][$moduleId])) {
|
|
foreach ($action as $actionType => $actionData) {
|
|
if (isset($this->responseData[$moduleName][$moduleId][$actionType])) {
|
|
if ($this->responseData[$moduleName][$moduleId][$actionType] == $actionData) {
|
|
// in future we can think something about this but for now we throw exception
|
|
throw new BusException(_("Can't add data to bus, same action type is present in bus"));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
$this->responseData = array_replace_recursive($this->responseData, $data);
|
|
}
|
|
|
|
/**
|
|
* Function which returns the data stored via addData().
|
|
*
|
|
* @return array|string response data, or an encoded empty response when no data is available
|
|
*/
|
|
public function getData() {
|
|
if (empty($this->responseData)) {
|
|
// Helps to avoid unnecessary bus error message on client side when gromox is stopped.
|
|
// we just error_log and send empty grommunio array on client side
|
|
error_log(_("Response data requested from bus but it doesn't have any data."));
|
|
|
|
return json_encode(["grommunio" => []]);
|
|
}
|
|
|
|
return $this->responseData;
|
|
}
|
|
|
|
/**
|
|
* Merge persistent registrations from a concurrently completed request.
|
|
* Existing notifier instances belong to the newer on-disk state; notifier
|
|
* names and registrations only present in the other request are additive.
|
|
*
|
|
* @param Bus $bus request-local bus state
|
|
* @param null|Bus $baseBus state from which the request started
|
|
* @param bool $reset clear transient notifier and response data
|
|
* @param bool $preserveTransient retain request-local notifier data while importing
|
|
*/
|
|
public function mergePersistentState($bus, $baseBus = null, $reset = true, $preserveTransient = false) {
|
|
if (!$bus instanceof self) {
|
|
return;
|
|
}
|
|
|
|
if (!is_array($this->notifiers)) {
|
|
$this->notifiers = [];
|
|
}
|
|
if (!is_array($this->notifierVersions)) {
|
|
$this->notifierVersions = [];
|
|
}
|
|
if (is_array($bus->notifiers)) {
|
|
foreach ($bus->notifiers as $notifierName => $notifier) {
|
|
$currentVersion = isset($this->notifierVersions[$notifierName]) ? (int) $this->notifierVersions[$notifierName] : 0;
|
|
$otherVersion = isset($bus->notifierVersions[$notifierName]) ? (int) $bus->notifierVersions[$notifierName] : 0;
|
|
if (!isset($this->notifiers[$notifierName])) {
|
|
$this->notifiers[$notifierName] = $notifier;
|
|
$this->notifierVersions[$notifierName] = $otherVersion;
|
|
}
|
|
elseif ($otherVersion > $currentVersion) {
|
|
$this->notifiers[$notifierName] = $preserveTransient ?
|
|
$this->mergeNotifierPersistentState($this->notifiers[$notifierName], $notifier) : $notifier;
|
|
$this->notifierVersions[$notifierName] = $otherVersion;
|
|
}
|
|
elseif ($otherVersion === $currentVersion && $baseBus instanceof self && isset($baseBus->notifiers[$notifierName])) {
|
|
$baseNotifier = serialize($baseBus->notifiers[$notifierName]);
|
|
if (serialize($this->notifiers[$notifierName]) === $baseNotifier && serialize($notifier) !== $baseNotifier) {
|
|
$this->notifiers[$notifierName] = $preserveTransient ?
|
|
$this->mergeNotifierPersistentState($this->notifiers[$notifierName], $notifier) : $notifier;
|
|
$this->notifierVersions[$notifierName] = $otherVersion;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
$baseRegistrations = $baseBus instanceof self ? $baseBus->registeredNotifiers : [];
|
|
$baseStoreRegistrations = $baseBus instanceof self ? $baseBus->registeredStoreNotifiers : [];
|
|
$this->mergeRegistrations($bus->registeredNotifiers, $baseRegistrations, false);
|
|
$this->mergeRegistrations($bus->registeredStoreNotifiers, $baseStoreRegistrations, true);
|
|
if ($reset) {
|
|
$this->reset();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Publish new registrations before module execution and import registrations
|
|
* published by requests that started from the same state snapshot.
|
|
*/
|
|
public function synchronizePersistentState() {
|
|
$stateId = $GLOBALS['request_state_id'] ?? null;
|
|
if (!is_string($stateId) || $stateId === '' || ($GLOBALS['bus'] ?? null) !== $this) {
|
|
return true;
|
|
}
|
|
|
|
$registrationState = new State('bus-registration-' . hash('sha256', $stateId));
|
|
if (!$registrationState->open()) {
|
|
return false;
|
|
}
|
|
$version = (int) $registrationState->read('version');
|
|
if ($this->synchronizedRegistrationVersion === $version && !$this->registrationStateDirty) {
|
|
$registrationState->close();
|
|
|
|
return true;
|
|
}
|
|
|
|
$state = new State($stateId);
|
|
if (!$state->open()) {
|
|
$registrationState->close();
|
|
|
|
return false;
|
|
}
|
|
|
|
try {
|
|
$currentBus = $state->read('bus');
|
|
if ($currentBus instanceof self) {
|
|
$currentBus->reset();
|
|
}
|
|
else {
|
|
$currentBus = unserialize(serialize($this));
|
|
$currentBus->reset();
|
|
}
|
|
|
|
$before = serialize($currentBus);
|
|
$beforeTopology = $currentBus->persistentTopologyFingerprint();
|
|
$baseBus = $GLOBALS['request_bus_base'] ?? null;
|
|
$localCopy = clone $this;
|
|
$localCopy->responseData = [];
|
|
foreach ($this->notifiers as $notifierName => $notifier) {
|
|
$localCopy->notifiers[$notifierName] = $this->persistentNotifierCopy($notifier);
|
|
}
|
|
$currentBus->mergePersistentState($localCopy, $baseBus instanceof self ? $baseBus : null);
|
|
$topology = $currentBus->persistentTopologyFingerprint();
|
|
if ($topology !== $beforeTopology) {
|
|
++$version;
|
|
$registrationState->write('version', $version);
|
|
}
|
|
$currentBus->synchronizedRegistrationVersion = $version;
|
|
$currentBus->registrationStateDirty = false;
|
|
if (serialize($currentBus) !== $before) {
|
|
$state->write('bus', $currentBus);
|
|
}
|
|
}
|
|
finally {
|
|
$state->close();
|
|
$registrationState->close();
|
|
}
|
|
|
|
$this->mergePersistentState($currentBus, null, false, true);
|
|
$this->synchronizedRegistrationVersion = $version;
|
|
$this->registrationStateDirty = false;
|
|
if ($baseBus instanceof self) {
|
|
$baseBus->mergePublishedBaseline($currentBus);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Extend a request's merge baseline with registrations it has observed.
|
|
*
|
|
* @param mixed $bus
|
|
*/
|
|
private function mergePublishedBaseline($bus) {
|
|
if (!$bus instanceof self) {
|
|
return;
|
|
}
|
|
foreach ($bus->notifiers as $notifierName => $notifier) {
|
|
$currentVersion = isset($this->notifierVersions[$notifierName]) ? (int) $this->notifierVersions[$notifierName] : 0;
|
|
$otherVersion = isset($bus->notifierVersions[$notifierName]) ? (int) $bus->notifierVersions[$notifierName] : 0;
|
|
if (!isset($this->notifiers[$notifierName]) || $otherVersion > $currentVersion) {
|
|
$this->notifiers[$notifierName] = unserialize(serialize($notifier));
|
|
$this->notifierVersions[$notifierName] = $otherVersion;
|
|
}
|
|
}
|
|
$baseRegistrations = $this->registeredNotifiers;
|
|
$baseStoreRegistrations = $this->registeredStoreNotifiers;
|
|
$this->mergeRegistrations($bus->registeredNotifiers, $baseRegistrations, false);
|
|
$this->mergeRegistrations($bus->registeredStoreNotifiers, $baseStoreRegistrations, true);
|
|
$this->synchronizedRegistrationVersion = $bus->synchronizedRegistrationVersion;
|
|
$this->registrationStateDirty = false;
|
|
}
|
|
|
|
/**
|
|
* Fingerprint notifier names and their append-only registrations.
|
|
*/
|
|
private function persistentTopologyFingerprint() {
|
|
$notifierClasses = [];
|
|
if (is_array($this->notifiers)) {
|
|
foreach ($this->notifiers as $name => $notifier) {
|
|
$notifierClasses[$name] = is_object($notifier) ? get_class($notifier) : gettype($notifier);
|
|
}
|
|
}
|
|
ksort($notifierClasses);
|
|
|
|
return hash('sha256', serialize([
|
|
$notifierClasses,
|
|
$this->registeredNotifiers,
|
|
$this->registeredStoreNotifiers,
|
|
]));
|
|
}
|
|
|
|
/**
|
|
* Three-way merge one of the append-only registration lists.
|
|
*
|
|
* @param mixed $registrations registrations from another Bus
|
|
* @param mixed $baseRegistrations registrations from its starting state
|
|
* @param bool $store whether these are store registrations
|
|
*/
|
|
private function mergeRegistrations($registrations, $baseRegistrations, $store) {
|
|
if (!is_array($registrations)) {
|
|
return;
|
|
}
|
|
|
|
if ($store) {
|
|
$target = &$this->registeredStoreNotifiers;
|
|
}
|
|
else {
|
|
$target = &$this->registeredNotifiers;
|
|
}
|
|
if (!is_array($target)) {
|
|
$target = [];
|
|
}
|
|
|
|
$basePairs = [];
|
|
$baseEntries = [];
|
|
if (is_array($baseRegistrations)) {
|
|
foreach ($baseRegistrations as $registration) {
|
|
if (!is_array($registration) || !self::isRegistrationEntryid($registration['entryid'] ?? null)) {
|
|
continue;
|
|
}
|
|
$entryKey = $this->registrationEntryKey($registration['entryid']);
|
|
if (isset($baseEntries[$entryKey])) {
|
|
continue;
|
|
}
|
|
$baseEntries[$entryKey] = true;
|
|
foreach ($registration as $notifierName => $notifier) {
|
|
if ($notifierName !== 'entryid' && is_string($notifierName) && is_array($notifier) && isset($notifier['events'])) {
|
|
$basePairs[$this->registrationPairKey($entryKey, $notifierName)] = $notifier['events'];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
$targetEntries = [];
|
|
$targetPairs = [];
|
|
foreach ($target as $index => $registration) {
|
|
if (!is_array($registration) || !self::isRegistrationEntryid($registration['entryid'] ?? null)) {
|
|
continue;
|
|
}
|
|
$entryKey = $this->registrationEntryKey($registration['entryid']);
|
|
if (isset($targetEntries[$entryKey])) {
|
|
continue;
|
|
}
|
|
$targetEntries[$entryKey] = $index;
|
|
foreach ($registration as $notifierName => $notifier) {
|
|
if ($notifierName !== 'entryid' && is_string($notifierName) && is_array($notifier) && isset($notifier['events'])) {
|
|
$targetPairs[$this->registrationPairKey($entryKey, $notifierName)] = $notifier['events'];
|
|
}
|
|
}
|
|
}
|
|
|
|
$localEntries = [];
|
|
foreach ($registrations as $registration) {
|
|
if (!is_array($registration) || !self::isRegistrationEntryid($registration['entryid'] ?? null)) {
|
|
continue;
|
|
}
|
|
$entryKey = $this->registrationEntryKey($registration['entryid']);
|
|
if (isset($localEntries[$entryKey])) {
|
|
continue;
|
|
}
|
|
$localEntries[$entryKey] = true;
|
|
|
|
foreach ($registration as $notifierName => $notifier) {
|
|
if ($notifierName === 'entryid' || !is_string($notifierName) || !is_array($notifier) || !isset($notifier['events'])) {
|
|
continue;
|
|
}
|
|
|
|
$pairKey = $this->registrationPairKey($entryKey, $notifierName);
|
|
$baseHasPair = array_key_exists($pairKey, $basePairs);
|
|
if ($baseHasPair && $basePairs[$pairKey] === $notifier['events']) {
|
|
continue;
|
|
}
|
|
|
|
$targetHasPair = array_key_exists($pairKey, $targetPairs);
|
|
$targetMatchesBase = $targetHasPair === $baseHasPair &&
|
|
(!$baseHasPair || $targetPairs[$pairKey] === $basePairs[$pairKey]);
|
|
if (!$targetMatchesBase) {
|
|
continue;
|
|
}
|
|
|
|
if (!isset($targetEntries[$entryKey])) {
|
|
$target[] = ['entryid' => $registration['entryid']];
|
|
$targetEntries[$entryKey] = array_key_last($target);
|
|
}
|
|
$target[$targetEntries[$entryKey]][$notifierName] = ['events' => $notifier['events']];
|
|
$targetPairs[$pairKey] = $notifier['events'];
|
|
}
|
|
}
|
|
unset($target);
|
|
}
|
|
|
|
/**
|
|
* Registrations without an entryid never match a notification.
|
|
*/
|
|
private function pruneRegistrations() {
|
|
foreach (['registeredNotifiers', 'registeredStoreNotifiers'] as $list) {
|
|
if (!is_array($this->{$list})) {
|
|
$this->{$list} = [];
|
|
|
|
continue;
|
|
}
|
|
$this->{$list} = array_values(array_filter($this->{$list}, static fn ($registration) => is_array($registration) &&
|
|
self::isRegistrationEntryid($registration['entryid'] ?? null)));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param mixed $entryid
|
|
*
|
|
* @return bool
|
|
*/
|
|
private static function isRegistrationEntryid($entryid) {
|
|
return is_string($entryid) && $entryid !== '';
|
|
}
|
|
|
|
/**
|
|
* Build a type-safe index key for an entryid.
|
|
*
|
|
* @param mixed $entryid
|
|
*/
|
|
private function registrationEntryKey($entryid) {
|
|
return 'e' . strlen($entryid) . ':' . $entryid;
|
|
}
|
|
|
|
/**
|
|
* Build a type-safe index key for an entryid/notifier pair.
|
|
*
|
|
* @param mixed $entryKey
|
|
* @param mixed $notifierName
|
|
*/
|
|
private function registrationPairKey($entryKey, $notifierName) {
|
|
return $entryKey . '|n' . strlen($notifierName) . ':' . $notifierName;
|
|
}
|
|
|
|
/**
|
|
* Reset the bus state and response data, and send resets to all notifier.
|
|
*
|
|
* Since the bus is a global, persistent object, the reset() function is called before or after
|
|
* processing. This makes sure that the on-disk serialized representation of the bus object is as
|
|
* small as possible.
|
|
*/
|
|
public function reset() {
|
|
$this->responseData = [];
|
|
$this->pruneRegistrations();
|
|
|
|
foreach ($this->notifiers as $key => $notifier) {
|
|
$this->notifiers[$key]->reset();
|
|
}
|
|
}
|
|
}
|