2015-03-02 09:25:31 -05:00
|
|
|
<?php
|
2024-05-23 03:26:56 -04:00
|
|
|
|
2015-03-02 09:25:31 -05:00
|
|
|
/**
|
2024-05-23 03:26:56 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2015-03-02 09:25:31 -05:00
|
|
|
*/
|
|
|
|
|
namespace OC\Command;
|
|
|
|
|
|
|
|
|
|
use OCP\Command\IBus;
|
|
|
|
|
use OCP\Command\ICommand;
|
|
|
|
|
|
|
|
|
|
class QueueBus implements IBus {
|
|
|
|
|
/**
|
2017-10-18 08:15:03 -04:00
|
|
|
* @var ICommand[]|callable[]
|
2015-03-02 09:25:31 -05:00
|
|
|
*/
|
2015-03-24 05:02:48 -04:00
|
|
|
private $queue = [];
|
2015-03-02 09:25:31 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Schedule a command to be fired
|
|
|
|
|
*
|
|
|
|
|
* @param \OCP\Command\ICommand | callable $command
|
|
|
|
|
*/
|
|
|
|
|
public function push($command) {
|
|
|
|
|
$this->queue[] = $command;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Require all commands using a trait to be run synchronous
|
|
|
|
|
*
|
|
|
|
|
* @param string $trait
|
|
|
|
|
*/
|
|
|
|
|
public function requireSync($trait) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param \OCP\Command\ICommand | callable $command
|
|
|
|
|
*/
|
|
|
|
|
private function runCommand($command) {
|
|
|
|
|
if ($command instanceof ICommand) {
|
2015-03-24 05:46:29 -04:00
|
|
|
// ensure the command can be serialized
|
|
|
|
|
$serialized = serialize($command);
|
2020-04-10 08:19:56 -04:00
|
|
|
if (strlen($serialized) > 4000) {
|
2015-03-24 05:48:21 -04:00
|
|
|
throw new \InvalidArgumentException('Trying to push a command which serialized form can not be stored in the database (>4000 character)');
|
|
|
|
|
}
|
2015-03-24 05:46:29 -04:00
|
|
|
$unserialized = unserialize($serialized);
|
|
|
|
|
$unserialized->handle();
|
2015-03-02 09:25:31 -05:00
|
|
|
} else {
|
|
|
|
|
$command();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function run() {
|
|
|
|
|
while ($command = array_shift($this->queue)) {
|
|
|
|
|
$this->runCommand($command);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|