Skip to content

Commit 38e8b6e

Browse files
committed
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 38e8b6e

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/CloudTasksJob.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ 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+
$command = TaskHandler::getCommandProperties($job['data']['command']);
29+
$this->queue = $command['queue'] ?? config('queue.connections.' .config('queue.default') . '.queue');
3030
}
3131

3232
public function getJobId(): string

src/TaskHandler.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,17 @@ private function loadQueueRetryConfig(CloudTasksJob $job): void
131131

132132
$this->retryConfig = CloudTasksApi::getRetryConfig($queueName);
133133
}
134+
135+
public static function getCommandProperties(string $command): array
136+
{
137+
if (Str::startsWith($command, 'O:')) {
138+
return (array) unserialize($command, ['allowed_classes' => false]);
139+
}
140+
141+
if (app()->bound(Encrypter::class)) {
142+
return (array) unserialize(app(Encrypter::class)->decrypt($command), ['allowed_classes' => false]);
143+
}
144+
145+
return [];
146+
}
134147
}

0 commit comments

Comments
 (0)