Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 39 additions & 2 deletions src/Illuminate/Events/CallQueuedHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use Illuminate\Contracts\Queue\Job;
use Illuminate\Contracts\Container\Container;
use ReflectionMethod;

class CallQueuedHandler {

Expand Down Expand Up @@ -36,8 +37,10 @@ public function call(Job $job, array $data)
$job, $this->container->make($data['class'])
);

call_user_func_array(
[$handler, $data['method']], unserialize($data['data'])
$jobData = unserialize($data['data']);
$this->parameterizeJobDataDependencies($handler, $data['method'], $jobData);
$this->container->call(
[$handler, $data['method']], $jobData
);

if ( ! $job->isDeletedOrReleased())
Expand All @@ -46,6 +49,40 @@ public function call(Job $job, array $data)
}
}

/**
* Convert the job data into named parameters that the IoC container can use to resolve dependencies
*
* @param object $handler
* @param string $method
* @param array $data
* @return void
*/
protected function parameterizeJobDataDependencies($handler, $method, &$data)
{
foreach ((new ReflectionMethod($handler, $method))->getParameters() as $callbackKey => $callbackParameter)
{
$callbackParameterClass = ($callbackParameter->getClass()) ? $callbackParameter->getClass()->name : null;
if (empty($callbackParameterClass))
{
continue;
}
$callbackParameterName = $callbackParameter->name;
foreach ($data as $dataKey => &$possibleParameter)
{
if (!is_object($possibleParameter) || !is_numeric($dataKey))
{
continue;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use braces, or one line...

}
if (get_class($possibleParameter) == $callbackParameterClass)
{
$data[$callbackParameterName] = $possibleParameter;
unset($data[$dataKey]);
}
}
unset($possibleParameter);
}
}

/**
* Set the job instance of the given class if necessary.
*
Expand Down