123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- <?php
- namespace OC\Command;
- use OCP\Command\IBus;
- use OCP\Command\ICommand;
- abstract class AsyncBus implements IBus {
-
- private $syncTraits = [];
-
- public function push($command) {
- if ($this->canRunAsync($command)) {
- $this->queueCommand($command);
- } else {
- $this->runCommand($command);
- }
- }
-
- abstract protected function queueCommand($command);
-
- public function requireSync($trait) {
- $this->syncTraits[] = trim($trait, '\\');
- }
-
- private function runCommand($command) {
- if ($command instanceof ICommand) {
- $command->handle();
- } else {
- $command();
- }
- }
-
- private function canRunAsync($command) {
- $traits = $this->getTraits($command);
- foreach ($traits as $trait) {
- if (in_array($trait, $this->syncTraits)) {
- return false;
- }
- }
- return true;
- }
-
- private function getTraits($command) {
- if ($command instanceof ICommand) {
- return class_uses($command);
- } else {
- return [];
- }
- }
- }
|