Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.7] Queued Closures #25777

Merged
merged 6 commits into from
Sep 26, 2018
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"league/flysystem": "^1.0.8",
"monolog/monolog": "^1.12",
"nesbot/carbon": "^1.26.3",
"opis/closure": "^3.1",
GrahamCampbell marked this conversation as resolved.
Show resolved Hide resolved
"psr/container": "^1.0",
"psr/simple-cache": "^1.0",
"ramsey/uuid": "^3.7",
Expand Down
6 changes: 6 additions & 0 deletions src/Illuminate/Foundation/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
use Illuminate\Support\Carbon;
use Illuminate\Support\HtmlString;
use Illuminate\Container\Container;
use Illuminate\Queue\CallQueuedClosure;
use Illuminate\Contracts\Bus\Dispatcher;
use Illuminate\Queue\SerializableClosure;
use Illuminate\Contracts\Auth\Access\Gate;
use Illuminate\Contracts\Support\Responsable;
use Illuminate\Contracts\Routing\UrlGenerator;
Expand Down Expand Up @@ -390,6 +392,10 @@ function decrypt($value, $unserialize = true)
*/
function dispatch($job)
{
if ($job instanceof Closure) {
$job = new CallQueuedClosure(new SerializableClosure($job));
}

return new PendingDispatch($job);
}
}
Expand Down
62 changes: 62 additions & 0 deletions src/Illuminate/Queue/CallQueuedClosure.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Illuminate\Queue;

use ReflectionFunction;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Contracts\Container\Container;

class CallQueuedClosure implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

/**
* The serializable Closure instance.
*
* @var \Illuminate\Queue\SerializableClosure
*/
public $closure;

/**
* Indicate if the job should be deleted when models are missing.
*
* @var bool
*/
public $deleteWhenMissingModels = true;

/**
* Create a new job instance.
*
* @param \Illuminate\Queue\SerializableClosure
* @return void
*/
public function __construct(SerializableClosure $closure)
{
$this->closure = $closure;
}

/**
* Execute the job.
*
* @param \Illuminate\Contracts\Container\Container $container
* @return void
*/
public function handle(Container $container)
{
$container->call($this->closure->getClosure());
}

/**
* Get the display name for the queued job.
*
* @return string
*/
public function displayName()
{
$reflection = new ReflectionFunction($this->closure->getClosure());

return 'Closure ('.basename($reflection->getFileName()).':'.$reflection->getStartLine().')';
}
}
21 changes: 17 additions & 4 deletions src/Illuminate/Queue/QueueServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Illuminate\Queue;

use Illuminate\Support\Str;
use Opis\Closure\SerializableClosure;
use Illuminate\Support\ServiceProvider;
use Illuminate\Queue\Connectors\SqsConnector;
use Illuminate\Queue\Connectors\NullConnector;
Expand Down Expand Up @@ -30,14 +32,11 @@ class QueueServiceProvider extends ServiceProvider
public function register()
{
$this->registerManager();

$this->registerConnection();

$this->registerWorker();

$this->registerListener();

$this->registerFailedJobServices();
$this->registerOpisSecurityKey();
}

/**
Expand Down Expand Up @@ -215,6 +214,20 @@ protected function databaseFailedJobProvider($config)
);
}

/**
* Configure Opis Closure signing for security.
*
* @return void
*/
protected function registerOpisSecurityKey()
{
if (Str::startsWith($key = $this->app['config']->get('app.key'), 'base64:')) {
$key = base64_decode(substr($key, 7));
}

SerializableClosure::setSecretKey($key);
}

/**
* Get the services provided by the provider.
*
Expand Down
40 changes: 40 additions & 0 deletions src/Illuminate/Queue/SerializableClosure.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Illuminate\Queue;

use Opis\Closure\SerializableClosure as OpisSerializableClosure;

class SerializableClosure extends OpisSerializableClosure
{
use SerializesAndRestoresModelIdentifiers;

/**
* Transform the use variables before serialization.
*
* @param array $data The Closure's use variables
* @return array
*/
protected function transformUseVariables($data)
{
foreach ($data as $key => $value) {
$data[$key] = $this->getSerializedPropertyValue($value);
}

return $data;
}

/**
* Resolve the use variables after unserialization.
*
* @param array $data The Closure's transformed use variables
* @return array
*/
protected function resolveUseVariables($data)
{
foreach ($data as $key => $value) {
$data[$key] = $this->getRestoredPropertyValue($value);
}

return $data;
}
}
1 change: 1 addition & 0 deletions src/Illuminate/Queue/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"illuminate/database": "5.7.*",
"illuminate/filesystem": "5.7.*",
"illuminate/support": "5.7.*",
"opis/closure": "^3.1",
"symfony/debug": "^4.1",
"symfony/process": "^4.1"
},
Expand Down