Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Allow specification of methods when pushing to Queue.

  • Loading branch information...
commit 55ba4afccd9680753d8948d423124c2caf6e64c0 1 parent 253b049
@taylorotwell taylorotwell authored
View
1  readme.md
@@ -6,6 +6,7 @@
- Touch owning models on delete.
- Added `Str::macro` method.
- Calling `update` on a Model instance will fill and update that model.
+- Allow specification of methods when pushing to Queue (`Queue::push('Class@method')`).
## Beta 4
View
16 src/Illuminate/Queue/Jobs/BeanstalkdJob.php
@@ -7,13 +7,6 @@
class BeanstalkdJob extends Job {
/**
- * The IoC container instance.
- *
- * @var \Illuminate\Container
- */
- protected $container;
-
- /**
* The Pheanstalk instance.
*
* @var Pheanstalk
@@ -51,14 +44,7 @@ public function __construct(Container $container,
*/
public function fire()
{
- $payload = json_decode($this->job->getData(), true);
-
- // Once we have the message payload, we can create the given class and fire
- // it off with the given data. The data is in the messages serialized so
- // we will unserialize it and pass into the jobs in its original form.
- $this->instance = $this->container->make($payload['job']);
-
- $this->instance->fire($this, $payload['data']);
+ $this->resolveAndFire(json_decode($this->job->getData(), true));
}
/**
View
16 src/Illuminate/Queue/Jobs/IronJob.php
@@ -6,13 +6,6 @@
class IronJob extends Job {
/**
- * The IoC container instance.
- *
- * @var \Illuminate\Container
- */
- protected $container;
-
- /**
* The IronMQ instance.
*
* @var IronMQ
@@ -60,14 +53,7 @@ public function __construct(Container $container,
*/
public function fire()
{
- // Once we have the message payload, we can create the given class and fire
- // it off with the given data. The data is in the messages serialized so
- // we will unserialize it and pass into the jobs in its original form.
- $payload = json_decode($this->job->body, true);
-
- $this->instance = $this->container->make($payload['job']);
-
- $this->instance->fire($this, $payload['data']);
+ $this->resolveAndFire(json_decode($this->job->body, true));
}
/**
View
46 src/Illuminate/Queue/Jobs/Job.php
@@ -10,6 +10,13 @@
protected $instance;
/**
+ * The IoC container instance.
+ *
+ * @var \Illuminate\Container\Container
+ */
+ protected $container;
+
+ /**
* Fire the job.
*
* @return void
@@ -39,6 +46,45 @@
abstract public function attempts();
/**
+ * Resolve and fire the job handler method.
+ *
+ * @param array $payload
+ * @return void
+ */
+ protected function resolveAndFire(array $payload)
+ {
+ list($class, $method) = $this->parseJob($payload['job']);
+
+ $this->instance = $this->resolve($class);
+
+ $this->instance->{$method}($this, $payload['data']);
+ }
+
+ /**
+ * Resolve the given job handler.
+ *
+ * @param string $class
+ * @return mixed
+ */
+ protected function resolve($class)
+ {
+ return $this->container->make($class);
+ }
+
+ /**
+ * 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 : array($segments[0], 'fire');
+ }
+
+ /**
* Determine if job should be auto-deleted.
*
* @return bool
View
16 src/Illuminate/Queue/Jobs/SqsJob.php
@@ -6,13 +6,6 @@
class SqsJob extends Job {
/**
- * The IoC container instance.
- *
- * @var \Illuminate\Container
- */
- protected $container;
-
- /**
* The Amazon SQS client instance.
*
* @var Aws\Sqs\SqsClient
@@ -60,14 +53,7 @@ public function __construct(Container $container,
*/
public function fire()
{
- $payload = json_decode($this->job['Body'], true);
-
- // Once we have the message payload, we can create the given class and fire
- // it off with the given data. The data is in the messages serialized so
- // we will unserialize it and pass into the jobs in its original form.
- $this->instance = $this->container->make($payload['job']);
-
- $this->instance->fire($this, $payload['data']);
+ $this->resolveAndFire(json_decode($this->job['Body'], true));
}
/**
View
11 src/Illuminate/Queue/Jobs/SyncJob.php
@@ -5,13 +5,6 @@
class SyncJob extends Job {
/**
- * The IoC container instance.
- *
- * @var \Illuminate\Container
- */
- protected $container;
-
- /**
* The class name of the job.
*
* @var string
@@ -47,9 +40,7 @@ public function __construct(Container $container, $job, $data = '')
*/
public function fire()
{
- $this->instance = $this->container->make($this->job);
-
- $this->instance->fire($this, $this->data);
+ $this->resolveAndFire(array('job' => $this->job, 'data' => $this->data));
}
/**
View
12 tests/Queue/QueueSyncJobTest.php
@@ -21,4 +21,16 @@ public function testFireResolvesAndFiresJobClass()
$job->fire();
}
+
+ public function testFireResolvesAndFiresJobClassWithCorrectMethod()
+ {
+ $container = m::mock('Illuminate\Container\Container');
+ $job = new Illuminate\Queue\Jobs\SyncJob($container, 'Foo@bar', 'data');
+ $handler = m::mock('StdClass');
+ $container->shouldReceive('make')->once()->with('Foo')->andReturn($handler);
+ $handler->shouldReceive('bar')->once()->with($job, 'data');
+
+ $job->fire();
+ }
+
}
Please sign in to comment.
Something went wrong with that request. Please try again.