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 1 commit
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
Next Next commit
Initial pass at queuable closures.
This reintroduces a feature that was previously present in early versions of Laravel queues. However, there have been improvements to serializable closure libraries in the meantime which allows for better security and better handling of Eloquent models and collections. Namely, signing closures with a hash to prevent modification and arbitrary code execution, as well as transforming and resolving models and collections to ModelIdentifier instances.
  • Loading branch information
taylorotwell committed Sep 23, 2018
commit 49f37fd85660d5c56e41a68d534c7a8add571eac
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
64 changes: 64 additions & 0 deletions src/Illuminate/Queue/CallQueuedClosure.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace Illuminate\Queue;

use ReflectionFunction;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
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 = config('app.key'), 'base64:')) {
Copy link
Member

Choose a reason for hiding this comment

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

The config function exists in the foundation namespace, so queue shouldn't really depend on it if we want to continue to allow the queue package to be required by arbitrary software projects. Also, the duplication of the base64 decoding logic is a bit gross.

$key = base64_decode(substr($key, 7));
}

SerializableClosure::setSecretKey($key);
}

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

namespace Illuminate\Queue;

use Illuminate\Queue\SerializesAndRestoresModelIdentifiers;
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;
}
}