基本知识点包括: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;
}