QueueBus.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\Command;
  8. use OCP\Command\IBus;
  9. use OCP\Command\ICommand;
  10. class QueueBus implements IBus {
  11. /**
  12. * @var ICommand[]|callable[]
  13. */
  14. private $queue = [];
  15. /**
  16. * Schedule a command to be fired
  17. *
  18. * @param \OCP\Command\ICommand | callable $command
  19. */
  20. public function push($command) {
  21. $this->queue[] = $command;
  22. }
  23. /**
  24. * Require all commands using a trait to be run synchronous
  25. *
  26. * @param string $trait
  27. */
  28. public function requireSync($trait) {
  29. }
  30. /**
  31. * @param \OCP\Command\ICommand | callable $command
  32. */
  33. private function runCommand($command) {
  34. if ($command instanceof ICommand) {
  35. // ensure the command can be serialized
  36. $serialized = serialize($command);
  37. if (strlen($serialized) > 4000) {
  38. throw new \InvalidArgumentException('Trying to push a command which serialized form can not be stored in the database (>4000 character)');
  39. }
  40. $unserialized = unserialize($serialized);
  41. $unserialized->handle();
  42. } else {
  43. $command();
  44. }
  45. }
  46. public function run() {
  47. while ($command = array_shift($this->queue)) {
  48. $this->runCommand($command);
  49. }
  50. }
  51. }