首页 > 解决方案 > Laravel 8 从有效负载中反序列化命令的问题(在失败的作业中)

问题描述

我是 Laravel 的绝对初学者并且有问题。

我需要将有效负载转换为格式化字符串。这是 failed_jobs 表中的有效负载:

从 failed_jobs 表中获取的原始有效负载是:

    {"retries":5,"data":{"command":"O:23:\"App\\Jobs\\SearchForEvent\":11:{s:8:\"\u0000*\u0000alert\";O:45:\"Illuminate\\Contracts\\Database\\ModelIdentifier\":4:{s:5:\"class\";s:16:\"App\\Models\\Alert\";s:2:\"id\";i:1;s:9:\"relations\";a:0:{}s:10:\"connection\";s:5:\"mysql\";}s:12:\"\u0000*\u0000startDate\";N;s:10:\"\u0000*\u0000endDate\";N;s:3:\"job\";N;s:10:\"connection\";N;s:5:\"queue\";N;s:15:\"chainConnection\";N;s:10:\"chainQueue\";N;s:5:\"delay\";N;s:10:\"middleware\";a:0:{}s:7:\"chained\";a:0:{}}","commandName":"App\\Jobs\\SearchForEvent"},"maxTries":null,"attempts":0,"uuid":"44543b2d-d724-443f-b1b9-8cd4c6eb9d6c","timeout":null,"tags":["App\\Models\\Alert:1"],"id":"44543b2d-d724-443f-b1b9-8cd4c6eb9d6c","retry_of":"5e268070-b783-419a-b111-313db95a8cf9","displayName":"App\\Jobs\\SearchForEvent","job":"Illuminate\\Queue\\CallQueuedHandler@call","type":"job","pushedAt":"1617265554.6785","retryUntil":null,"maxExceptions":null,"delay":null,"timeoutAt":null}

我需要这样的输出结果:

有效载荷:

    retries: 5
    data:
      command: 
      {
         alert: {
           class: "App\Models\Alert",
           id: 1,
           relations: [
           ],
           connection: "mysql"
         },
         startDate: null,
         endDate: null,
         job: null,
         connection: null,
         queue: null,
         chainConnection: null,
         chainQueue: null,
         delay: null,
         middleware: [
         ],
         chained: [
         ]
    }
    commandName: App\Jobs\SearchForEvent
  maxTries: 
  attempts: 0
  uuid: 44543b2d-d724-443f-b1b9-8cd4c6eb9d6c
  timeout: 
  tags:
    0: App\Models\Alert:1
  id: 44543b2d-d724-443f-b1b9-8cd4c6eb9d6c
  retry_of: 5e268070-b783-419a-b111-313db95a8cf9
  displayName: App\Jobs\SearchForEvent
  job: Illuminate\Queue\CallQueuedHandler@call
  type: job
  pushedAt: 1617265554.6785
  retryUntil: 
  maxExceptions: 
  delay: 
  timeoutAt: 

我有一些解决方案,但看起来还不够好。解析有效载荷不是问题(但反序列化命令是):

    public function findFailedJob(int $id): void
    {
        $this->failedJobObj = $this->model->find($id);
        $this->rawPayload = $this->failedJobObj->payload;
        $payloadArray = json_decode($this->failedJobObj->payload, 1);
        $this->manageCommand();
        $this->makeHumanReadablePayload($payloadArray);
    }

解析命令是我的问题。它包含空代码“\u0000”,这会使反序列化出现问题。我已经从演示中制作了非常糟糕的方法:

private function manageCommand()
{
    $payload = json_decode($this->failedJobObj->payload, true);
    if (! isset($payload['data']['command'])) {
        return '';
    }

    $str = str_replace('\u0000', ' ', $payload['data']['command']);
    $unserialized = unserialize($str, ['allowed_classes' => false]);
    $this->command = print_r($unserialized, 1);
}

结果它返回:

命令:

__PHP_Incomplete_Class Object
(
    [__PHP_Incomplete_Class_Name] => App\Jobs\SearchForEvent
    [alert:protected] => __PHP_Incomplete_Class Object
        (
            [__PHP_Incomplete_Class_Name] => Illuminate\Contracts\Database\ModelIdentifier
            [class] => App\Models\Alert
            [id] => 1
            [relations] => Array
                (
                )

            [connection] => mysql
        )

    [startDate:protected] => 
    [endDate:protected] => 
    [job] => 
    [connection] => 
    [queue] => 
    [chainConnection] => 
    [chainQueue] => 
    [delay] => 
    [middleware] => Array
        (
        )

    [chained] => Array
        (
        )

)

对包含“\u0000”的反序列化“命令”有更好的解决方案吗?

提前致谢!

在此处输入图像描述

标签: laravelserializationunicode

解决方案


推荐阅读