Skip to content

Commit

Permalink
Only run preg_match when unserialize fails
Browse files Browse the repository at this point in the history
  • Loading branch information
hapidjus committed Oct 25, 2024
1 parent a6920bd commit 3036570
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/Watchers/JobWatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@
namespace Laravel\Telescope\Watchers;

use Illuminate\Bus\BatchRepository;
use Illuminate\Contracts\Encryption\Encrypter;
use Illuminate\Queue\Events\JobFailed;
use Illuminate\Queue\Events\JobProcessed;
use Illuminate\Queue\Queue;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Laravel\Telescope\EntryType;
use Laravel\Telescope\EntryUpdate;
use Laravel\Telescope\ExceptionContext;
use Laravel\Telescope\ExtractProperties;
use Laravel\Telescope\ExtractTags;
use Laravel\Telescope\IncomingEntry;
use Laravel\Telescope\Telescope;
use RuntimeException;

class JobWatcher extends Watcher
{
Expand Down Expand Up @@ -228,8 +231,21 @@ protected function updateBatch($payload)

private function getBatchId(array $data)
{
if (preg_match('/"batchId";s:\d+:"([^"]+)"/', $data['command'], $matches)) {
return $matches[1];
try {
if (Str::startsWith($data['command'], 'O:')) {
$unserialized = unserialize($data['command']);
} elseif (app()->bound(Encrypter::class)) {
$unserialized = unserialize(app(Encrypter::class)->decrypt($data['command']));
} else {
throw new RuntimeException('Unable to extract job payload.');
}

$properties = ExtractProperties::from($unserialized);
return $properties['batchId'];
} catch (\Exception | \Error) {
if (preg_match('/"batchId";s:\d+:"([^"]+)"/', $data['command'], $matches)) {
return $matches[1];
}
}

return null;
Expand Down

0 comments on commit 3036570

Please sign in to comment.