emit file events via Dispatcher, too

another step to get rid of hooks and emitters

Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
This commit is contained in:
Arthur Schiwon 2019-09-03 12:30:10 +02:00
parent 0d7f7e5495
commit 20901c59d4
No known key found for this signature in database
GPG Key ID: 7424F1874854DF23
5 changed files with 40 additions and 22 deletions

View File

@ -119,7 +119,7 @@ class File extends Node implements \OCP\Files\File {
$fileInfo = $this->getFileInfo(); $fileInfo = $this->getFileInfo();
$this->view->unlink($this->path); $this->view->unlink($this->path);
$nonExisting = new NonExistingFile($this->root, $this->view, $this->path, $fileInfo); $nonExisting = new NonExistingFile($this->root, $this->view, $this->path, $fileInfo);
$this->root->emit('\OC\Files', 'postDelete', array($nonExisting)); $this->sendHooks(['postDelete'], [$nonExisting]);
$this->exists = false; $this->exists = false;
$this->fileInfo = null; $this->fileInfo = null;
} else { } else {

View File

@ -156,14 +156,12 @@ class Folder extends Node implements \OCP\Files\Folder {
if ($this->checkPermissions(\OCP\Constants::PERMISSION_CREATE)) { if ($this->checkPermissions(\OCP\Constants::PERMISSION_CREATE)) {
$fullPath = $this->getFullPath($path); $fullPath = $this->getFullPath($path);
$nonExisting = new NonExistingFolder($this->root, $this->view, $fullPath); $nonExisting = new NonExistingFolder($this->root, $this->view, $fullPath);
$this->root->emit('\OC\Files', 'preWrite', array($nonExisting)); $this->sendHooks(['preWrite', 'preCreate'], [$nonExisting]);
$this->root->emit('\OC\Files', 'preCreate', array($nonExisting));
if(!$this->view->mkdir($fullPath)) { if(!$this->view->mkdir($fullPath)) {
throw new NotPermittedException('Could not create folder'); throw new NotPermittedException('Could not create folder');
} }
$node = new Folder($this->root, $this->view, $fullPath); $node = new Folder($this->root, $this->view, $fullPath);
$this->root->emit('\OC\Files', 'postWrite', array($node)); $this->sendHooks(['postWrite', 'postCreate'], [$node]);
$this->root->emit('\OC\Files', 'postCreate', array($node));
return $node; return $node;
} else { } else {
throw new NotPermittedException('No create permission for folder'); throw new NotPermittedException('No create permission for folder');
@ -179,14 +177,12 @@ class Folder extends Node implements \OCP\Files\Folder {
if ($this->checkPermissions(\OCP\Constants::PERMISSION_CREATE)) { if ($this->checkPermissions(\OCP\Constants::PERMISSION_CREATE)) {
$fullPath = $this->getFullPath($path); $fullPath = $this->getFullPath($path);
$nonExisting = new NonExistingFile($this->root, $this->view, $fullPath); $nonExisting = new NonExistingFile($this->root, $this->view, $fullPath);
$this->root->emit('\OC\Files', 'preWrite', array($nonExisting)); $this->sendHooks(['preWrite', 'preCreate'], [$nonExisting]);
$this->root->emit('\OC\Files', 'preCreate', array($nonExisting));
if (!$this->view->touch($fullPath)) { if (!$this->view->touch($fullPath)) {
throw new NotPermittedException('Could not create path'); throw new NotPermittedException('Could not create path');
} }
$node = new File($this->root, $this->view, $fullPath); $node = new File($this->root, $this->view, $fullPath);
$this->root->emit('\OC\Files', 'postWrite', array($node)); $this->sendHooks(['postWrite', 'postCreate'], [$node]);
$this->root->emit('\OC\Files', 'postCreate', array($node));
return $node; return $node;
} }
throw new NotPermittedException('No create permission for path'); throw new NotPermittedException('No create permission for path');
@ -341,7 +337,7 @@ class Folder extends Node implements \OCP\Files\Folder {
$fileInfo = $this->getFileInfo(); $fileInfo = $this->getFileInfo();
$this->view->rmdir($this->path); $this->view->rmdir($this->path);
$nonExisting = new NonExistingFolder($this->root, $this->view, $this->path, $fileInfo); $nonExisting = new NonExistingFolder($this->root, $this->view, $this->path, $fileInfo);
$this->root->emit('\OC\Files', 'postDelete', array($nonExisting)); $this->sendHooks(['postDelete'], [$nonExisting]);
$this->exists = false; $this->exists = false;
} else { } else {
throw new NotPermittedException('No delete permission for path'); throw new NotPermittedException('No delete permission for path');

View File

@ -26,6 +26,8 @@ use OCP\Files\FileInfo;
use OC\Files\Filesystem; use OC\Files\Filesystem;
use OC\Files\View; use OC\Files\View;
use OCP\Util; use OCP\Util;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
class HookConnector { class HookConnector {
/** /**
@ -42,6 +44,8 @@ class HookConnector {
* @var FileInfo[] * @var FileInfo[]
*/ */
private $deleteMetaCache = []; private $deleteMetaCache = [];
/** @var EventDispatcherInterface */
private $dispatcher;
/** /**
* HookConnector constructor. * HookConnector constructor.
@ -49,9 +53,10 @@ class HookConnector {
* @param Root $root * @param Root $root
* @param View $view * @param View $view
*/ */
public function __construct(Root $root, View $view) { public function __construct(Root $root, View $view, EventDispatcherInterface $dispatcher) {
$this->root = $root; $this->root = $root;
$this->view = $view; $this->view = $view;
$this->dispatcher = $dispatcher;
} }
public function viewToNode() { public function viewToNode() {
@ -79,72 +84,85 @@ class HookConnector {
public function write($arguments) { public function write($arguments) {
$node = $this->getNodeForPath($arguments['path']); $node = $this->getNodeForPath($arguments['path']);
$this->root->emit('\OC\Files', 'preWrite', [$node]); $this->root->emit('\OC\Files', 'preWrite', [$node]);
$this->dispatcher->dispatch('\OCP\Files::preWrite', new GenericEvent($node));
} }
public function postWrite($arguments) { public function postWrite($arguments) {
$node = $this->getNodeForPath($arguments['path']); $node = $this->getNodeForPath($arguments['path']);
$this->root->emit('\OC\Files', 'postWrite', [$node]); $this->root->emit('\OC\Files', 'postWrite', [$node]);
$this->dispatcher->dispatch('\OCP\Files::postWrite', new GenericEvent($node));
} }
public function create($arguments) { public function create($arguments) {
$node = $this->getNodeForPath($arguments['path']); $node = $this->getNodeForPath($arguments['path']);
$this->root->emit('\OC\Files', 'preCreate', [$node]); $this->root->emit('\OC\Files', 'preCreate', [$node]);
$this->dispatcher->dispatch('\OCP\Files::preCreate', new GenericEvent($node));
} }
public function postCreate($arguments) { public function postCreate($arguments) {
$node = $this->getNodeForPath($arguments['path']); $node = $this->getNodeForPath($arguments['path']);
$this->root->emit('\OC\Files', 'postCreate', [$node]); $this->root->emit('\OC\Files', 'postCreate', [$node]);
$this->dispatcher->dispatch('\OCP\Files::postCreate', new GenericEvent($node));
} }
public function delete($arguments) { public function delete($arguments) {
$node = $this->getNodeForPath($arguments['path']); $node = $this->getNodeForPath($arguments['path']);
$this->deleteMetaCache[$node->getPath()] = $node->getFileInfo(); $this->deleteMetaCache[$node->getPath()] = $node->getFileInfo();
$this->root->emit('\OC\Files', 'preDelete', [$node]); $this->root->emit('\OC\Files', 'preDelete', [$node]);
$this->dispatcher->dispatch('\OCP\Files::preDelete', new GenericEvent($node));
} }
public function postDelete($arguments) { public function postDelete($arguments) {
$node = $this->getNodeForPath($arguments['path']); $node = $this->getNodeForPath($arguments['path']);
unset($this->deleteMetaCache[$node->getPath()]); unset($this->deleteMetaCache[$node->getPath()]);
$this->root->emit('\OC\Files', 'postDelete', [$node]); $this->root->emit('\OC\Files', 'postDelete', [$node]);
$this->dispatcher->dispatch('\OCP\Files::postDelete', new GenericEvent($node));
} }
public function touch($arguments) { public function touch($arguments) {
$node = $this->getNodeForPath($arguments['path']); $node = $this->getNodeForPath($arguments['path']);
$this->root->emit('\OC\Files', 'preTouch', [$node]); $this->root->emit('\OC\Files', 'preTouch', [$node]);
$this->dispatcher->dispatch('\OCP\Files::preTouch', new GenericEvent($node));
} }
public function postTouch($arguments) { public function postTouch($arguments) {
$node = $this->getNodeForPath($arguments['path']); $node = $this->getNodeForPath($arguments['path']);
$this->root->emit('\OC\Files', 'postTouch', [$node]); $this->root->emit('\OC\Files', 'postTouch', [$node]);
$this->dispatcher->dispatch('\OCP\Files::postTouch', new GenericEvent($node));
} }
public function rename($arguments) { public function rename($arguments) {
$source = $this->getNodeForPath($arguments['oldpath']); $source = $this->getNodeForPath($arguments['oldpath']);
$target = $this->getNodeForPath($arguments['newpath']); $target = $this->getNodeForPath($arguments['newpath']);
$this->root->emit('\OC\Files', 'preRename', [$source, $target]); $this->root->emit('\OC\Files', 'preRename', [$source, $target]);
$this->dispatcher->dispatch('\OCP\Files::preRename', new GenericEvent([$source, $target]));
} }
public function postRename($arguments) { public function postRename($arguments) {
$source = $this->getNodeForPath($arguments['oldpath']); $source = $this->getNodeForPath($arguments['oldpath']);
$target = $this->getNodeForPath($arguments['newpath']); $target = $this->getNodeForPath($arguments['newpath']);
$this->root->emit('\OC\Files', 'postRename', [$source, $target]); $this->root->emit('\OC\Files', 'postRename', [$source, $target]);
$this->dispatcher->dispatch('\OCP\Files::postRename', new GenericEvent([$source, $target]));
} }
public function copy($arguments) { public function copy($arguments) {
$source = $this->getNodeForPath($arguments['oldpath']); $source = $this->getNodeForPath($arguments['oldpath']);
$target = $this->getNodeForPath($arguments['newpath']); $target = $this->getNodeForPath($arguments['newpath']);
$this->root->emit('\OC\Files', 'preCopy', [$source, $target]); $this->root->emit('\OC\Files', 'preCopy', [$source, $target]);
$this->dispatcher->dispatch('\OCP\Files::preCopy', new GenericEvent([$source, $target]));
} }
public function postCopy($arguments) { public function postCopy($arguments) {
$source = $this->getNodeForPath($arguments['oldpath']); $source = $this->getNodeForPath($arguments['oldpath']);
$target = $this->getNodeForPath($arguments['newpath']); $target = $this->getNodeForPath($arguments['newpath']);
$this->root->emit('\OC\Files', 'postCopy', [$source, $target]); $this->root->emit('\OC\Files', 'postCopy', [$source, $target]);
$this->dispatcher->dispatch('\OCP\Files::postCopy', new GenericEvent([$source, $target]));
} }
public function read($arguments) { public function read($arguments) {
$node = $this->getNodeForPath($arguments['path']); $node = $this->getNodeForPath($arguments['path']);
$this->root->emit('\OC\Files', 'read', [$node]); $this->root->emit('\OC\Files', 'read', [$node]);
$this->dispatcher->dispatch('\OCP\Files::read', new GenericEvent([$node]));
} }
private function getNodeForPath($path) { private function getNodeForPath($path) {

View File

@ -33,6 +33,7 @@ use OCP\Files\FileInfo;
use OCP\Files\InvalidPathException; use OCP\Files\InvalidPathException;
use OCP\Files\NotFoundException; use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException; use OCP\Files\NotPermittedException;
use Symfony\Component\EventDispatcher\GenericEvent;
// FIXME: this class really should be abstract // FIXME: this class really should be abstract
class Node implements \OCP\Files\Node { class Node implements \OCP\Files\Node {
@ -104,9 +105,12 @@ class Node implements \OCP\Files\Node {
/** /**
* @param string[] $hooks * @param string[] $hooks
*/ */
protected function sendHooks($hooks) { protected function sendHooks($hooks, array $args = null) {
$args = !empty($args) ? $args : [$this];
$dispatcher = \OC::$server->getEventDispatcher();
foreach ($hooks as $hook) { foreach ($hooks as $hook) {
$this->root->emit('\OC\Files', $hook, array($this)); $this->root->emit('\OC\Files', $hook, $args);
$dispatcher->dispatch('\OCP\Files::' . $hook, new GenericEvent($args));
} }
} }
@ -394,14 +398,14 @@ class Node implements \OCP\Files\Node {
$parent = $this->root->get(dirname($targetPath)); $parent = $this->root->get(dirname($targetPath));
if ($parent instanceof Folder and $this->isValidPath($targetPath) and $parent->isCreatable()) { if ($parent instanceof Folder and $this->isValidPath($targetPath) and $parent->isCreatable()) {
$nonExisting = $this->createNonExistingNode($targetPath); $nonExisting = $this->createNonExistingNode($targetPath);
$this->root->emit('\OC\Files', 'preCopy', [$this, $nonExisting]); $this->sendHooks(['preCopy'], [$this, $nonExisting]);
$this->root->emit('\OC\Files', 'preWrite', [$nonExisting]); $this->sendHooks(['preWrite'], [$nonExisting]);
if (!$this->view->copy($this->path, $targetPath)) { if (!$this->view->copy($this->path, $targetPath)) {
throw new NotPermittedException('Could not copy ' . $this->path . ' to ' . $targetPath); throw new NotPermittedException('Could not copy ' . $this->path . ' to ' . $targetPath);
} }
$targetNode = $this->root->get($targetPath); $targetNode = $this->root->get($targetPath);
$this->root->emit('\OC\Files', 'postCopy', [$this, $targetNode]); $this->sendHooks(['postCopy'], [$this, $targetNode]);
$this->root->emit('\OC\Files', 'postWrite', [$targetNode]); $this->sendHooks(['postWrite'], [$targetNode]);
return $targetNode; return $targetNode;
} else { } else {
throw new NotPermittedException('No permission to copy to path ' . $targetPath); throw new NotPermittedException('No permission to copy to path ' . $targetPath);
@ -425,14 +429,14 @@ class Node implements \OCP\Files\Node {
) )
) { ) {
$nonExisting = $this->createNonExistingNode($targetPath); $nonExisting = $this->createNonExistingNode($targetPath);
$this->root->emit('\OC\Files', 'preRename', [$this, $nonExisting]); $this->sendHooks(['preRename'], [$this, $nonExisting]);
$this->root->emit('\OC\Files', 'preWrite', [$nonExisting]); $this->sendHooks(['preWrite'], [$nonExisting]);
if (!$this->view->rename($this->path, $targetPath)) { if (!$this->view->rename($this->path, $targetPath)) {
throw new NotPermittedException('Could not move ' . $this->path . ' to ' . $targetPath); throw new NotPermittedException('Could not move ' . $this->path . ' to ' . $targetPath);
} }
$targetNode = $this->root->get($targetPath); $targetNode = $this->root->get($targetPath);
$this->root->emit('\OC\Files', 'postRename', [$this, $targetNode]); $this->sendHooks(['postRename'], [$this, $targetNode]);
$this->root->emit('\OC\Files', 'postWrite', [$targetNode]); $this->sendHooks(['postWrite'], [$targetNode]);
$this->path = $targetPath; $this->path = $targetPath;
return $targetNode; return $targetNode;
} else { } else {

View File

@ -298,7 +298,7 @@ class Server extends ServerContainer implements IServerContainer {
$this->getLogger(), $this->getLogger(),
$this->getUserManager() $this->getUserManager()
); );
$connector = new HookConnector($root, $view); $connector = new HookConnector($root, $view, $c->getEventDispatcher());
$connector->viewToNode(); $connector->viewToNode();
$previewConnector = new \OC\Preview\WatcherConnector($root, $c->getSystemConfig()); $previewConnector = new \OC\Preview\WatcherConnector($root, $c->getSystemConfig());