AsyncBus.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Robin Appelman <robin@icewind.nl>
  6. *
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace OC\Command;
  23. use OCP\Command\IBus;
  24. use OCP\Command\ICommand;
  25. use SuperClosure\Serializer;
  26. /**
  27. * Asynchronous command bus that uses the background job system as backend
  28. */
  29. class AsyncBus implements IBus {
  30. /**
  31. * @var \OCP\BackgroundJob\IJobList
  32. */
  33. private $jobList;
  34. /**
  35. * List of traits for command which require sync execution
  36. *
  37. * @var string[]
  38. */
  39. private $syncTraits = [];
  40. /**
  41. * @param \OCP\BackgroundJob\IJobList $jobList
  42. */
  43. function __construct($jobList) {
  44. $this->jobList = $jobList;
  45. }
  46. /**
  47. * Schedule a command to be fired
  48. *
  49. * @param \OCP\Command\ICommand | callable $command
  50. */
  51. public function push($command) {
  52. if ($this->canRunAsync($command)) {
  53. $this->jobList->add($this->getJobClass($command), $this->serializeCommand($command));
  54. } else {
  55. $this->runCommand($command);
  56. }
  57. }
  58. /**
  59. * Require all commands using a trait to be run synchronous
  60. *
  61. * @param string $trait
  62. */
  63. public function requireSync($trait) {
  64. $this->syncTraits[] = trim($trait, '\\');
  65. }
  66. /**
  67. * @param \OCP\Command\ICommand | callable $command
  68. */
  69. private function runCommand($command) {
  70. if ($command instanceof ICommand) {
  71. $command->handle();
  72. } else {
  73. $command();
  74. }
  75. }
  76. /**
  77. * @param \OCP\Command\ICommand | callable $command
  78. * @return string
  79. */
  80. private function getJobClass($command) {
  81. if ($command instanceof \Closure) {
  82. return 'OC\Command\ClosureJob';
  83. } else if (is_callable($command)) {
  84. return 'OC\Command\CallableJob';
  85. } else if ($command instanceof ICommand) {
  86. return 'OC\Command\CommandJob';
  87. } else {
  88. throw new \InvalidArgumentException('Invalid command');
  89. }
  90. }
  91. /**
  92. * @param \OCP\Command\ICommand | callable $command
  93. * @return string
  94. */
  95. private function serializeCommand($command) {
  96. if ($command instanceof \Closure) {
  97. $serializer = new Serializer();
  98. return $serializer->serialize($command);
  99. } else if (is_callable($command) or $command instanceof ICommand) {
  100. return serialize($command);
  101. } else {
  102. throw new \InvalidArgumentException('Invalid command');
  103. }
  104. }
  105. /**
  106. * @param \OCP\Command\ICommand | callable $command
  107. * @return bool
  108. */
  109. private function canRunAsync($command) {
  110. $traits = $this->getTraits($command);
  111. foreach ($traits as $trait) {
  112. if (array_search($trait, $this->syncTraits) !== false) {
  113. return false;
  114. }
  115. }
  116. return true;
  117. }
  118. /**
  119. * @param \OCP\Command\ICommand | callable $command
  120. * @return string[]
  121. */
  122. private function getTraits($command) {
  123. if ($command instanceof ICommand) {
  124. return class_uses($command);
  125. } else {
  126. return [];
  127. }
  128. }
  129. }