websocket refactoring

This commit is contained in:
Yuri Kuznetsov
2021-08-02 16:59:43 +03:00
parent 3d1245bee6
commit 58dbd7868a
9 changed files with 151 additions and 14 deletions
@@ -198,16 +198,6 @@ class DefaultBinding implements BindingProcessor
'externalAccountClientManager'
);
$binder->bindImplementation(
'Espo\\Core\\WebSocket\\Sender',
'Espo\\Core\\WebSocket\\ZeroMQSender'
);
$binder->bindImplementation(
'Espo\\Core\\WebSocket\\Subscriber',
'Espo\\Core\\WebSocket\\ZeroMQSubscriber'
);
$binder->bindImplementation(
'Espo\\Core\\Acl\\Table\\TableFactory',
'Espo\\Core\\Acl\\Table\\DefaultTableFactory'
@@ -0,0 +1,66 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2021 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
* Website: https://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\WebSocket;
use Espo\Core\InjectableFactory;
use Espo\Core\Utils\Config;
use Espo\Core\Utils\Metadata;
use Espo\Core\Exceptions\Error;
class SenderFactory
{
private $injectableFactory;
private $config;
private $metadata;
private const DEFAULT_MESSAGER = 'ZeroMQ';
public function __construct(InjectableFactory $injectableFactory, Config $config, Metadata $metadata)
{
$this->injectableFactory = $injectableFactory;
$this->config = $config;
$this->metadata = $metadata;
}
public function create(): Sender
{
$messager = $this->config->get('webSocketMessager') ?? self::DEFAULT_MESSAGER;
$className = $this->metadata->get(['app', 'webSocket', 'messagers', $messager, 'senderClassName']);
if (!$className) {
throw new Error("No sender for messager '{$messager}'.");
}
return $this->injectableFactory->create($className);
}
}
@@ -46,6 +46,9 @@ use Ratchet\Wamp\WampServer;
*/
class ServerStarter
{
/**
* @var Subscriber
*/
private $subscriber;
private $categoriesData;
@@ -62,9 +65,9 @@ class ServerStarter
private $metadata;
public function __construct(Subscriber $subscriber, Config $config, Metadata $metadata)
public function __construct(SubscriberFactory $subscriberFactory, Config $config, Metadata $metadata)
{
$this->subscriber = $subscriber;
$this->subscriber = $subscriberFactory->create();
$this->config = $config;
$this->metadata = $metadata;
@@ -35,13 +35,16 @@ use stdClass;
class Submission
{
/**
* @var Sender
*/
private $sender;
private $log;
public function __construct(Sender $sender, Log $log)
public function __construct(SenderFactory $senderFactory, Log $log)
{
$this->sender = $sender;
$this->sender = $senderFactory->create();
$this->log = $log;
}
@@ -0,0 +1,66 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2021 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
* Website: https://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\WebSocket;
use Espo\Core\InjectableFactory;
use Espo\Core\Utils\Config;
use Espo\Core\Utils\Metadata;
use Espo\Core\Exceptions\Error;
class SubscriberFactory
{
private $injectableFactory;
private $config;
private $metadata;
private const DEFAULT_MESSAGER = 'ZeroMQ';
public function __construct(InjectableFactory $injectableFactory, Config $config, Metadata $metadata)
{
$this->injectableFactory = $injectableFactory;
$this->config = $config;
$this->metadata = $metadata;
}
public function create(): Subscriber
{
$messager = $this->config->get('webSocketMessager') ?? self::DEFAULT_MESSAGER;
$className = $this->metadata->get(['app', 'webSocket', 'messagers', $messager, 'subscriberClassName']);
if (!$className) {
throw new Error("No subscriber for messager '{$messager}'.");
}
return $this->injectableFactory->create($className);
}
}
@@ -154,6 +154,7 @@ return [
'noteEditThresholdPeriod' => '7 days',
'emailForceUseExternalClient' => false,
'useWebSocket' => false,
'webSocketMessager' => 'ZeroMQ',
'auth2FAMethodList' => ['Totp'],
'personNameFormat' => 'firstLast',
'newNotificationCountInTitle' => false,
@@ -118,6 +118,7 @@ return [
'webSocketPort',
'webSocketZeroMQSubscriberDsn',
'webSocketZeroMQSubmissionDsn',
'webSocketMessager',
'actualDatabaseType',
'actualDatabaseVersion',
],
@@ -22,6 +22,7 @@
["app", "actions", "__ANY__", "implementationClassName"],
["app", "fieldProcessing"],
["app", "scheduledJobs"],
["app", "webSocket", "messagers"],
["selectDefs"],
["recordDefs"],
["aclDefs"],
@@ -9,5 +9,11 @@
"paramList": ["scope", "id"],
"accessCheckCommand": "AclCheck --userId=:userId --scope=:scope --id=:id --action=stream"
}
},
"messagers": {
"ZeroMQ": {
"senderClassName": "Espo\\Core\\WebSocket\\ZeroMQSender",
"subscriberClassName": "Espo\\Core\\WebSocket\\ZeroMQSubscriber"
}
}
}