1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Zemit Framework. |
4
|
|
|
* |
5
|
|
|
* (c) Zemit Team <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE.txt |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Zemit\Modules\Cli; |
12
|
|
|
|
13
|
|
|
use Phalcon\Cli\Dispatcher; |
14
|
|
|
use Phalcon\Version; |
15
|
|
|
use Zemit\Utils; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class Task |
19
|
|
|
* |
20
|
|
|
* @author Julien Turbide <[email protected]> |
21
|
|
|
* @copyright Zemit Team <[email protected]> |
22
|
|
|
* |
23
|
|
|
* @since 1.0 |
24
|
|
|
* @version 1.0 |
25
|
|
|
* |
26
|
|
|
* @package Zemit\Modules\Cli |
27
|
|
|
*/ |
28
|
|
|
class Task extends \Phalcon\Cli\Task |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
public $consoleDoc = <<<DOC |
34
|
|
|
Usage: |
35
|
|
|
php zemit cli <task> <action> [<params> ...] |
36
|
|
|
|
37
|
|
|
Options: |
38
|
|
|
task: build,cache,cron,errors,help,scaffold |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
DOC; |
42
|
|
|
|
43
|
|
|
public function helpAction() |
44
|
|
|
{ |
45
|
|
|
echo $this->consoleDoc; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function mainAction() |
49
|
|
|
{ |
50
|
|
|
$this->helpAction(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Sending rest response as an http response |
55
|
|
|
* |
56
|
|
|
* @param array|null $response |
57
|
|
|
* @param null $status |
|
|
|
|
58
|
|
|
* @param null $code |
|
|
|
|
59
|
|
|
* @param int $jsonOptions |
60
|
|
|
* @param int $depth |
61
|
|
|
* |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
|
|
public function normalizeResponse($response = null, $code = null, $status = null, $jsonOptions = 0, $depth = 512) |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
$debug = $this->config->app->debug ?: $this->dispatcher->getParam('debug') ?: false; |
|
|
|
|
67
|
|
|
|
68
|
|
|
// keep forced status code or set our own |
69
|
|
|
$responseStatusCode = $this->response->getStatusCode(); |
70
|
|
|
$reasonPhrase = $this->response->getReasonPhrase(); |
71
|
|
|
$status ??= $reasonPhrase ? : 'OK'; |
72
|
|
|
$code ??= (int)$responseStatusCode ? : 200; |
73
|
|
|
$view = $this->view->getParamsToView(); |
74
|
|
|
$hash = hash('sha512', json_encode($view)); |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Debug section |
78
|
|
|
* - Versions |
79
|
|
|
* - Request |
80
|
|
|
* - Identity |
81
|
|
|
* - Profiler |
82
|
|
|
* - Dispatcher |
83
|
|
|
* - Router |
84
|
|
|
*/ |
85
|
|
|
$identity = $debug ? $this->identity->getIdentity() : null; |
|
|
|
|
86
|
|
|
$profiler = $debug && $this->profiler ? $this->profiler->toArray() : null; |
|
|
|
|
87
|
|
|
$dispatcher = $debug ? $this->dispatcher->toArray() : null; |
|
|
|
|
88
|
|
|
$router = $debug ? $this->router->toArray() : null; |
|
|
|
|
89
|
|
|
|
90
|
|
|
$api = $debug ? [ |
91
|
|
|
'php' => phpversion(), |
92
|
|
|
'phalcon' => Version::get(), |
93
|
|
|
'zemit' => $this->config->core->version, |
94
|
|
|
'core' => $this->config->core->name, |
95
|
|
|
'app' => $this->config->app->version, |
96
|
|
|
'name' => $this->config->app->name, |
97
|
|
|
] : []; |
98
|
|
|
$api['version'] = '0.1'; |
99
|
|
|
|
100
|
|
|
$this->response->setStatusCode($code, $code . ' ' . $status); |
101
|
|
|
|
102
|
|
|
return array_merge([ |
103
|
|
|
'api' => $api, |
104
|
|
|
'timestamp' => date('c'), |
105
|
|
|
'hash' => $hash, |
106
|
|
|
'status' => $status, |
107
|
|
|
'code' => $code, |
108
|
|
|
'response' => $response, |
109
|
|
|
'view' => $view, |
110
|
|
|
], $debug ? [ |
111
|
|
|
'identity' => $identity, |
112
|
|
|
'profiler' => $profiler, |
113
|
|
|
'dispatcher' => $dispatcher, |
114
|
|
|
'router' => $router, |
115
|
|
|
'memory' => Utils::getMemoryUsage(), |
116
|
|
|
] : []); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Handle rest response automagically |
121
|
|
|
* |
122
|
|
|
* @param Dispatcher $dispatcher |
123
|
|
|
*/ |
124
|
|
|
public function afterExecuteRoute(Dispatcher $dispatcher) |
125
|
|
|
{ |
126
|
|
|
// Merge response into view variables |
127
|
|
|
$response = $dispatcher->getReturnedValue(); |
128
|
|
|
|
129
|
|
|
// Quiet output |
130
|
|
|
$quiet = $this->dispatcher->getParam('quiet'); |
131
|
|
|
if ($quiet) { |
132
|
|
|
exit(!$response? 1 : 0); |
|
|
|
|
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
// Normalize response |
136
|
|
|
if (is_array($response)) { |
137
|
|
|
$this->view->setVars($response, true); |
|
|
|
|
138
|
|
|
} |
139
|
|
|
$normalizedResponse = $this->normalizeResponse(is_array($response) ? null : $response); |
140
|
|
|
$dispatcher->setReturnedValue($normalizedResponse); |
141
|
|
|
|
142
|
|
|
// Set response |
143
|
|
|
$verbose = $this->dispatcher->getParam('verbose'); |
144
|
|
|
$ret = $verbose? $normalizedResponse : $response; |
145
|
|
|
|
146
|
|
|
// Format response |
147
|
|
|
$format = $this->dispatcher->getParam('format'); |
148
|
|
|
$format ??= 'dump'; |
149
|
|
|
|
150
|
|
|
switch (strtolower($format)) { |
151
|
|
|
case 'dump': |
|
|
|
|
152
|
|
|
dd($ret); |
|
|
|
|
153
|
|
|
case 'json': |
154
|
|
|
echo json_encode($ret); |
155
|
|
|
break; |
156
|
|
|
case 'xml': |
157
|
|
|
$xml = new \SimpleXMLElement(''); |
158
|
|
|
array_walk_recursive($ret, array ($xml,'addChild')); |
159
|
|
|
echo $xml->asXML(); |
|
|
|
|
160
|
|
|
break; |
161
|
|
|
case 'serialize': |
162
|
|
|
echo serialize($ret); |
163
|
|
|
break; |
164
|
|
|
case 'raw': |
165
|
|
|
if (is_array($ret) || is_object($ret)) { |
166
|
|
|
print_r($ret); |
167
|
|
|
} |
168
|
|
|
else if (is_bool($ret)) { |
169
|
|
|
echo $ret? 'true' : 'false'; |
170
|
|
|
} |
171
|
|
|
else { |
172
|
|
|
echo strval($ret); |
173
|
|
|
} |
174
|
|
|
break; |
175
|
|
|
default: |
176
|
|
|
throw new \Exception('Unknown output format `'.$format.'` expected one of the string value: `json` `xml` `serialize` `dump` `raw`'); |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|