]> source.dussan.org Git - nextcloud-server.git/commitdiff
Allow apps to determine which commands should be run synchronous based on traints
authorRobin Appelman <icewind@owncloud.com>
Mon, 23 Feb 2015 14:25:59 +0000 (15:25 +0100)
committerRobin Appelman <icewind@owncloud.com>
Wed, 25 Feb 2015 14:09:41 +0000 (15:09 +0100)
lib/private/command/asyncbus.php
lib/public/command/ibus.php

index fc9c85acc3fb2a2f89960a44e7a5209705c63523..084842fa6f16e2a94e627d35c4c73775029576c1 100644 (file)
@@ -21,6 +21,13 @@ class AsyncBus implements IBus {
         */
        private $jobList;
 
+       /**
+        * List of traits for command which require sync execution
+        *
+        * @var string[]
+        */
+       private $syncTraits = [];
+
        /**
         * @param \OCP\BackgroundJob\IJobList $jobList
         */
@@ -34,7 +41,31 @@ class AsyncBus implements IBus {
         * @param \OCP\Command\ICommand | callable $command
         */
        public function push($command) {
-               $this->jobList->add($this->getJobClass($command), $this->serializeCommand($command));
+               if ($this->canRunAsync($command)) {
+                       $this->jobList->add($this->getJobClass($command), $this->serializeCommand($command));
+               } else {
+                       $this->runCommand($command);
+               }
+       }
+
+       /**
+        * Require all commands using a trait to be run synchronous
+        *
+        * @param string $trait
+        */
+       public function requireSync($trait) {
+               $this->syncTraits[] = trim($trait, '\\');
+       }
+
+       /**
+        * @param \OCP\Command\ICommand | callable $command
+        */
+       private function runCommand($command) {
+               if ($command instanceof ICommand) {
+                       $command->handle();
+               } else {
+                       $command();
+               }
        }
 
        /**
@@ -67,4 +98,30 @@ class AsyncBus implements IBus {
                        throw new \InvalidArgumentException('Invalid command');
                }
        }
+
+       /**
+        * @param \OCP\Command\ICommand | callable $command
+        * @return bool
+        */
+       private function canRunAsync($command) {
+               $traits = $this->getTraits($command);
+               foreach ($traits as $trait) {
+                       if (array_search($trait, $this->syncTraits) !== false) {
+                               return false;
+                       }
+               }
+               return true;
+       }
+
+       /**
+        * @param \OCP\Command\ICommand | callable $command
+        * @return string[]
+        */
+       private function getTraits($command) {
+               if ($command instanceof ICommand) {
+                       return class_uses($command);
+               } else {
+                       return [];
+               }
+       }
 }
index 707f8fd072d342cea368dcc64d88b63a68f3afec..bbb89ee04e6a1b5602dbfc45df489c1a6696deab 100644 (file)
@@ -15,4 +15,11 @@ interface IBus {
         * @param \OCP\Command\ICommand | callable $command
         */
        public function push($command);
+
+       /**
+        * Require all commands using a trait to be run synchronous
+        *
+        * @param string $trait
+        */
+       public function requireSync($trait);
 }