Merge pull request #18535 from nextcloud/enh/flow/newDispatcher

Use the new Events in Flow
This commit is contained in:
blizzz 2020-01-07 09:58:40 +01:00 committed by GitHub
commit 55fd157f15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 208 additions and 25 deletions

View File

@ -37,12 +37,16 @@ use OCA\WorkflowEngine\Helper\ScopeContext;
use OCA\WorkflowEngine\Service\RuleMatcher; use OCA\WorkflowEngine\Service\RuleMatcher;
use OCP\AppFramework\QueryException; use OCP\AppFramework\QueryException;
use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\Storage\IStorage; use OCP\Files\Storage\IStorage;
use OCP\IDBConnection; use OCP\IDBConnection;
use OCP\IL10N; use OCP\IL10N;
use OCP\ILogger; use OCP\ILogger;
use OCP\IServerContainer; use OCP\IServerContainer;
use OCP\IUserSession; use OCP\IUserSession;
use OCP\WorkflowEngine\Events\RegisterChecksEvent;
use OCP\WorkflowEngine\Events\RegisterEntitiesEvent;
use OCP\WorkflowEngine\Events\RegisterOperationsEvent;
use OCP\WorkflowEngine\ICheck; use OCP\WorkflowEngine\ICheck;
use OCP\WorkflowEngine\IComplexOperation; use OCP\WorkflowEngine\IComplexOperation;
use OCP\WorkflowEngine\IEntity; use OCP\WorkflowEngine\IEntity;
@ -50,7 +54,7 @@ use OCP\WorkflowEngine\IEntityEvent;
use OCP\WorkflowEngine\IManager; use OCP\WorkflowEngine\IManager;
use OCP\WorkflowEngine\IOperation; use OCP\WorkflowEngine\IOperation;
use OCP\WorkflowEngine\IRuleMatcher; use OCP\WorkflowEngine\IRuleMatcher;
use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface as LegacyDispatcher;
use Symfony\Component\EventDispatcher\GenericEvent; use Symfony\Component\EventDispatcher\GenericEvent;
class Manager implements IManager { class Manager implements IManager {
@ -79,8 +83,8 @@ class Manager implements IManager {
/** @var IL10N */ /** @var IL10N */
protected $l; protected $l;
/** @var EventDispatcherInterface */ /** @var LegacyDispatcher */
protected $eventDispatcher; protected $legacyEventDispatcher;
/** @var IEntity[] */ /** @var IEntity[] */
protected $registeredEntities = []; protected $registeredEntities = [];
@ -100,26 +104,26 @@ class Manager implements IManager {
/** @var IUserSession */ /** @var IUserSession */
protected $session; protected $session;
/** /** @var IEventDispatcher */
* @param IDBConnection $connection private $dispatcher;
* @param IServerContainer $container
* @param IL10N $l
*/
public function __construct( public function __construct(
IDBConnection $connection, IDBConnection $connection,
IServerContainer $container, IServerContainer $container,
IL10N $l, IL10N $l,
EventDispatcherInterface $eventDispatcher, LegacyDispatcher $eventDispatcher,
ILogger $logger, ILogger $logger,
IUserSession $session IUserSession $session,
IEventDispatcher $dispatcher
) { ) {
$this->connection = $connection; $this->connection = $connection;
$this->container = $container; $this->container = $container;
$this->l = $l; $this->l = $l;
$this->eventDispatcher = $eventDispatcher; $this->legacyEventDispatcher = $eventDispatcher;
$this->logger = $logger; $this->logger = $logger;
$this->operationsByScope = new CappedMemoryCache(64); $this->operationsByScope = new CappedMemoryCache(64);
$this->session = $session; $this->session = $session;
$this->dispatcher = $dispatcher;
} }
public function getRuleMatcher(): IRuleMatcher { public function getRuleMatcher(): IRuleMatcher {
@ -606,7 +610,8 @@ class Manager implements IManager {
* @return IEntity[] * @return IEntity[]
*/ */
public function getEntitiesList(): array { public function getEntitiesList(): array {
$this->eventDispatcher->dispatch(IManager::EVENT_NAME_REG_ENTITY, new GenericEvent($this)); $this->dispatcher->dispatchTyped(new RegisterEntitiesEvent($this));
$this->legacyEventDispatcher->dispatch(IManager::EVENT_NAME_REG_ENTITY, new GenericEvent($this));
return array_values(array_merge($this->getBuildInEntities(), $this->registeredEntities)); return array_values(array_merge($this->getBuildInEntities(), $this->registeredEntities));
} }
@ -615,7 +620,8 @@ class Manager implements IManager {
* @return IOperation[] * @return IOperation[]
*/ */
public function getOperatorList(): array { public function getOperatorList(): array {
$this->eventDispatcher->dispatch(IManager::EVENT_NAME_REG_OPERATION, new GenericEvent($this)); $this->dispatcher->dispatchTyped(new RegisterOperationsEvent($this));
$this->legacyEventDispatcher->dispatch(IManager::EVENT_NAME_REG_OPERATION, new GenericEvent($this));
return array_merge($this->getBuildInOperators(), $this->registeredOperators); return array_merge($this->getBuildInOperators(), $this->registeredOperators);
} }
@ -624,7 +630,8 @@ class Manager implements IManager {
* @return ICheck[] * @return ICheck[]
*/ */
public function getCheckList(): array { public function getCheckList(): array {
$this->eventDispatcher->dispatch(IManager::EVENT_NAME_REG_CHECK, new GenericEvent($this)); $this->dispatcher->dispatchTyped(new RegisterChecksEvent($this));
$this->legacyEventDispatcher->dispatch(IManager::EVENT_NAME_REG_CHECK, new GenericEvent($this));
return array_merge($this->getBuildInChecks(), $this->registeredChecks); return array_merge($this->getBuildInChecks(), $this->registeredChecks);
} }

View File

@ -26,6 +26,7 @@ use OC\L10N\L10N;
use OCA\WorkflowEngine\Entity\File; use OCA\WorkflowEngine\Entity\File;
use OCA\WorkflowEngine\Helper\ScopeContext; use OCA\WorkflowEngine\Helper\ScopeContext;
use OCA\WorkflowEngine\Manager; use OCA\WorkflowEngine\Manager;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\IRootFolder; use OCP\Files\IRootFolder;
use OCP\IDBConnection; use OCP\IDBConnection;
use OCP\IL10N; use OCP\IL10N;
@ -57,13 +58,15 @@ class ManagerTest extends TestCase {
/** @var \PHPUnit\Framework\MockObject\MockObject|ILogger */ /** @var \PHPUnit\Framework\MockObject\MockObject|ILogger */
protected $logger; protected $logger;
/** @var \PHPUnit\Framework\MockObject\MockObject|EventDispatcherInterface */ /** @var \PHPUnit\Framework\MockObject\MockObject|EventDispatcherInterface */
protected $eventDispatcher; protected $legacyDispatcher;
/** @var MockObject|IServerContainer */ /** @var MockObject|IServerContainer */
protected $container; protected $container;
/** @var MockObject|IUserSession */ /** @var MockObject|IUserSession */
protected $session; protected $session;
/** @var MockObject|L10N */ /** @var MockObject|L10N */
protected $l; protected $l;
/** @var MockObject|IEventDispatcher */
protected $dispatcher;
protected function setUp(): void { protected function setUp(): void {
parent::setUp(); parent::setUp();
@ -77,17 +80,19 @@ class ManagerTest extends TestCase {
return vsprintf($text, $parameters); return vsprintf($text, $parameters);
})); }));
$this->eventDispatcher = $this->createMock(EventDispatcherInterface::class); $this->legacyDispatcher = $this->createMock(EventDispatcherInterface::class);
$this->logger = $this->createMock(ILogger::class); $this->logger = $this->createMock(ILogger::class);
$this->session = $this->createMock(IUserSession::class); $this->session = $this->createMock(IUserSession::class);
$this->dispatcher = $this->createMock(IEventDispatcher::class);
$this->manager = new Manager( $this->manager = new Manager(
\OC::$server->getDatabaseConnection(), \OC::$server->getDatabaseConnection(),
$this->container, $this->container,
$this->l, $this->l,
$this->eventDispatcher, $this->legacyDispatcher,
$this->logger, $this->logger,
$this->session $this->session,
$this->dispatcher
); );
$this->clearTables(); $this->clearTables();
} }
@ -402,7 +407,7 @@ class ManagerTest extends TestCase {
/** @var MockObject|IEntity $extraEntity */ /** @var MockObject|IEntity $extraEntity */
$extraEntity = $this->createMock(IEntity::class); $extraEntity = $this->createMock(IEntity::class);
$this->eventDispatcher->expects($this->once()) $this->legacyDispatcher->expects($this->once())
->method('dispatch') ->method('dispatch')
->with('OCP\WorkflowEngine::registerEntities', $this->anything()) ->with('OCP\WorkflowEngine::registerEntities', $this->anything())
->willReturnCallback(function() use ($extraEntity) { ->willReturnCallback(function() use ($extraEntity) {

View File

@ -489,6 +489,9 @@ return array(
'OCP\\WorkflowEngine\\EntityContext\\IDisplayText' => $baseDir . '/lib/public/WorkflowEngine/EntityContext/IDisplayText.php', 'OCP\\WorkflowEngine\\EntityContext\\IDisplayText' => $baseDir . '/lib/public/WorkflowEngine/EntityContext/IDisplayText.php',
'OCP\\WorkflowEngine\\EntityContext\\IIcon' => $baseDir . '/lib/public/WorkflowEngine/EntityContext/IIcon.php', 'OCP\\WorkflowEngine\\EntityContext\\IIcon' => $baseDir . '/lib/public/WorkflowEngine/EntityContext/IIcon.php',
'OCP\\WorkflowEngine\\EntityContext\\IUrl' => $baseDir . '/lib/public/WorkflowEngine/EntityContext/IUrl.php', 'OCP\\WorkflowEngine\\EntityContext\\IUrl' => $baseDir . '/lib/public/WorkflowEngine/EntityContext/IUrl.php',
'OCP\\WorkflowEngine\\Events\\RegisterChecksEvent' => $baseDir . '/lib/public/WorkflowEngine/Events/RegisterChecksEvent.php',
'OCP\\WorkflowEngine\\Events\\RegisterEntitiesEvent' => $baseDir . '/lib/public/WorkflowEngine/Events/RegisterEntitiesEvent.php',
'OCP\\WorkflowEngine\\Events\\RegisterOperationsEvent' => $baseDir . '/lib/public/WorkflowEngine/Events/RegisterOperationsEvent.php',
'OCP\\WorkflowEngine\\GenericEntityEvent' => $baseDir . '/lib/public/WorkflowEngine/GenericEntityEvent.php', 'OCP\\WorkflowEngine\\GenericEntityEvent' => $baseDir . '/lib/public/WorkflowEngine/GenericEntityEvent.php',
'OCP\\WorkflowEngine\\ICheck' => $baseDir . '/lib/public/WorkflowEngine/ICheck.php', 'OCP\\WorkflowEngine\\ICheck' => $baseDir . '/lib/public/WorkflowEngine/ICheck.php',
'OCP\\WorkflowEngine\\IComplexOperation' => $baseDir . '/lib/public/WorkflowEngine/IComplexOperation.php', 'OCP\\WorkflowEngine\\IComplexOperation' => $baseDir . '/lib/public/WorkflowEngine/IComplexOperation.php',

View File

@ -518,6 +518,9 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
'OCP\\WorkflowEngine\\EntityContext\\IDisplayText' => __DIR__ . '/../../..' . '/lib/public/WorkflowEngine/EntityContext/IDisplayText.php', 'OCP\\WorkflowEngine\\EntityContext\\IDisplayText' => __DIR__ . '/../../..' . '/lib/public/WorkflowEngine/EntityContext/IDisplayText.php',
'OCP\\WorkflowEngine\\EntityContext\\IIcon' => __DIR__ . '/../../..' . '/lib/public/WorkflowEngine/EntityContext/IIcon.php', 'OCP\\WorkflowEngine\\EntityContext\\IIcon' => __DIR__ . '/../../..' . '/lib/public/WorkflowEngine/EntityContext/IIcon.php',
'OCP\\WorkflowEngine\\EntityContext\\IUrl' => __DIR__ . '/../../..' . '/lib/public/WorkflowEngine/EntityContext/IUrl.php', 'OCP\\WorkflowEngine\\EntityContext\\IUrl' => __DIR__ . '/../../..' . '/lib/public/WorkflowEngine/EntityContext/IUrl.php',
'OCP\\WorkflowEngine\\Events\\RegisterChecksEvent' => __DIR__ . '/../../..' . '/lib/public/WorkflowEngine/Events/RegisterChecksEvent.php',
'OCP\\WorkflowEngine\\Events\\RegisterEntitiesEvent' => __DIR__ . '/../../..' . '/lib/public/WorkflowEngine/Events/RegisterEntitiesEvent.php',
'OCP\\WorkflowEngine\\Events\\RegisterOperationsEvent' => __DIR__ . '/../../..' . '/lib/public/WorkflowEngine/Events/RegisterOperationsEvent.php',
'OCP\\WorkflowEngine\\GenericEntityEvent' => __DIR__ . '/../../..' . '/lib/public/WorkflowEngine/GenericEntityEvent.php', 'OCP\\WorkflowEngine\\GenericEntityEvent' => __DIR__ . '/../../..' . '/lib/public/WorkflowEngine/GenericEntityEvent.php',
'OCP\\WorkflowEngine\\ICheck' => __DIR__ . '/../../..' . '/lib/public/WorkflowEngine/ICheck.php', 'OCP\\WorkflowEngine\\ICheck' => __DIR__ . '/../../..' . '/lib/public/WorkflowEngine/ICheck.php',
'OCP\\WorkflowEngine\\IComplexOperation' => __DIR__ . '/../../..' . '/lib/public/WorkflowEngine/IComplexOperation.php', 'OCP\\WorkflowEngine\\IComplexOperation' => __DIR__ . '/../../..' . '/lib/public/WorkflowEngine/IComplexOperation.php',

View File

@ -0,0 +1,54 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2019, Roeland Jago Douma <roeland@famdouma.nl>
*
* @author Roeland Jago Douma <roeland@famdouma.nl>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCP\WorkflowEngine\Events;
use OCP\EventDispatcher\Event;
use OCP\WorkflowEngine\ICheck;
use OCP\WorkflowEngine\IManager;
/**
* @since 18.0.0
*/
class RegisterChecksEvent extends Event {
/** @var IManager */
private $manager;
/**
* @since 18.0.0
*/
public function __construct(IManager $manager) {
parent::__construct();
$this->manager = $manager;
}
/**
* @since 18.0.0
*/
public function registerCheck(ICheck $check): void {
$this->manager->registerCheck($check);
}
}

View File

@ -0,0 +1,54 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2019, Roeland Jago Douma <roeland@famdouma.nl>
*
* @author Roeland Jago Douma <roeland@famdouma.nl>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCP\WorkflowEngine\Events;
use OCP\EventDispatcher\Event;
use OCP\WorkflowEngine\IEntity;
use OCP\WorkflowEngine\IManager;
/**
* @since 18.0.0
*/
class RegisterEntitiesEvent extends Event {
/** @var IManager */
private $manager;
/**
* @since 18.0.0
*/
public function __construct(IManager $manager) {
parent::__construct();
$this->manager = $manager;
}
/**
* @since 18.0.0
*/
public function registerEntity(IEntity $entity): void {
$this->manager->registerEntity($entity);
}
}

View File

@ -0,0 +1,54 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2019, Roeland Jago Douma <roeland@famdouma.nl>
*
* @author Roeland Jago Douma <roeland@famdouma.nl>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCP\WorkflowEngine\Events;
use OCP\EventDispatcher\Event;
use OCP\WorkflowEngine\IManager;
use OCP\WorkflowEngine\IOperation;
/**
* @since 18.0.0
*/
class RegisterOperationsEvent extends Event {
/** @var IManager */
private $manager;
/**
* @since 18.0.0
*/
public function __construct(IManager $manager) {
parent::__construct();
$this->manager = $manager;
}
/**
* @since 18.0.0
*/
public function registerOperation(IOperation $operation): void {
$this->manager->registerOperation($operation);
}
}

View File

@ -35,29 +35,32 @@ interface IManager {
const SCOPE_ADMIN = 0; const SCOPE_ADMIN = 0;
const SCOPE_USER = 1; const SCOPE_USER = 1;
/**
* @depreacted Will be removed in NC19. Use the dedicated events in OCP\WorkflowEngine\Events
*/
const EVENT_NAME_REG_OPERATION = 'OCP\WorkflowEngine::registerOperations'; const EVENT_NAME_REG_OPERATION = 'OCP\WorkflowEngine::registerOperations';
const EVENT_NAME_REG_ENTITY = 'OCP\WorkflowEngine::registerEntities'; const EVENT_NAME_REG_ENTITY = 'OCP\WorkflowEngine::registerEntities';
const EVENT_NAME_REG_CHECK = 'OCP\WorkflowEngine::registerChecks'; const EVENT_NAME_REG_CHECK = 'OCP\WorkflowEngine::registerChecks';
/** /**
* Listen to `\OCP\WorkflowEngine::EVENT_NAME_REG_ENTITY` at the * Listen to `OCP\WorkflowEngine\Events\RegisterEntitiesEvent` at the
* EventDispatcher for registering your entities. * IEventDispatcher for registering your entities.
* *
* @since 18.0.0 * @since 18.0.0
*/ */
public function registerEntity(IEntity $entity): void; public function registerEntity(IEntity $entity): void;
/** /**
* Listen to `\OCP\WorkflowEngine::EVENT_NAME_REG_OPERATION` at the * Listen to `OCP\WorkflowEngine\Events\RegisterOperationsEvent` at the
* EventDispatcher for registering your operators. * IEventDispatcher for registering your operators.
* *
* @since 18.0.0 * @since 18.0.0
*/ */
public function registerOperation(IOperation $operator): void; public function registerOperation(IOperation $operator): void;
/** /**
* Listen to `\OCP\WorkflowEngine::EVENT_NAME_REG_CHECK` at the * Listen to `OCP\WorkflowEngine\Events\RegisterChecksEvent` at the
* EventDispatcher for registering your operators. * IEventDispatcher for registering your operators.
* *
* @since 18.0.0 * @since 18.0.0
*/ */