You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

asyncbus.php 3.3KB

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