mirror of
https://github.com/idanoo/php-resque.git
synced 2024-11-21 16:01:53 +00:00
Making the factory the default layer to construct jobs
This commit is contained in:
parent
df20186c37
commit
be19e12c31
@ -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);
|
||||
}
|
||||
|
@ -31,7 +31,7 @@ class Resque_Tests_EventTest extends Resque_Tests_TestCase
|
||||
$payload = array(
|
||||
'class' => 'Test_Job',
|
||||
'args' => array(
|
||||
'somevar',
|
||||
array('somevar'),
|
||||
),
|
||||
);
|
||||
$job = new Resque_Job('jobs', $payload);
|
||||
|
@ -362,13 +362,24 @@ class Resque_Tests_JobTest extends Resque_Tests_TestCase
|
||||
$this->assertEquals(Resque::size($queue), 2);
|
||||
}
|
||||
|
||||
public function testUseFactoryToGetJobInstance()
|
||||
public function testUseDefaultFactoryToGetJobInstance()
|
||||
{
|
||||
$payload = array(
|
||||
'class' => 'Some_Job_Class',
|
||||
'args' => null
|
||||
);
|
||||
$job = new Resque_Job('jobs', $payload);
|
||||
$instance = $job->getInstance();
|
||||
$this->assertInstanceOf('Some_Job_Class', $instance);
|
||||
}
|
||||
|
||||
public function testUseFactoryToGetJobInstance()
|
||||
{
|
||||
$payload = array(
|
||||
'class' => 'Some_Job_Class',
|
||||
'args' => array(array())
|
||||
);
|
||||
$job = new Resque_Job('jobs', $payload);
|
||||
$factory = new Some_Stub_Factory();
|
||||
$job->setJobFactory($factory);
|
||||
$instance = $job->getInstance();
|
||||
@ -379,7 +390,7 @@ class Resque_Tests_JobTest extends Resque_Tests_TestCase
|
||||
{
|
||||
$payload = array(
|
||||
'class' => 'Some_Job_Class',
|
||||
'args' => null
|
||||
'args' => array(array())
|
||||
);
|
||||
$job = new Resque_Job('jobs', $payload);
|
||||
$factory = $this->getMock('Resque_Job_FactoryInterface');
|
||||
@ -411,7 +422,7 @@ class Some_Stub_Factory implements Resque_Job_FactoryInterface
|
||||
* @param $queue
|
||||
* @return Resque_JobInterface
|
||||
*/
|
||||
public function create($className, array $args, $queue)
|
||||
public function create($className, $args, $queue)
|
||||
{
|
||||
return new Some_Job_Class();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user