Laravel DatabaseQueue 基本知识点

本文介绍了一个基于PHP的队列系统中如何创建并解析队列任务的有效载荷。通过对闭包、对象和平坦数据的不同处理方式,实现了任务的有效序列化及反序列化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

基本知识点包括:Container; php serialize, deseriaize; json_encode, json_decode.

framework/src/Illuminate/Queue/DatabaseQueue.php

    /**
     * Create a payload string from the given job and data.
     *
     * @param  string  $job
     * @param  mixed   $data
     * @param  string  $queue
     * @return string
     */
    protected function createPayload($job, $data = '', $queue = null)
    {
        if ($job instanceof Closure) {
            return json_encode($this->createClosurePayload($job, $data));
        }

        if (is_object($job)) {
            return json_encode([
                'job' => 'Illuminate\Queue\CallQueuedHandler@call',
                'data' => ['commandName' => get_class($job), 'command' => serialize(clone $job)],
            ]);
        }

        return json_encode($this->createPlainPayload($job, $data));
    }

framework/src/Illuminate/Queue/Jobs/Job.php

    protected function resolveAndFire(array $payload)
    {
        list($class, $method) = $this->parseJob($payload['job']);

        $this->instance = $this->resolve($class);

        $this->instance->{$method}($this, $this->resolveQueueableEntities($payload['data']));
    }

    /**
     * Parse the job declaration into class and method.
     *
     * @param  string  $job
     * @return array
     */
    protected function parseJob($job)
    {
        $segments = explode('@', $job);

        return count($segments) > 1 ? $segments : [$segments[0], 'fire'];
    }

    /**
     * Resolve the given job handler.
     *
     * @param  string  $class
     * @return mixed
     */
    protected function resolve($class)
    {
        return $this->container->make($class);
    }

    /**
     * Resolve all of the queueable entities in the given payload.
     *
     * @param  mixed  $data
     * @return mixed
     */
    protected function resolveQueueableEntities($data)
    {
        if (is_string($data)) {
            return $this->resolveQueueableEntity($data);
        }

        if (is_array($data)) {
            $data = array_map(function ($d) {
                if (is_array($d)) {
                    return $this->resolveQueueableEntities($d);
                }

                return $this->resolveQueueableEntity($d);
            }, $data);
        }

        return $data;
    }
     /**
     * Resolve a single queueable entity from the resolver.
     *
     * @param  mixed  $value
     * @return \Illuminate\Contracts\Queue\QueueableEntity
     */
    protected function resolveQueueableEntity($value)
    {
        if (is_string($value) && Str::startsWith($value, '::entity::')) {
            list($marker, $type, $id) = explode('|', $value, 3);

            return $this->getEntityResolver()->resolve($type, $id);
        }

        return $value;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值