AsyncBus.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. /**
  26. * Asynchronous command bus that uses the background job system as backend
  27. */
  28. abstract class AsyncBus implements IBus {
  29. /**
  30. * List of traits for command which require sync execution
  31. *
  32. * @var string[]
  33. */
  34. private $syncTraits = [];
  35. /**
  36. * Schedule a command to be fired
  37. *
  38. * @param \OCP\Command\ICommand | callable $command
  39. */
  40. public function push($command) {
  41. if ($this->canRunAsync($command)) {
  42. $this->queueCommand($command);
  43. } else {
  44. $this->runCommand($command);
  45. }
  46. }
  47. /**
  48. * Queue a command in the bus
  49. *
  50. * @param \OCP\Command\ICommand | callable $command
  51. */
  52. abstract protected function queueCommand($command);
  53. /**
  54. * Require all commands using a trait to be run synchronous
  55. *
  56. * @param string $trait
  57. */
  58. public function requireSync($trait) {
  59. $this->syncTraits[] = trim($trait, '\\');
  60. }
  61. /**
  62. * @param \OCP\Command\ICommand | callable $command
  63. */
  64. private function runCommand($command) {
  65. if ($command instanceof ICommand) {
  66. $command->handle();
  67. } else {
  68. $command();
  69. }
  70. }
  71. /**
  72. * @param \OCP\Command\ICommand | callable $command
  73. * @return bool
  74. */
  75. private function canRunAsync($command) {
  76. $traits = $this->getTraits($command);
  77. foreach ($traits as $trait) {
  78. if (array_search($trait, $this->syncTraits) !== false) {
  79. return false;
  80. }
  81. }
  82. return true;
  83. }
  84. /**
  85. * @param \OCP\Command\ICommand | callable $command
  86. * @return string[]
  87. */
  88. private function getTraits($command) {
  89. if ($command instanceof ICommand) {
  90. return class_uses($command);
  91. } else {
  92. return [];
  93. }
  94. }
  95. }