Skip to content

Commit

Permalink
fix: Move Node HookConnecter events to IEventDispatcher
Browse files Browse the repository at this point in the history
Signed-off-by: Joas Schilling <[email protected]>
  • Loading branch information
nickvergessen committed Jul 26, 2023
1 parent 2636a81 commit 37ab7f7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 38 deletions.
38 changes: 13 additions & 25 deletions lib/private/Files/Node/HookConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
use OCP\Files\FileInfo;
use OCP\Files\IRootFolder;
use OCP\Util;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

class HookConnector {
/** @var IRootFolder */
Expand All @@ -58,26 +57,15 @@ class HookConnector {
/** @var FileInfo[] */
private $deleteMetaCache = [];

/** @var EventDispatcherInterface */
private $legacyDispatcher;

/** @var IEventDispatcher */
private $dispatcher;

/**
* HookConnector constructor.
*
* @param Root $root
* @param View $view
*/
public function __construct(
IRootFolder $root,
View $view,
EventDispatcherInterface $legacyDispatcher,
IEventDispatcher $dispatcher) {
$this->root = $root;
$this->view = $view;
$this->legacyDispatcher = $legacyDispatcher;
$this->dispatcher = $dispatcher;
}

Expand Down Expand Up @@ -106,7 +94,7 @@ public function viewToNode() {
public function write($arguments) {
$node = $this->getNodeForPath($arguments['path']);
$this->root->emit('\OC\Files', 'preWrite', [$node]);
$this->legacyDispatcher->dispatch('\OCP\Files::preWrite', new GenericEvent($node));
$this->dispatcher->dispatch('\OCP\Files::preWrite', new GenericEvent($node));

$event = new BeforeNodeWrittenEvent($node);
$this->dispatcher->dispatchTyped($event);
Expand All @@ -115,7 +103,7 @@ public function write($arguments) {
public function postWrite($arguments) {
$node = $this->getNodeForPath($arguments['path']);
$this->root->emit('\OC\Files', 'postWrite', [$node]);
$this->legacyDispatcher->dispatch('\OCP\Files::postWrite', new GenericEvent($node));
$this->dispatcher->dispatch('\OCP\Files::postWrite', new GenericEvent($node));

$event = new NodeWrittenEvent($node);
$this->dispatcher->dispatchTyped($event);
Expand All @@ -124,7 +112,7 @@ public function postWrite($arguments) {
public function create($arguments) {
$node = $this->getNodeForPath($arguments['path']);
$this->root->emit('\OC\Files', 'preCreate', [$node]);
$this->legacyDispatcher->dispatch('\OCP\Files::preCreate', new GenericEvent($node));
$this->dispatcher->dispatch('\OCP\Files::preCreate', new GenericEvent($node));

$event = new BeforeNodeCreatedEvent($node);
$this->dispatcher->dispatchTyped($event);
Expand All @@ -133,7 +121,7 @@ public function create($arguments) {
public function postCreate($arguments) {
$node = $this->getNodeForPath($arguments['path']);
$this->root->emit('\OC\Files', 'postCreate', [$node]);
$this->legacyDispatcher->dispatch('\OCP\Files::postCreate', new GenericEvent($node));
$this->dispatcher->dispatch('\OCP\Files::postCreate', new GenericEvent($node));

$event = new NodeCreatedEvent($node);
$this->dispatcher->dispatchTyped($event);
Expand All @@ -143,7 +131,7 @@ public function delete($arguments) {
$node = $this->getNodeForPath($arguments['path']);
$this->deleteMetaCache[$node->getPath()] = $node->getFileInfo();
$this->root->emit('\OC\Files', 'preDelete', [$node]);
$this->legacyDispatcher->dispatch('\OCP\Files::preDelete', new GenericEvent($node));
$this->dispatcher->dispatch('\OCP\Files::preDelete', new GenericEvent($node));

$event = new BeforeNodeDeletedEvent($node);
$this->dispatcher->dispatchTyped($event);
Expand All @@ -153,7 +141,7 @@ public function postDelete($arguments) {
$node = $this->getNodeForPath($arguments['path']);
unset($this->deleteMetaCache[$node->getPath()]);
$this->root->emit('\OC\Files', 'postDelete', [$node]);
$this->legacyDispatcher->dispatch('\OCP\Files::postDelete', new GenericEvent($node));
$this->dispatcher->dispatch('\OCP\Files::postDelete', new GenericEvent($node));

$event = new NodeDeletedEvent($node);
$this->dispatcher->dispatchTyped($event);
Expand All @@ -162,7 +150,7 @@ public function postDelete($arguments) {
public function touch($arguments) {
$node = $this->getNodeForPath($arguments['path']);
$this->root->emit('\OC\Files', 'preTouch', [$node]);
$this->legacyDispatcher->dispatch('\OCP\Files::preTouch', new GenericEvent($node));
$this->dispatcher->dispatch('\OCP\Files::preTouch', new GenericEvent($node));

$event = new BeforeNodeTouchedEvent($node);
$this->dispatcher->dispatchTyped($event);
Expand All @@ -171,7 +159,7 @@ public function touch($arguments) {
public function postTouch($arguments) {
$node = $this->getNodeForPath($arguments['path']);
$this->root->emit('\OC\Files', 'postTouch', [$node]);
$this->legacyDispatcher->dispatch('\OCP\Files::postTouch', new GenericEvent($node));
$this->dispatcher->dispatch('\OCP\Files::postTouch', new GenericEvent($node));

$event = new NodeTouchedEvent($node);
$this->dispatcher->dispatchTyped($event);
Expand All @@ -181,7 +169,7 @@ public function rename($arguments) {
$source = $this->getNodeForPath($arguments['oldpath']);
$target = $this->getNodeForPath($arguments['newpath']);
$this->root->emit('\OC\Files', 'preRename', [$source, $target]);
$this->legacyDispatcher->dispatch('\OCP\Files::preRename', new GenericEvent([$source, $target]));
$this->dispatcher->dispatch('\OCP\Files::preRename', new GenericEvent([$source, $target]));

$event = new BeforeNodeRenamedEvent($source, $target);
$this->dispatcher->dispatchTyped($event);
Expand All @@ -191,7 +179,7 @@ public function postRename($arguments) {
$source = $this->getNodeForPath($arguments['oldpath']);
$target = $this->getNodeForPath($arguments['newpath']);
$this->root->emit('\OC\Files', 'postRename', [$source, $target]);
$this->legacyDispatcher->dispatch('\OCP\Files::postRename', new GenericEvent([$source, $target]));
$this->dispatcher->dispatch('\OCP\Files::postRename', new GenericEvent([$source, $target]));

$event = new NodeRenamedEvent($source, $target);
$this->dispatcher->dispatchTyped($event);
Expand All @@ -201,7 +189,7 @@ public function copy($arguments) {
$source = $this->getNodeForPath($arguments['oldpath']);
$target = $this->getNodeForPath($arguments['newpath']);
$this->root->emit('\OC\Files', 'preCopy', [$source, $target]);
$this->legacyDispatcher->dispatch('\OCP\Files::preCopy', new GenericEvent([$source, $target]));
$this->dispatcher->dispatch('\OCP\Files::preCopy', new GenericEvent([$source, $target]));

$event = new BeforeNodeCopiedEvent($source, $target);
$this->dispatcher->dispatchTyped($event);
Expand All @@ -211,7 +199,7 @@ public function postCopy($arguments) {
$source = $this->getNodeForPath($arguments['oldpath']);
$target = $this->getNodeForPath($arguments['newpath']);
$this->root->emit('\OC\Files', 'postCopy', [$source, $target]);
$this->legacyDispatcher->dispatch('\OCP\Files::postCopy', new GenericEvent([$source, $target]));
$this->dispatcher->dispatch('\OCP\Files::postCopy', new GenericEvent([$source, $target]));

$event = new NodeCopiedEvent($source, $target);
$this->dispatcher->dispatchTyped($event);
Expand All @@ -220,7 +208,7 @@ public function postCopy($arguments) {
public function read($arguments) {
$node = $this->getNodeForPath($arguments['path']);
$this->root->emit('\OC\Files', 'read', [$node]);
$this->legacyDispatcher->dispatch('\OCP\Files::read', new GenericEvent([$node]));
$this->dispatcher->dispatch('\OCP\Files::read', new GenericEvent([$node]));

$event = new BeforeNodeReadEvent($node);
$this->dispatcher->dispatchTyped($event);
Expand Down
1 change: 0 additions & 1 deletion lib/private/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,6 @@ public function __construct($webRoot, \OC\Config $config) {
return new HookConnector(
$c->get(IRootFolder::class),
new View(),
$c->get(\OC\EventDispatcher\SymfonyAdapter::class),
$c->get(IEventDispatcher::class)
);
});
Expand Down
18 changes: 6 additions & 12 deletions tests/lib/Files/Node/HookConnectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@
use OCP\Files\Events\Node\NodeWrittenEvent;
use OCP\Files\Node;
use OCP\IUserManager;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
use Test\TestCase;
use Test\Traits\MountProviderTrait;
Expand All @@ -50,9 +48,6 @@ class HookConnectorTest extends TestCase {
use UserTrait;
use MountProviderTrait;

/** @var EventDispatcherInterface|MockObject */
protected $legacyDispatcher;

/** @var IEventDispatcher */
protected $eventDispatcher;

Expand Down Expand Up @@ -82,7 +77,6 @@ protected function setUp(): void {
$this->createMock(IUserManager::class),
$this->createMock(IEventDispatcher::class)
);
$this->legacyDispatcher = \OC::$server->getEventDispatcher();
$this->eventDispatcher = \OC::$server->query(IEventDispatcher::class);
}

Expand Down Expand Up @@ -149,7 +143,7 @@ public function viewToNodeProvider() {
* @dataProvider viewToNodeProvider
*/
public function testViewToNode(callable $operation, $expectedHook, $expectedLegacyEvent, $expectedEvent) {
$connector = new HookConnector($this->root, $this->view, $this->legacyDispatcher, $this->eventDispatcher);
$connector = new HookConnector($this->root, $this->view, $this->eventDispatcher);
$connector->viewToNode();
$hookCalled = false;
/** @var Node $hookNode */
Expand All @@ -163,7 +157,7 @@ public function testViewToNode(callable $operation, $expectedHook, $expectedLega
$dispatcherCalled = false;
/** @var Node $dispatcherNode */
$dispatcherNode = null;
$this->legacyDispatcher->addListener($expectedLegacyEvent, function ($event) use (&$dispatcherCalled, &$dispatcherNode) {
$this->eventDispatcher->addListener($expectedLegacyEvent, function ($event) use (&$dispatcherCalled, &$dispatcherNode) {
/** @var GenericEvent|APIGenericEvent $event */
$dispatcherCalled = true;
$dispatcherNode = $event->getSubject();
Expand Down Expand Up @@ -218,7 +212,7 @@ public function viewToNodeProviderCopyRename() {
* @dataProvider viewToNodeProviderCopyRename
*/
public function testViewToNodeCopyRename(callable $operation, $expectedHook, $expectedLegacyEvent, $expectedEvent) {
$connector = new HookConnector($this->root, $this->view, $this->legacyDispatcher, $this->eventDispatcher);
$connector = new HookConnector($this->root, $this->view, $this->eventDispatcher);
$connector->viewToNode();
$hookCalled = false;
/** @var Node $hookSourceNode */
Expand All @@ -237,7 +231,7 @@ public function testViewToNodeCopyRename(callable $operation, $expectedHook, $ex
$dispatcherSourceNode = null;
/** @var Node $dispatcherTargetNode */
$dispatcherTargetNode = null;
$this->legacyDispatcher->addListener($expectedLegacyEvent, function ($event) use (&$dispatcherSourceNode, &$dispatcherTargetNode, &$dispatcherCalled) {
$this->eventDispatcher->addListener($expectedLegacyEvent, function ($event) use (&$dispatcherSourceNode, &$dispatcherTargetNode, &$dispatcherCalled) {
/** @var GenericEvent|APIGenericEvent $event */
$dispatcherCalled = true;
[$dispatcherSourceNode, $dispatcherTargetNode] = $event->getSubject();
Expand Down Expand Up @@ -273,7 +267,7 @@ public function testViewToNodeCopyRename(callable $operation, $expectedHook, $ex
}

public function testPostDeleteMeta() {
$connector = new HookConnector($this->root, $this->view, $this->legacyDispatcher, $this->eventDispatcher);
$connector = new HookConnector($this->root, $this->view, $this->eventDispatcher);
$connector->viewToNode();
$hookCalled = false;
/** @var Node $hookNode */
Expand All @@ -287,7 +281,7 @@ public function testPostDeleteMeta() {
$dispatcherCalled = false;
/** @var Node $dispatcherNode */
$dispatcherNode = null;
$this->legacyDispatcher->addListener('\OCP\Files::postDelete', function ($event) use (&$dispatcherCalled, &$dispatcherNode) {
$this->eventDispatcher->addListener('\OCP\Files::postDelete', function ($event) use (&$dispatcherCalled, &$dispatcherNode) {
/** @var GenericEvent|APIGenericEvent $event */
$dispatcherCalled = true;
$dispatcherNode = $event->getSubject();
Expand Down

0 comments on commit 37ab7f7

Please sign in to comment.