mirror of
https://github.com/idanoo/php-resque
synced 2025-07-01 13:42:22 +00:00
Making the factory the default layer to construct jobs
This commit is contained in:
parent
df20186c37
commit
be19e12c31
5 changed files with 64 additions and 29 deletions
|
@ -6,7 +6,7 @@
|
|||
* @author Chris Boulton <chris@bigcommerce.com>
|
||||
* @license http://www.opensource.org/licenses/mit-license.php
|
||||
*/
|
||||
class Resque_Job implements Resque_JobInterface
|
||||
class Resque_Job
|
||||
{
|
||||
/**
|
||||
* @var string The name of the queue that this job belongs to.
|
||||
|
@ -24,7 +24,7 @@ class Resque_Job implements Resque_JobInterface
|
|||
public $payload;
|
||||
|
||||
/**
|
||||
* @var Resque_JobInterface Instance of the class performing work for this job.
|
||||
* @var object|Resque_JobInterface Instance of the class performing work for this job.
|
||||
*/
|
||||
private $instance;
|
||||
|
||||
|
@ -169,28 +169,9 @@ class Resque_Job implements Resque_JobInterface
|
|||
return $this->instance;
|
||||
}
|
||||
|
||||
if(!class_exists($this->payload['class'])) {
|
||||
throw new Resque_Exception(
|
||||
'Could not find job class ' . $this->payload['class'] . '.'
|
||||
);
|
||||
}
|
||||
|
||||
if(!method_exists($this->payload['class'], 'perform')) {
|
||||
throw new Resque_Exception(
|
||||
'Job class ' . $this->payload['class'] . ' does not contain a perform method.'
|
||||
);
|
||||
}
|
||||
|
||||
if ($this->jobFactory !== null) {
|
||||
$this->instance = $this->jobFactory->create($this->payload['class'], $this->getArguments(), $this->queue);
|
||||
return $this->instance;
|
||||
}
|
||||
$this->instance = new $this->payload['class'];
|
||||
$this->instance->job = $this;
|
||||
$this->instance->args = $this->getArguments();
|
||||
$this->instance->queue = $this->queue;
|
||||
|
||||
return $this->instance;
|
||||
$this->instance = $this->getJobFactory()->create($this->payload['class'], $this->getArguments(), $this->queue);
|
||||
$this->instance->job = $this;
|
||||
return $this->instance;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -294,4 +275,15 @@ class Resque_Job implements Resque_JobInterface
|
|||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Resque_Job_FactoryInterface
|
||||
*/
|
||||
public function getJobFactory()
|
||||
{
|
||||
if ($this->jobFactory === null) {
|
||||
$this->jobFactory = new Resque_Job_Factory();
|
||||
}
|
||||
return $this->jobFactory;
|
||||
}
|
||||
}
|
||||
|
|
32
lib/Resque/Job/Factory.php
Normal file
32
lib/Resque/Job/Factory.php
Normal file
|
@ -0,0 +1,32 @@
|
|||
<?php
|
||||
|
||||
class Resque_Job_Factory implements Resque_Job_FactoryInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @param $className
|
||||
* @param array $args
|
||||
* @param $queue
|
||||
* @return Resque_JobInterface
|
||||
* @throws \Resque_Exception
|
||||
*/
|
||||
public function create($className, $args, $queue)
|
||||
{
|
||||
if (!class_exists($className)) {
|
||||
throw new Resque_Exception(
|
||||
'Could not find job class ' . $className . '.'
|
||||
);
|
||||
}
|
||||
|
||||
if (!method_exists($className, 'perform')) {
|
||||
throw new Resque_Exception(
|
||||
'Job class ' . $className . ' does not contain a perform method.'
|
||||
);
|
||||
}
|
||||
|
||||
$instance = new $className;
|
||||
$instance->args = $args;
|
||||
$instance->queue = $queue;
|
||||
return $instance;
|
||||
}
|
||||
}
|
|
@ -8,5 +8,5 @@ interface Resque_Job_FactoryInterface
|
|||
* @param $queue
|
||||
* @return Resque_JobInterface
|
||||
*/
|
||||
public function create($className, array $args, $queue);
|
||||
public function create($className, $args, $queue);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue