QueueBus.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Julius Härtl <jus@bitgrid.net>
  7. * @author Robin Appelman <robin@icewind.nl>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OC\Command;
  25. use OCP\Command\IBus;
  26. use OCP\Command\ICommand;
  27. class QueueBus implements IBus {
  28. /**
  29. * @var ICommand[]|callable[]
  30. */
  31. private $queue = [];
  32. /**
  33. * Schedule a command to be fired
  34. *
  35. * @param \OCP\Command\ICommand | callable $command
  36. */
  37. public function push($command) {
  38. $this->queue[] = $command;
  39. }
  40. /**
  41. * Require all commands using a trait to be run synchronous
  42. *
  43. * @param string $trait
  44. */
  45. public function requireSync($trait) {
  46. }
  47. /**
  48. * @param \OCP\Command\ICommand | callable $command
  49. */
  50. private function runCommand($command) {
  51. if ($command instanceof ICommand) {
  52. // ensure the command can be serialized
  53. $serialized = serialize($command);
  54. if (strlen($serialized) > 4000) {
  55. throw new \InvalidArgumentException('Trying to push a command which serialized form can not be stored in the database (>4000 character)');
  56. }
  57. $unserialized = unserialize($serialized);
  58. $unserialized->handle();
  59. } else {
  60. $command();
  61. }
  62. }
  63. public function run() {
  64. while ($command = array_shift($this->queue)) {
  65. $this->runCommand($command);
  66. }
  67. }
  68. }