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.

72 lines
1.8 KiB

10 years ago
  1. <?php
  2. /**
  3. * @author Robin Appelman <icewind@owncloud.com>
  4. *
  5. * @copyright Copyright (c) 2016, 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. class QueueBus implements IBus {
  25. /**
  26. * @var (ICommand|callable)[]
  27. */
  28. private $queue = [];
  29. /**
  30. * Schedule a command to be fired
  31. *
  32. * @param \OCP\Command\ICommand | callable $command
  33. */
  34. public function push($command) {
  35. $this->queue[] = $command;
  36. }
  37. /**
  38. * Require all commands using a trait to be run synchronous
  39. *
  40. * @param string $trait
  41. */
  42. public function requireSync($trait) {
  43. }
  44. /**
  45. * @param \OCP\Command\ICommand | callable $command
  46. */
  47. private function runCommand($command) {
  48. if ($command instanceof ICommand) {
  49. // ensure the command can be serialized
  50. $serialized = serialize($command);
  51. if(strlen($serialized) > 4000) {
  52. throw new \InvalidArgumentException('Trying to push a command which serialized form can not be stored in the database (>4000 character)');
  53. }
  54. $unserialized = unserialize($serialized);
  55. $unserialized->handle();
  56. } else {
  57. $command();
  58. }
  59. }
  60. public function run() {
  61. while ($command = array_shift($this->queue)) {
  62. $this->runCommand($command);
  63. }
  64. }
  65. }