Skip to content

Commit 2832ba6

Browse files
authored
Support encryption, avoid double __unserialize call
Laravel uses the magic method `__unserialize()` in the `SerializesModels` trait, which does a bunch of work to rehydrate models attached to jobs. This gets called every time the job is unserialized. The addition of an `unserialize()` call in the constructor to get the `queue` property causes it to run twice because of the original call to `unserialize()` is in `\Illuminate\Queue\CallQueuedHandler->getCommand()`. `CallQueuedHandler->getCommand()` also implements support for encrypted command payloads. This change brings over the encryption support logic from `CallQueuedHandler->getCommand()` and passed `['allowed_classes' => false]` to the `unserialize()` call. This will make it _not_ hydrate to the original job object, but instead to an instance of `__PHP_Incomplete_Class` avoiding the unnecessary call to `__unserialize()` with this `unserialize()` call. We then cast to an `(array)` to access the `queue` property without issue.
1 parent 7b0f6ae commit 2832ba6

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

src/CloudTasksJob.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,13 @@ public function __construct(array $job, CloudTasksQueue $cloudTasksQueue)
2424
$this->job = $job;
2525
$this->container = Container::getInstance();
2626
$this->cloudTasksQueue = $cloudTasksQueue;
27-
/** @var \stdClass $command */
28-
$command = unserialize($job['data']['command']);
29-
$this->queue = $command->queue;
27+
28+
if (Str::startsWith($job['data']['command'], 'O:')) {
29+
$command = (array) unserialize($job['data']['command'], ['allowed_classes' => false]);
30+
} elseif ($this->container->bound(Encrypter::class)) {
31+
$command = (array) unserialize($this->container[Encrypter::class]->decrypt($job['data']['command']), ['allowed_classes' => false]);
32+
}
33+
$this->queue = $command['queue'];
3034
}
3135

3236
public function getJobId(): string

0 commit comments

Comments
 (0)