Skip to content

Commit 55fd157

Browse files
authored
Merge pull request #18535 from nextcloud/enh/flow/newDispatcher
Use the new Events in Flow
2 parents 1b8f816 + 25d4f32 commit 55fd157

8 files changed

Lines changed: 208 additions & 25 deletions

File tree

apps/workflowengine/lib/Manager.php

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,24 @@
3737
use OCA\WorkflowEngine\Service\RuleMatcher;
3838
use OCP\AppFramework\QueryException;
3939
use OCP\DB\QueryBuilder\IQueryBuilder;
40+
use OCP\EventDispatcher\IEventDispatcher;
4041
use OCP\Files\Storage\IStorage;
4142
use OCP\IDBConnection;
4243
use OCP\IL10N;
4344
use OCP\ILogger;
4445
use OCP\IServerContainer;
4546
use OCP\IUserSession;
47+
use OCP\WorkflowEngine\Events\RegisterChecksEvent;
48+
use OCP\WorkflowEngine\Events\RegisterEntitiesEvent;
49+
use OCP\WorkflowEngine\Events\RegisterOperationsEvent;
4650
use OCP\WorkflowEngine\ICheck;
4751
use OCP\WorkflowEngine\IComplexOperation;
4852
use OCP\WorkflowEngine\IEntity;
4953
use OCP\WorkflowEngine\IEntityEvent;
5054
use OCP\WorkflowEngine\IManager;
5155
use OCP\WorkflowEngine\IOperation;
5256
use OCP\WorkflowEngine\IRuleMatcher;
53-
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
57+
use Symfony\Component\EventDispatcher\EventDispatcherInterface as LegacyDispatcher;
5458
use Symfony\Component\EventDispatcher\GenericEvent;
5559

5660
class Manager implements IManager {
@@ -79,8 +83,8 @@ class Manager implements IManager {
7983
/** @var IL10N */
8084
protected $l;
8185

82-
/** @var EventDispatcherInterface */
83-
protected $eventDispatcher;
86+
/** @var LegacyDispatcher */
87+
protected $legacyEventDispatcher;
8488

8589
/** @var IEntity[] */
8690
protected $registeredEntities = [];
@@ -100,26 +104,26 @@ class Manager implements IManager {
100104
/** @var IUserSession */
101105
protected $session;
102106

103-
/**
104-
* @param IDBConnection $connection
105-
* @param IServerContainer $container
106-
* @param IL10N $l
107-
*/
107+
/** @var IEventDispatcher */
108+
private $dispatcher;
109+
108110
public function __construct(
109111
IDBConnection $connection,
110112
IServerContainer $container,
111113
IL10N $l,
112-
EventDispatcherInterface $eventDispatcher,
114+
LegacyDispatcher $eventDispatcher,
113115
ILogger $logger,
114-
IUserSession $session
116+
IUserSession $session,
117+
IEventDispatcher $dispatcher
115118
) {
116119
$this->connection = $connection;
117120
$this->container = $container;
118121
$this->l = $l;
119-
$this->eventDispatcher = $eventDispatcher;
122+
$this->legacyEventDispatcher = $eventDispatcher;
120123
$this->logger = $logger;
121124
$this->operationsByScope = new CappedMemoryCache(64);
122125
$this->session = $session;
126+
$this->dispatcher = $dispatcher;
123127
}
124128

125129
public function getRuleMatcher(): IRuleMatcher {
@@ -606,7 +610,8 @@ public function formatOperation(array $operation): array {
606610
* @return IEntity[]
607611
*/
608612
public function getEntitiesList(): array {
609-
$this->eventDispatcher->dispatch(IManager::EVENT_NAME_REG_ENTITY, new GenericEvent($this));
613+
$this->dispatcher->dispatchTyped(new RegisterEntitiesEvent($this));
614+
$this->legacyEventDispatcher->dispatch(IManager::EVENT_NAME_REG_ENTITY, new GenericEvent($this));
610615

611616
return array_values(array_merge($this->getBuildInEntities(), $this->registeredEntities));
612617
}
@@ -615,7 +620,8 @@ public function getEntitiesList(): array {
615620
* @return IOperation[]
616621
*/
617622
public function getOperatorList(): array {
618-
$this->eventDispatcher->dispatch(IManager::EVENT_NAME_REG_OPERATION, new GenericEvent($this));
623+
$this->dispatcher->dispatchTyped(new RegisterOperationsEvent($this));
624+
$this->legacyEventDispatcher->dispatch(IManager::EVENT_NAME_REG_OPERATION, new GenericEvent($this));
619625

620626
return array_merge($this->getBuildInOperators(), $this->registeredOperators);
621627
}
@@ -624,7 +630,8 @@ public function getOperatorList(): array {
624630
* @return ICheck[]
625631
*/
626632
public function getCheckList(): array {
627-
$this->eventDispatcher->dispatch(IManager::EVENT_NAME_REG_CHECK, new GenericEvent($this));
633+
$this->dispatcher->dispatchTyped(new RegisterChecksEvent($this));
634+
$this->legacyEventDispatcher->dispatch(IManager::EVENT_NAME_REG_CHECK, new GenericEvent($this));
628635

629636
return array_merge($this->getBuildInChecks(), $this->registeredChecks);
630637
}

apps/workflowengine/tests/ManagerTest.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use OCA\WorkflowEngine\Entity\File;
2727
use OCA\WorkflowEngine\Helper\ScopeContext;
2828
use OCA\WorkflowEngine\Manager;
29+
use OCP\EventDispatcher\IEventDispatcher;
2930
use OCP\Files\IRootFolder;
3031
use OCP\IDBConnection;
3132
use OCP\IL10N;
@@ -57,13 +58,15 @@ class ManagerTest extends TestCase {
5758
/** @var \PHPUnit\Framework\MockObject\MockObject|ILogger */
5859
protected $logger;
5960
/** @var \PHPUnit\Framework\MockObject\MockObject|EventDispatcherInterface */
60-
protected $eventDispatcher;
61+
protected $legacyDispatcher;
6162
/** @var MockObject|IServerContainer */
6263
protected $container;
6364
/** @var MockObject|IUserSession */
6465
protected $session;
6566
/** @var MockObject|L10N */
6667
protected $l;
68+
/** @var MockObject|IEventDispatcher */
69+
protected $dispatcher;
6770

6871
protected function setUp(): void {
6972
parent::setUp();
@@ -77,17 +80,19 @@ protected function setUp(): void {
7780
return vsprintf($text, $parameters);
7881
}));
7982

80-
$this->eventDispatcher = $this->createMock(EventDispatcherInterface::class);
83+
$this->legacyDispatcher = $this->createMock(EventDispatcherInterface::class);
8184
$this->logger = $this->createMock(ILogger::class);
8285
$this->session = $this->createMock(IUserSession::class);
86+
$this->dispatcher = $this->createMock(IEventDispatcher::class);
8387

8488
$this->manager = new Manager(
8589
\OC::$server->getDatabaseConnection(),
8690
$this->container,
8791
$this->l,
88-
$this->eventDispatcher,
92+
$this->legacyDispatcher,
8993
$this->logger,
90-
$this->session
94+
$this->session,
95+
$this->dispatcher
9196
);
9297
$this->clearTables();
9398
}
@@ -402,7 +407,7 @@ public function testGetEntitiesList() {
402407
/** @var MockObject|IEntity $extraEntity */
403408
$extraEntity = $this->createMock(IEntity::class);
404409

405-
$this->eventDispatcher->expects($this->once())
410+
$this->legacyDispatcher->expects($this->once())
406411
->method('dispatch')
407412
->with('OCP\WorkflowEngine::registerEntities', $this->anything())
408413
->willReturnCallback(function() use ($extraEntity) {

lib/composer/composer/autoload_classmap.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,9 @@
489489
'OCP\\WorkflowEngine\\EntityContext\\IDisplayText' => $baseDir . '/lib/public/WorkflowEngine/EntityContext/IDisplayText.php',
490490
'OCP\\WorkflowEngine\\EntityContext\\IIcon' => $baseDir . '/lib/public/WorkflowEngine/EntityContext/IIcon.php',
491491
'OCP\\WorkflowEngine\\EntityContext\\IUrl' => $baseDir . '/lib/public/WorkflowEngine/EntityContext/IUrl.php',
492+
'OCP\\WorkflowEngine\\Events\\RegisterChecksEvent' => $baseDir . '/lib/public/WorkflowEngine/Events/RegisterChecksEvent.php',
493+
'OCP\\WorkflowEngine\\Events\\RegisterEntitiesEvent' => $baseDir . '/lib/public/WorkflowEngine/Events/RegisterEntitiesEvent.php',
494+
'OCP\\WorkflowEngine\\Events\\RegisterOperationsEvent' => $baseDir . '/lib/public/WorkflowEngine/Events/RegisterOperationsEvent.php',
492495
'OCP\\WorkflowEngine\\GenericEntityEvent' => $baseDir . '/lib/public/WorkflowEngine/GenericEntityEvent.php',
493496
'OCP\\WorkflowEngine\\ICheck' => $baseDir . '/lib/public/WorkflowEngine/ICheck.php',
494497
'OCP\\WorkflowEngine\\IComplexOperation' => $baseDir . '/lib/public/WorkflowEngine/IComplexOperation.php',

lib/composer/composer/autoload_static.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,9 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
518518
'OCP\\WorkflowEngine\\EntityContext\\IDisplayText' => __DIR__ . '/../../..' . '/lib/public/WorkflowEngine/EntityContext/IDisplayText.php',
519519
'OCP\\WorkflowEngine\\EntityContext\\IIcon' => __DIR__ . '/../../..' . '/lib/public/WorkflowEngine/EntityContext/IIcon.php',
520520
'OCP\\WorkflowEngine\\EntityContext\\IUrl' => __DIR__ . '/../../..' . '/lib/public/WorkflowEngine/EntityContext/IUrl.php',
521+
'OCP\\WorkflowEngine\\Events\\RegisterChecksEvent' => __DIR__ . '/../../..' . '/lib/public/WorkflowEngine/Events/RegisterChecksEvent.php',
522+
'OCP\\WorkflowEngine\\Events\\RegisterEntitiesEvent' => __DIR__ . '/../../..' . '/lib/public/WorkflowEngine/Events/RegisterEntitiesEvent.php',
523+
'OCP\\WorkflowEngine\\Events\\RegisterOperationsEvent' => __DIR__ . '/../../..' . '/lib/public/WorkflowEngine/Events/RegisterOperationsEvent.php',
521524
'OCP\\WorkflowEngine\\GenericEntityEvent' => __DIR__ . '/../../..' . '/lib/public/WorkflowEngine/GenericEntityEvent.php',
522525
'OCP\\WorkflowEngine\\ICheck' => __DIR__ . '/../../..' . '/lib/public/WorkflowEngine/ICheck.php',
523526
'OCP\\WorkflowEngine\\IComplexOperation' => __DIR__ . '/../../..' . '/lib/public/WorkflowEngine/IComplexOperation.php',
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
declare(strict_types=1);
3+
/**
4+
* @copyright Copyright (c) 2019, Roeland Jago Douma <roeland@famdouma.nl>
5+
*
6+
* @author Roeland Jago Douma <roeland@famdouma.nl>
7+
*
8+
* @license GNU AGPL version 3 or any later version
9+
*
10+
* This program is free software: you can redistribute it and/or modify
11+
* it under the terms of the GNU Affero General Public License as
12+
* published by the Free Software Foundation, either version 3 of the
13+
* License, or (at your option) any later version.
14+
*
15+
* This program is distributed in the hope that it will be useful,
16+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
* GNU Affero General Public License for more details.
19+
*
20+
* You should have received a copy of the GNU Affero General Public License
21+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
22+
*
23+
*/
24+
25+
namespace OCP\WorkflowEngine\Events;
26+
27+
use OCP\EventDispatcher\Event;
28+
use OCP\WorkflowEngine\ICheck;
29+
use OCP\WorkflowEngine\IManager;
30+
31+
/**
32+
* @since 18.0.0
33+
*/
34+
class RegisterChecksEvent extends Event {
35+
36+
/** @var IManager */
37+
private $manager;
38+
39+
/**
40+
* @since 18.0.0
41+
*/
42+
public function __construct(IManager $manager) {
43+
parent::__construct();
44+
45+
$this->manager = $manager;
46+
}
47+
48+
/**
49+
* @since 18.0.0
50+
*/
51+
public function registerCheck(ICheck $check): void {
52+
$this->manager->registerCheck($check);
53+
}
54+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
declare(strict_types=1);
3+
/**
4+
* @copyright Copyright (c) 2019, Roeland Jago Douma <roeland@famdouma.nl>
5+
*
6+
* @author Roeland Jago Douma <roeland@famdouma.nl>
7+
*
8+
* @license GNU AGPL version 3 or any later version
9+
*
10+
* This program is free software: you can redistribute it and/or modify
11+
* it under the terms of the GNU Affero General Public License as
12+
* published by the Free Software Foundation, either version 3 of the
13+
* License, or (at your option) any later version.
14+
*
15+
* This program is distributed in the hope that it will be useful,
16+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
* GNU Affero General Public License for more details.
19+
*
20+
* You should have received a copy of the GNU Affero General Public License
21+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
22+
*
23+
*/
24+
25+
namespace OCP\WorkflowEngine\Events;
26+
27+
use OCP\EventDispatcher\Event;
28+
use OCP\WorkflowEngine\IEntity;
29+
use OCP\WorkflowEngine\IManager;
30+
31+
/**
32+
* @since 18.0.0
33+
*/
34+
class RegisterEntitiesEvent extends Event {
35+
36+
/** @var IManager */
37+
private $manager;
38+
39+
/**
40+
* @since 18.0.0
41+
*/
42+
public function __construct(IManager $manager) {
43+
parent::__construct();
44+
45+
$this->manager = $manager;
46+
}
47+
48+
/**
49+
* @since 18.0.0
50+
*/
51+
public function registerEntity(IEntity $entity): void {
52+
$this->manager->registerEntity($entity);
53+
}
54+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
declare(strict_types=1);
3+
/**
4+
* @copyright Copyright (c) 2019, Roeland Jago Douma <roeland@famdouma.nl>
5+
*
6+
* @author Roeland Jago Douma <roeland@famdouma.nl>
7+
*
8+
* @license GNU AGPL version 3 or any later version
9+
*
10+
* This program is free software: you can redistribute it and/or modify
11+
* it under the terms of the GNU Affero General Public License as
12+
* published by the Free Software Foundation, either version 3 of the
13+
* License, or (at your option) any later version.
14+
*
15+
* This program is distributed in the hope that it will be useful,
16+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
* GNU Affero General Public License for more details.
19+
*
20+
* You should have received a copy of the GNU Affero General Public License
21+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
22+
*
23+
*/
24+
25+
namespace OCP\WorkflowEngine\Events;
26+
27+
use OCP\EventDispatcher\Event;
28+
use OCP\WorkflowEngine\IManager;
29+
use OCP\WorkflowEngine\IOperation;
30+
31+
/**
32+
* @since 18.0.0
33+
*/
34+
class RegisterOperationsEvent extends Event {
35+
36+
/** @var IManager */
37+
private $manager;
38+
39+
/**
40+
* @since 18.0.0
41+
*/
42+
public function __construct(IManager $manager) {
43+
parent::__construct();
44+
45+
$this->manager = $manager;
46+
}
47+
48+
/**
49+
* @since 18.0.0
50+
*/
51+
public function registerOperation(IOperation $operation): void {
52+
$this->manager->registerOperation($operation);
53+
}
54+
}

lib/public/WorkflowEngine/IManager.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,29 +35,32 @@ interface IManager {
3535
const SCOPE_ADMIN = 0;
3636
const SCOPE_USER = 1;
3737

38+
/**
39+
* @depreacted Will be removed in NC19. Use the dedicated events in OCP\WorkflowEngine\Events
40+
*/
3841
const EVENT_NAME_REG_OPERATION = 'OCP\WorkflowEngine::registerOperations';
3942
const EVENT_NAME_REG_ENTITY = 'OCP\WorkflowEngine::registerEntities';
4043
const EVENT_NAME_REG_CHECK = 'OCP\WorkflowEngine::registerChecks';
4144

4245
/**
43-
* Listen to `\OCP\WorkflowEngine::EVENT_NAME_REG_ENTITY` at the
44-
* EventDispatcher for registering your entities.
46+
* Listen to `OCP\WorkflowEngine\Events\RegisterEntitiesEvent` at the
47+
* IEventDispatcher for registering your entities.
4548
*
4649
* @since 18.0.0
4750
*/
4851
public function registerEntity(IEntity $entity): void;
4952

5053
/**
51-
* Listen to `\OCP\WorkflowEngine::EVENT_NAME_REG_OPERATION` at the
52-
* EventDispatcher for registering your operators.
54+
* Listen to `OCP\WorkflowEngine\Events\RegisterOperationsEvent` at the
55+
* IEventDispatcher for registering your operators.
5356
*
5457
* @since 18.0.0
5558
*/
5659
public function registerOperation(IOperation $operator): void;
5760

5861
/**
59-
* Listen to `\OCP\WorkflowEngine::EVENT_NAME_REG_CHECK` at the
60-
* EventDispatcher for registering your operators.
62+
* Listen to `OCP\WorkflowEngine\Events\RegisterChecksEvent` at the
63+
* IEventDispatcher for registering your operators.
6164
*
6265
* @since 18.0.0
6366
*/

0 commit comments

Comments
 (0)