forked from CoderKungfu/php-queue
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathREST.php
More file actions
237 lines (212 loc) · 6.72 KB
/
Copy pathREST.php
File metadata and controls
237 lines (212 loc) · 6.72 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php
/**
* PHPQueue\REST class
*
* Example:
* curl -XPOST http://<server>/<queueName> -d "var1=foo&var2=bar"
* curl -XPOST http://<server>/<queueName> -H "Content-Type: application/json" -d '{"var1":"foo","var2":"bar"}'
* curl -XPUT http://<server>/<queueName>
*/
namespace PHPQueue;
use PHPQueue\Exception\Exception;
class REST
{
public static $json_payload_key = null;
public static $rest_server;
public static $response_content = array(
'application/json' => 'json_encode'
);
public $auth_class = null;
public function __construct($options=array())
{
if ( !empty($options['auth']) ) {
$auth_class = $options['auth'];
if (is_string($auth_class)) {
if (!(strpos($auth_class, "\\") === 0)) {
$auth_class = '\\' . $auth_class;
}
$auth_class = new $auth_class($options);
}
$this->auth_class = $auth_class;
}
}
/**
* Starts a Respect/REST server with default routes:
*/
public static function defaultRoutes($options=array())
{
$router = !empty($options['router']) ? $options['router'] : '\PHPQueue\REST';
if (is_string($router)) {
$router = new $router($options);
}
$response_format = !empty($options['format'])
? array_merge(self::$response_content, $options['format'])
: self::$response_content;
self::startServer()
->always('Accept', $response_format)
->any('/*/**', function($queue=null, $actions=array()) use ($router, $options) {
return $router->route($queue, $actions, $options);
});
}
/**
* Starts the Respect/REST server
* @return \Respect\Rest\Router
*/
public static function startServer()
{
if (empty(self::$rest_server)) {
self::$rest_server = new \Respect\Rest\Router;
}
return self::$rest_server;
}
/**
* Specify how a routed URL should be handled.
* @param string $queue
* @param array $actions
* @param array $options array('auth'=>Object)
* @return stdClass
*/
public function route($queue=null, $actions=array(), $options=array())
{
try {
$this->isAuth();
} catch (\Exception $ex) {
return $this->failed(401, $ex->getMessage());
}
$method = !empty($_GET['REQUEST_METHOD']) ? $_GET['REQUEST_METHOD'] : $_SERVER['REQUEST_METHOD'];
switch ($method) {
case 'POST':
return $this->post($queue);
break;
case 'PUT':
return $this->work($queue);
break;
default:
return $this->failed(404, "Method not supported.");
break;
}
}
protected function isAuth()
{
if ( !is_null($this->auth_class) ) {
if ( !is_a($this->auth_class, '\PHPQueue\Interfaces\Auth') ) {
throw new Exception("Invalid Auth Object.");
}
if (!$this->auth_class->isAuth()) {
throw new Exception("Not Authorized");
}
}
return true;
}
/**
* Handles a POST method
* @param string $queueName
* @return stdClass
*/
protected function post($queueName=null)
{
$payload = $this->getPayload();
try {
$queue = Base::getQueue($queueName);
Base::addJob($queue, $payload);
return $this->successful();
} catch (\Exception $ex) {
return $this->failed($ex->getCode(), $ex->getMessage());
}
}
/**
* @return array
*/
protected function getPayload()
{
$payload = array();
switch ($_SERVER['CONTENT_TYPE']) {
case 'application/json':
$content = file_get_contents('php://input');
$payload = json_decode($content, true);
if ( !empty(self::$json_payload_key) && isset($payload['data']) ) {
$payload = $payload['data'];
}
break;
default:
if (!empty($_POST)) {
$payload = $_POST;
}
break;
}
return $payload;
}
/**
* Trigger a worker for a queue. Next item in the queue will be retrieved and worked with the appropriate worker.
* @param string $queueName
* @return stdClass
*/
protected function work($queueName=null)
{
$queue = Base::getQueue($queueName);
try {
$newJob = Base::getJob($queue);
} catch (\Exception $ex) {
return $this->failed(405, $ex->getMessage());
}
if (empty($newJob)) {
return $this->failed(404, "No Job in queue.");
}
try {
if (empty($newJob->worker)) {
throw new Exception("No worker declared.");
}
if (is_string($newJob->worker)) {
$result_data = $this->processWorker($newJob->worker, $newJob);
} elseif (is_array($newJob->worker)) {
foreach ($newJob->worker as $worker_name) {
$result_data = $this->processWorker($worker_name, $newJob);
$newJob->data = $result_data;
}
}
Base::updateJob($queue, $newJob->job_id, $result_data);
return $this->successful();
} catch (\Exception $ex) {
$queue->releaseJob($newJob->job_id);
return $this->failed($ex->getCode(), $ex->getMessage());
}
}
protected function processWorker($worker_name, $new_job)
{
$newWorker = Base::getWorker($worker_name);
Base::workJob($newWorker, $new_job);
return $newWorker->result_data;
}
/**
* Convenience method for Successful call.
* @return stdClass
*/
protected function successful()
{
return self::respond(null, 200, "OK");
}
/**
* Convenience method for Failed call.
* @return stdClass
*/
protected function failed($code=501, $reason="")
{
return self::respond(null, $code, $reason);
}
/**
* Convenience method for a Data call.
* @return stdClass
*/
protected function showData($data=null)
{
return self::respond($data, 200, "OK");
}
/**
* Main method for generating response stdClass.
* @return stdClass
*/
protected function respond($data=null, $code=200, $message="")
{
return Helpers::output($data, $code, $message);
}
}