diff options
author | Roeland Jago Douma <roeland@famdouma.nl> | 2020-04-22 15:21:15 +0200 |
---|---|---|
committer | Roeland Jago Douma <roeland@famdouma.nl> | 2020-05-28 12:35:45 +0200 |
commit | 6aa6ab3e02c1ab50992c824b85ecc1a45988379c (patch) | |
tree | d09f950d2905d24497cea60fe3042dd2f62f89a1 /lib/private | |
parent | 1d469fc06e6a6755dbb5543b33fecb9ff2b57340 (diff) | |
download | nextcloud-server-6aa6ab3e02c1ab50992c824b85ecc1a45988379c.tar.gz nextcloud-server-6aa6ab3e02c1ab50992c824b85ecc1a45988379c.zip |
Add lazy events for the Node API
Right now if you want to get events via the Node API you have to have a
real instance of the Root. Which in turns sets up the whole FS.
We should make sure this is done lazy. Else enabling the preview
generator for example makes you setup the whole FS on each and every
authenticated call.
Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
Diffstat (limited to 'lib/private')
-rw-r--r-- | lib/private/Files/Node/HookConnector.php | 103 | ||||
-rw-r--r-- | lib/private/Server.php | 17 |
2 files changed, 95 insertions, 25 deletions
diff --git a/lib/private/Files/Node/HookConnector.php b/lib/private/Files/Node/HookConnector.php index 1f7f194c5f7..2f2641868af 100644 --- a/lib/private/Files/Node/HookConnector.php +++ b/lib/private/Files/Node/HookConnector.php @@ -27,26 +27,39 @@ namespace OC\Files\Node; use OC\Files\Filesystem; use OC\Files\View; use OCP\EventDispatcher\GenericEvent; +use OCP\EventDispatcher\IEventDispatcher; +use OCP\Files\Events\Node\BeforeNodeCopiedEvent; +use OCP\Files\Events\Node\BeforeNodeCreatedEvent; +use OCP\Files\Events\Node\BeforeNodeDeletedEvent; +use OCP\Files\Events\Node\BeforeNodeReadEvent; +use OCP\Files\Events\Node\BeforeNodeRenamedEvent; +use OCP\Files\Events\Node\BeforeNodeTouchedEvent; +use OCP\Files\Events\Node\BeforeNodeWrittenEvent; +use OCP\Files\Events\Node\NodeCopiedEvent; +use OCP\Files\Events\Node\NodeCreatedEvent; +use OCP\Files\Events\Node\NodeDeletedEvent; +use OCP\Files\Events\Node\NodeRenamedEvent; +use OCP\Files\Events\Node\NodeTouchedEvent; +use OCP\Files\Events\Node\NodeWrittenEvent; use OCP\Files\FileInfo; +use OCP\Files\IRootFolder; use OCP\Util; use Symfony\Component\EventDispatcher\EventDispatcherInterface; class HookConnector { - /** - * @var Root - */ + /** @var IRootFolder */ private $root; - /** - * @var View - */ + /** @var View */ private $view; - /** - * @var FileInfo[] - */ + /** @var FileInfo[] */ private $deleteMetaCache = []; + /** @var EventDispatcherInterface */ + private $legacyDispatcher; + + /** @var IEventDispatcher */ private $dispatcher; /** @@ -55,9 +68,14 @@ class HookConnector { * @param Root $root * @param View $view */ - public function __construct(Root $root, View $view, EventDispatcherInterface $dispatcher) { + public function __construct( + IRootFolder $root, + View $view, + EventDispatcherInterface $legacyDispatcher, + IEventDispatcher $dispatcher) { $this->root = $root; $this->view = $view; + $this->legacyDispatcher = $legacyDispatcher; $this->dispatcher = $dispatcher; } @@ -86,85 +104,124 @@ class HookConnector { public function write($arguments) { $node = $this->getNodeForPath($arguments['path']); $this->root->emit('\OC\Files', 'preWrite', [$node]); - $this->dispatcher->dispatch('\OCP\Files::preWrite', new GenericEvent($node)); + $this->legacyDispatcher->dispatch('\OCP\Files::preWrite', new GenericEvent($node)); + + $event = new BeforeNodeWrittenEvent($node); + $this->dispatcher->dispatchTyped($event); } public function postWrite($arguments) { $node = $this->getNodeForPath($arguments['path']); $this->root->emit('\OC\Files', 'postWrite', [$node]); - $this->dispatcher->dispatch('\OCP\Files::postWrite', new GenericEvent($node)); + $this->legacyDispatcher->dispatch('\OCP\Files::postWrite', new GenericEvent($node)); + + $event = new NodeWrittenEvent($node); + $this->dispatcher->dispatchTyped($event); } public function create($arguments) { $node = $this->getNodeForPath($arguments['path']); $this->root->emit('\OC\Files', 'preCreate', [$node]); - $this->dispatcher->dispatch('\OCP\Files::preCreate', new GenericEvent($node)); + $this->legacyDispatcher->dispatch('\OCP\Files::preCreate', new GenericEvent($node)); + + $event = new BeforeNodeCreatedEvent($node); + $this->dispatcher->dispatchTyped($event); } public function postCreate($arguments) { $node = $this->getNodeForPath($arguments['path']); $this->root->emit('\OC\Files', 'postCreate', [$node]); - $this->dispatcher->dispatch('\OCP\Files::postCreate', new GenericEvent($node)); + $this->legacyDispatcher->dispatch('\OCP\Files::postCreate', new GenericEvent($node)); + + $event = new NodeCreatedEvent($node); + $this->dispatcher->dispatchTyped($event); } public function delete($arguments) { $node = $this->getNodeForPath($arguments['path']); $this->deleteMetaCache[$node->getPath()] = $node->getFileInfo(); $this->root->emit('\OC\Files', 'preDelete', [$node]); - $this->dispatcher->dispatch('\OCP\Files::preDelete', new GenericEvent($node)); + $this->legacyDispatcher->dispatch('\OCP\Files::preDelete', new GenericEvent($node)); + + $event = new BeforeNodeDeletedEvent($node); + $this->dispatcher->dispatchTyped($event); } public function postDelete($arguments) { $node = $this->getNodeForPath($arguments['path']); unset($this->deleteMetaCache[$node->getPath()]); $this->root->emit('\OC\Files', 'postDelete', [$node]); - $this->dispatcher->dispatch('\OCP\Files::postDelete', new GenericEvent($node)); + $this->legacyDispatcher->dispatch('\OCP\Files::postDelete', new GenericEvent($node)); + + $event = new NodeDeletedEvent($node); + $this->dispatcher->dispatchTyped($event); } public function touch($arguments) { $node = $this->getNodeForPath($arguments['path']); $this->root->emit('\OC\Files', 'preTouch', [$node]); - $this->dispatcher->dispatch('\OCP\Files::preTouch', new GenericEvent($node)); + $this->legacyDispatcher->dispatch('\OCP\Files::preTouch', new GenericEvent($node)); + + $event = new BeforeNodeTouchedEvent($node); + $this->dispatcher->dispatchTyped($event); } public function postTouch($arguments) { $node = $this->getNodeForPath($arguments['path']); $this->root->emit('\OC\Files', 'postTouch', [$node]); - $this->dispatcher->dispatch('\OCP\Files::postTouch', new GenericEvent($node)); + $this->legacyDispatcher->dispatch('\OCP\Files::postTouch', new GenericEvent($node)); + + $event = new NodeTouchedEvent($node); + $this->dispatcher->dispatchTyped($event); } public function rename($arguments) { $source = $this->getNodeForPath($arguments['oldpath']); $target = $this->getNodeForPath($arguments['newpath']); $this->root->emit('\OC\Files', 'preRename', [$source, $target]); - $this->dispatcher->dispatch('\OCP\Files::preRename', new GenericEvent([$source, $target])); + $this->legacyDispatcher->dispatch('\OCP\Files::preRename', new GenericEvent([$source, $target])); + + $event = new BeforeNodeRenamedEvent($source, $target); + $this->dispatcher->dispatchTyped($event); } public function postRename($arguments) { $source = $this->getNodeForPath($arguments['oldpath']); $target = $this->getNodeForPath($arguments['newpath']); $this->root->emit('\OC\Files', 'postRename', [$source, $target]); - $this->dispatcher->dispatch('\OCP\Files::postRename', new GenericEvent([$source, $target])); + $this->legacyDispatcher->dispatch('\OCP\Files::postRename', new GenericEvent([$source, $target])); + + $event = new NodeRenamedEvent($source, $target); + $this->dispatcher->dispatchTyped($event); } public function copy($arguments) { $source = $this->getNodeForPath($arguments['oldpath']); $target = $this->getNodeForPath($arguments['newpath']); $this->root->emit('\OC\Files', 'preCopy', [$source, $target]); - $this->dispatcher->dispatch('\OCP\Files::preCopy', new GenericEvent([$source, $target])); + $this->legacyDispatcher->dispatch('\OCP\Files::preCopy', new GenericEvent([$source, $target])); + + $event = new BeforeNodeCopiedEvent($source, $target); + $this->dispatcher->dispatchTyped($event); } public function postCopy($arguments) { $source = $this->getNodeForPath($arguments['oldpath']); $target = $this->getNodeForPath($arguments['newpath']); $this->root->emit('\OC\Files', 'postCopy', [$source, $target]); - $this->dispatcher->dispatch('\OCP\Files::postCopy', new GenericEvent([$source, $target])); + $this->legacyDispatcher->dispatch('\OCP\Files::postCopy', new GenericEvent([$source, $target])); + + $event = new NodeCopiedEvent($source, $target); + $this->dispatcher->dispatchTyped($event); } public function read($arguments) { $node = $this->getNodeForPath($arguments['path']); $this->root->emit('\OC\Files', 'read', [$node]); - $this->dispatcher->dispatch('\OCP\Files::read', new GenericEvent([$node])); + $this->legacyDispatcher->dispatch('\OCP\Files::read', new GenericEvent([$node])); + + $event = new BeforeNodeReadEvent($node); + $this->dispatcher->dispatchTyped($event); } private function getNodeForPath($path) { diff --git a/lib/private/Server.php b/lib/private/Server.php index 9c3c6391c34..ba628d1269f 100644 --- a/lib/private/Server.php +++ b/lib/private/Server.php @@ -373,14 +373,21 @@ class Server extends ServerContainer implements IServerContainer { $this->getLogger(), $this->getUserManager() ); - $connector = new HookConnector($root, $view, $c->getEventDispatcher()); - $connector->viewToNode(); $previewConnector = new \OC\Preview\WatcherConnector($root, $c->getSystemConfig()); $previewConnector->connectWatcher(); return $root; }); + $this->registerService(HookConnector::class, function (Server $c) { + return new HookConnector( + $c->query(IRootFolder::class), + new View(), + $c->query(\OC\EventDispatcher\SymfonyAdapter::class), + $c->query(IEventDispatcher::class) + ); + }); + $this->registerDeprecatedAlias('SystemTagObjectMapper', ISystemTagObjectMapper::class); $this->registerService(IRootFolder::class, function (Server $c) { @@ -1359,6 +1366,12 @@ class Server extends ServerContainer implements IServerContainer { $this->connectDispatcher(); } + public function boot() { + /** @var HookConnector $hookConnector */ + $hookConnector = $this->query(HookConnector::class); + $hookConnector->viewToNode(); + } + /** * @return \OCP\Calendar\IManager */ |