This repository has been archived by the owner on Mar 4, 2019. It is now read-only.
/
CakeDjjobBehavior.php
139 lines (124 loc) · 3.51 KB
/
CakeDjjobBehavior.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
App::uses('ConnectionManager', 'Model');
App::uses('CakeJob', 'CakeDjjob.Job');
App::uses('DJJob', 'Djjob.Vendor');
/**
* CakeDjjob Model Behavior
*
* Wrapper around DJJob library
*
* @copyright Copyright 2011, Jose Diaz-Gonzalez. (http://josediazgonzalez.com)
* @link http://github.com/josegonzalez/cake_djjob
* @package cake_djjob
* @subpackage cake_djjob.models.behaviors
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
**/
class CakeDjjobBehavior extends ModelBehavior {
/**
* Contains configuration settings for use with individual model objects.
* Individual model settings should be stored as an associative array,
* keyed off of the model name.
*
* @var array
* @access public
* @see Model::$alias
*/
public $settings = array(
'connection'=> 'default',
'type' => 'mysql',
);
/**
* Initiate CakeDjjob Behavior
*
* @param object $model
* @param array $config
* @return void
* @access public
*/
public function setup(Model $model, $config = array()) {
$this->settings = array_merge($this->settings, $config);
$connection = ConnectionManager::getDataSource($this->settings['connection']);
if ($this->settings['type'] == 'mysql') {
DJJob::setConnection($connection->getConnection());
} else {
DJJob::configure(
implode(';', array(
"{$this->settings['type']}:host={$connection->config['host']}",
"dbname={$connection->config['database']}",
"port={$connection->config['port']}",
"user={$connection->config['login']}",
"password={$connection->config['password']}"
))
);
}
}
/**
* Returns a job
*
* Auto imports and passes through the constructor parameters to newly created job
* Note: (PHP 5 >= 5.1.3) - requires ReflectionClass if passing arguments
*
* @param object $model Model instance calling this method
* @param string $jobName Name of job being loaded
* @param mixed $argument Some argument to pass to the job
* @param mixed ... etc.
* @return mixed Job instance if available, null otherwise
*/
public function load(&$Model) {
$args = func_get_args();
array_shift($args);
if (empty($args) || !is_string($args[0])) {
return null;
}
$jobName = array_shift($args);
list($plugin, $className) = pluginSplit($jobName);
if ($plugin) {
$plugin = "{$plugin}.";
}
if (!class_exists($className)) {
App::uses($className, "{$plugin}Job");
}
if (empty($args)) {
return new $className();
}
if (!class_exists('ReflectionClass')) {
return null;
}
$ref = new ReflectionClass($className);
return $ref->newInstanceArgs($args);
}
/**
* Enqueues Jobs using DJJob
*
* Note that all Jobs enqueued using this system must extend the base CakeJob
* class which is included in this plugin
*
* @param Job $job
* @param string $queue
* @param string $run_at
* @return boolean True if enqueue is successful, false on failure
*/
public function enqueue(&$Model, $job, $queue = "default", $run_at = null) {
return DJJob::enqueue($job, $queue, $run_at);
}
/**
* Bulk Enqueues Jobs using DJJob
*
* @param array $jobs
* @param string $queue
* @param string $run_at
* @return boolean True if bulk enqueue is successful, false on failure
*/
public function bulkEnqueue(&$Model, $jobs, $queue = "default", $run_at = null) {
return DJJob::bulkEnqueue($jobs, $queue, $run_at);
}
/**
* Returns an array containing the status of a given queue
*
* @param string $queue
* @return array
**/
public function status(&$Model, $queue = "default") {
return DJJob::status($queue);
}
}