Skip to content

Commit 6911a33

Browse files
Merge pull request #55748 from nextcloud/bugfix/noid/closure-job
2 parents df8d838 + 34f1ae6 commit 6911a33

File tree

12 files changed

+75
-214
lines changed

12 files changed

+75
-214
lines changed

build/psalm-baseline.xml

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1795,6 +1795,9 @@
17951795
<code><![CDATA[\OC_Util::tearDownFS()]]></code>
17961796
<code><![CDATA[\OC_Util::tearDownFS()]]></code>
17971797
</DeprecatedClass>
1798+
<DeprecatedInterface>
1799+
<code><![CDATA[Expire]]></code>
1800+
</DeprecatedInterface>
17981801
<DeprecatedMethod>
17991802
<code><![CDATA[\OC_Util::tearDownFS()]]></code>
18001803
<code><![CDATA[\OC_Util::tearDownFS()]]></code>
@@ -1826,6 +1829,9 @@
18261829
</DeprecatedMethod>
18271830
</file>
18281831
<file src="apps/files_trashbin/lib/Command/Size.php">
1832+
<DeprecatedInterface>
1833+
<code><![CDATA[private]]></code>
1834+
</DeprecatedInterface>
18291835
<DeprecatedMethod>
18301836
<code><![CDATA[getAppValue]]></code>
18311837
<code><![CDATA[setAppValue]]></code>
@@ -2046,6 +2052,11 @@
20462052
<code><![CDATA[\OC_Util::tearDownFS()]]></code>
20472053
</DeprecatedMethod>
20482054
</file>
2055+
<file src="apps/files_versions/lib/Command/Expire.php">
2056+
<DeprecatedInterface>
2057+
<code><![CDATA[Expire]]></code>
2058+
</DeprecatedInterface>
2059+
</file>
20492060
<file src="apps/files_versions/lib/Command/ExpireVersions.php">
20502061
<DeprecatedClass>
20512062
<code><![CDATA[\OC_Util::setupFS($user)]]></code>
@@ -2095,6 +2106,9 @@
20952106
<code><![CDATA[Files::streamCopy($source, $target, true)]]></code>
20962107
<code><![CDATA[\OC_Util::setupFS($uid)]]></code>
20972108
</DeprecatedClass>
2109+
<DeprecatedInterface>
2110+
<code><![CDATA[$bus]]></code>
2111+
</DeprecatedInterface>
20982112
<DeprecatedMethod>
20992113
<code><![CDATA[Files::streamCopy($source, $target, true)]]></code>
21002114
<code><![CDATA[dispatch]]></code>
@@ -3468,15 +3482,19 @@
34683482
}, $context->getCalendarProviders())]]></code>
34693483
</NamedArgumentNotAllowed>
34703484
</file>
3471-
<file src="lib/private/Command/CallableJob.php">
3472-
<ParamNameMismatch>
3473-
<code><![CDATA[$serializedCallable]]></code>
3474-
</ParamNameMismatch>
3485+
<file src="lib/private/Command/CommandJob.php">
3486+
<UndefinedClass>
3487+
<code><![CDATA[\Test\Command\FilesystemCommand]]></code>
3488+
<code><![CDATA[\Test\Command\SimpleCommand]]></code>
3489+
<code><![CDATA[\Test\Command\StateFullCommand]]></code>
3490+
</UndefinedClass>
34753491
</file>
3476-
<file src="lib/private/Command/ClosureJob.php">
3477-
<InvalidArgument>
3478-
<code><![CDATA[[LaravelClosure::class]]]></code>
3479-
</InvalidArgument>
3492+
<file src="lib/private/Command/QueueBus.php">
3493+
<UndefinedClass>
3494+
<code><![CDATA[\Test\Command\FilesystemCommand]]></code>
3495+
<code><![CDATA[\Test\Command\SimpleCommand]]></code>
3496+
<code><![CDATA[\Test\Command\StateFullCommand]]></code>
3497+
</UndefinedClass>
34803498
</file>
34813499
<file src="lib/private/Comments/Manager.php">
34823500
<RedundantCast>

lib/composer/composer/autoload_classmap.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,8 +1220,6 @@
12201220
'OC\\Collaboration\\Resources\\Resource' => $baseDir . '/lib/private/Collaboration/Resources/Resource.php',
12211221
'OC\\Color' => $baseDir . '/lib/private/Color.php',
12221222
'OC\\Command\\AsyncBus' => $baseDir . '/lib/private/Command/AsyncBus.php',
1223-
'OC\\Command\\CallableJob' => $baseDir . '/lib/private/Command/CallableJob.php',
1224-
'OC\\Command\\ClosureJob' => $baseDir . '/lib/private/Command/ClosureJob.php',
12251223
'OC\\Command\\CommandJob' => $baseDir . '/lib/private/Command/CommandJob.php',
12261224
'OC\\Command\\CronBus' => $baseDir . '/lib/private/Command/CronBus.php',
12271225
'OC\\Command\\FileAccess' => $baseDir . '/lib/private/Command/FileAccess.php',

lib/composer/composer/autoload_static.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,8 +1261,6 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
12611261
'OC\\Collaboration\\Resources\\Resource' => __DIR__ . '/../../..' . '/lib/private/Collaboration/Resources/Resource.php',
12621262
'OC\\Color' => __DIR__ . '/../../..' . '/lib/private/Color.php',
12631263
'OC\\Command\\AsyncBus' => __DIR__ . '/../../..' . '/lib/private/Command/AsyncBus.php',
1264-
'OC\\Command\\CallableJob' => __DIR__ . '/../../..' . '/lib/private/Command/CallableJob.php',
1265-
'OC\\Command\\ClosureJob' => __DIR__ . '/../../..' . '/lib/private/Command/ClosureJob.php',
12661264
'OC\\Command\\CommandJob' => __DIR__ . '/../../..' . '/lib/private/Command/CommandJob.php',
12671265
'OC\\Command\\CronBus' => __DIR__ . '/../../..' . '/lib/private/Command/CronBus.php',
12681266
'OC\\Command\\FileAccess' => __DIR__ . '/../../..' . '/lib/private/Command/FileAccess.php',

lib/private/Command/AsyncBus.php

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
/**
46
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
57
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
@@ -19,14 +21,12 @@ abstract class AsyncBus implements IBus {
1921
*
2022
* @var string[]
2123
*/
22-
private $syncTraits = [];
24+
private array $syncTraits = [];
2325

2426
/**
2527
* Schedule a command to be fired
26-
*
27-
* @param \OCP\Command\ICommand | callable $command
2828
*/
29-
public function push($command) {
29+
public function push(ICommand $command): void {
3030
if ($this->canRunAsync($command)) {
3131
$this->queueCommand($command);
3232
} else {
@@ -36,54 +36,39 @@ public function push($command) {
3636

3737
/**
3838
* Queue a command in the bus
39-
*
40-
* @param \OCP\Command\ICommand | callable $command
4139
*/
42-
abstract protected function queueCommand($command);
40+
abstract protected function queueCommand(ICommand $command);
4341

4442
/**
4543
* Require all commands using a trait to be run synchronous
4644
*
4745
* @param string $trait
4846
*/
49-
public function requireSync($trait) {
47+
public function requireSync(string $trait): void {
5048
$this->syncTraits[] = trim($trait, '\\');
5149
}
5250

53-
/**
54-
* @param \OCP\Command\ICommand | callable $command
55-
*/
56-
private function runCommand($command) {
57-
if ($command instanceof ICommand) {
58-
$command->handle();
59-
} else {
60-
$command();
61-
}
51+
private function runCommand(ICommand $command): void {
52+
$command->handle();
6253
}
6354

6455
/**
65-
* @param \OCP\Command\ICommand | callable $command
6656
* @return bool
6757
*/
68-
private function canRunAsync($command) {
58+
private function canRunAsync(ICommand $command): bool {
6959
$traits = $this->getTraits($command);
7060
foreach ($traits as $trait) {
71-
if (in_array($trait, $this->syncTraits)) {
61+
if (in_array($trait, $this->syncTraits, true)) {
7262
return false;
7363
}
7464
}
7565
return true;
7666
}
7767

7868
/**
79-
* @param \OCP\Command\ICommand | callable $command
8069
* @return string[]
8170
*/
82-
private function getTraits($command) {
83-
if ($command instanceof ICommand) {
84-
return class_uses($command);
85-
} else {
86-
return [];
87-
}
71+
private function getTraits(ICommand $command): array {
72+
return class_uses($command);
8873
}
8974
}

lib/private/Command/CallableJob.php

Lines changed: 0 additions & 21 deletions
This file was deleted.

lib/private/Command/ClosureJob.php

Lines changed: 0 additions & 23 deletions
This file was deleted.

lib/private/Command/CommandJob.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,13 @@
1515
*/
1616
class CommandJob extends QueuedJob {
1717
protected function run($argument) {
18-
$command = unserialize($argument);
18+
$command = unserialize($argument, ['allowed_classes' => [
19+
\Test\Command\SimpleCommand::class,
20+
\Test\Command\StateFullCommand::class,
21+
\Test\Command\FilesystemCommand::class,
22+
\OCA\Files_Trashbin\Command\Expire::class,
23+
\OCA\Files_Versions\Command\Expire::class,
24+
]]);
1925
if ($command instanceof ICommand) {
2026
$command->handle();
2127
} else {

lib/private/Command/CronBus.php

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
/**
46
* SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
57
* SPDX-License-Identifier: AGPL-3.0-or-later
68
*/
79
namespace OC\Command;
810

9-
use Laravel\SerializableClosure\SerializableClosure;
10-
use OCP\BackgroundJob\IJob;
1111
use OCP\BackgroundJob\IJobList;
1212
use OCP\Command\ICommand;
1313

@@ -17,37 +17,7 @@ public function __construct(
1717
) {
1818
}
1919

20-
protected function queueCommand($command): void {
21-
$this->jobList->add($this->getJobClass($command), $this->serializeCommand($command));
22-
}
23-
24-
/**
25-
* @param ICommand|callable $command
26-
* @return class-string<IJob>
27-
*/
28-
private function getJobClass($command): string {
29-
if ($command instanceof \Closure) {
30-
return ClosureJob::class;
31-
} elseif (is_callable($command)) {
32-
return CallableJob::class;
33-
} elseif ($command instanceof ICommand) {
34-
return CommandJob::class;
35-
} else {
36-
throw new \InvalidArgumentException('Invalid command');
37-
}
38-
}
39-
40-
/**
41-
* @param ICommand|callable $command
42-
* @return string
43-
*/
44-
private function serializeCommand($command): string {
45-
if ($command instanceof \Closure) {
46-
return serialize(new SerializableClosure($command));
47-
} elseif (is_callable($command) || $command instanceof ICommand) {
48-
return serialize($command);
49-
} else {
50-
throw new \InvalidArgumentException('Invalid command');
51-
}
20+
protected function queueCommand(ICommand $command): void {
21+
$this->jobList->add(CommandJob::class, serialize($command));
5222
}
5323
}

lib/private/Command/QueueBus.php

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
/**
46
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
57
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
@@ -12,45 +14,40 @@
1214

1315
class QueueBus implements IBus {
1416
/**
15-
* @var ICommand[]|callable[]
17+
* @var ICommand[]
1618
*/
17-
private $queue = [];
19+
private array $queue = [];
1820

1921
/**
2022
* Schedule a command to be fired
21-
*
22-
* @param \OCP\Command\ICommand | callable $command
2323
*/
24-
public function push($command) {
24+
public function push(ICommand $command): void {
2525
$this->queue[] = $command;
2626
}
2727

2828
/**
2929
* Require all commands using a trait to be run synchronous
30-
*
31-
* @param string $trait
3230
*/
33-
public function requireSync($trait) {
31+
public function requireSync(string $trait): void {
3432
}
3533

36-
/**
37-
* @param \OCP\Command\ICommand | callable $command
38-
*/
39-
private function runCommand($command) {
40-
if ($command instanceof ICommand) {
41-
// ensure the command can be serialized
42-
$serialized = serialize($command);
43-
if (strlen($serialized) > 4000) {
44-
throw new \InvalidArgumentException('Trying to push a command which serialized form can not be stored in the database (>4000 character)');
45-
}
46-
$unserialized = unserialize($serialized);
47-
$unserialized->handle();
48-
} else {
49-
$command();
34+
private function runCommand(ICommand $command): void {
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)');
5039
}
40+
$unserialized = unserialize($serialized, ['allowed_classes' => [
41+
\Test\Command\SimpleCommand::class,
42+
\Test\Command\StateFullCommand::class,
43+
\Test\Command\FilesystemCommand::class,
44+
\OCA\Files_Trashbin\Command\Expire::class,
45+
\OCA\Files_Versions\Command\Expire::class,
46+
]]);
47+
$unserialized->handle();
5148
}
5249

53-
public function run() {
50+
public function run(): void {
5451
while ($command = array_shift($this->queue)) {
5552
$this->runCommand($command);
5653
}

lib/public/Command/IBus.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,24 @@
1010
/**
1111
* Interface IBus
1212
*
13+
* @deprecated 33.0.0 The interface is considered internal going forward and should not be implemented by apps anymore
1314
* @since 8.1.0
1415
*/
1516
interface IBus {
1617
/**
1718
* Schedule a command to be fired
1819
*
19-
* @param \OCP\Command\ICommand | callable $command
20+
* @param \OCP\Command\ICommand $command
21+
* @since 33.0.0 Only allowed for {@see \OCA\Files_Trashbin\Command\Expire} and {@see \OCA\Files_Versions\Command\Expire}
2022
* @since 8.1.0
2123
*/
22-
public function push($command);
24+
public function push(ICommand $command): void;
2325

2426
/**
2527
* Require all commands using a trait to be run synchronous
2628
*
2729
* @param string $trait
2830
* @since 8.1.0
2931
*/
30-
public function requireSync($trait);
32+
public function requireSync(string $trait): void;
3133
}

0 commit comments

Comments
 (0)