1
|
|
|
<?php |
2
|
|
|
namespace Agavi\Response; |
3
|
|
|
|
4
|
|
|
// +---------------------------------------------------------------------------+ |
5
|
|
|
// | This file is part of the Agavi package. | |
6
|
|
|
// | Copyright (c) 2005-2011 the Agavi Project. | |
7
|
|
|
// | | |
8
|
|
|
// | For the full copyright and license information, please view the LICENSE | |
9
|
|
|
// | file that was distributed with this source code. You can also view the | |
10
|
|
|
// | LICENSE file online at http://www.agavi.org/LICENSE.txt | |
11
|
|
|
// | vi: set noexpandtab: | |
12
|
|
|
// | Local Variables: | |
13
|
|
|
// | indent-tabs-mode: t | |
14
|
|
|
// | End: | |
15
|
|
|
// +---------------------------------------------------------------------------+ |
16
|
|
|
use Agavi\Dispatcher\OutputType; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* ConsoleResponse handles command line responses. |
20
|
|
|
* |
21
|
|
|
* @package agavi |
22
|
|
|
* @subpackage response |
23
|
|
|
* |
24
|
|
|
* @author David Zülke <[email protected]> |
25
|
|
|
* @copyright Authors |
26
|
|
|
* @copyright The Agavi Project |
27
|
|
|
* |
28
|
|
|
* @since 1.0.0 |
29
|
|
|
* |
30
|
|
|
* @version $Id$ |
31
|
|
|
*/ |
32
|
|
|
class ConsoleResponse extends Response |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var string The content to send back with this response. |
36
|
|
|
*/ |
37
|
|
|
protected $content = ''; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var int The shell exit code. |
41
|
|
|
*/ |
42
|
|
|
protected $exitCode = 0; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Import response metadata (nothing in this case) from another response. |
46
|
|
|
* |
47
|
|
|
* @param Response $otherResponse The other response to import information from. |
48
|
|
|
* |
49
|
|
|
* @author David Zülke <[email protected]> |
50
|
|
|
* @since 1.0.0 |
51
|
|
|
*/ |
52
|
|
|
public function merge(Response $otherResponse) |
53
|
|
|
{ |
54
|
|
|
parent::merge($otherResponse); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Redirect externally. Not implemented here. |
59
|
|
|
* |
60
|
|
|
* @param mixed $to Where to redirect. |
61
|
|
|
* |
62
|
|
|
* @throws \BadMethodCallException |
63
|
|
|
* |
64
|
|
|
* @author David Zülke <[email protected]> |
65
|
|
|
* @since 1.0.0 |
66
|
|
|
*/ |
67
|
|
|
public function setRedirect($to) |
68
|
|
|
{ |
69
|
|
|
throw new \BadMethodCallException('Redirects are not implemented for Console.'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Get info about the set redirect. Not implemented here. |
74
|
|
|
* |
75
|
|
|
* @return array An assoc array of redirect info, or null if none set. |
76
|
|
|
* |
77
|
|
|
* @throws \BadMethodCallException |
78
|
|
|
* |
79
|
|
|
* @author David Zülke <[email protected]> |
80
|
|
|
* @since 1.0.0 |
81
|
|
|
*/ |
82
|
|
|
public function getRedirect() |
83
|
|
|
{ |
84
|
|
|
throw new \BadMethodCallException('Redirects are not implemented for Console.'); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Check if a redirect is set. Not implemented here. |
89
|
|
|
* |
90
|
|
|
* @return bool true, if a redirect is set, otherwise false |
91
|
|
|
* |
92
|
|
|
* @throws \BadMethodCallException |
93
|
|
|
* |
94
|
|
|
* @author David Zülke <[email protected]> |
95
|
|
|
* @since 1.0.0 |
96
|
|
|
*/ |
97
|
|
|
public function hasRedirect() |
98
|
|
|
{ |
99
|
|
|
throw new \BadMethodCallException('Redirects are not implemented for Console.'); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Clear any set redirect information. Not implemented here. |
104
|
|
|
* |
105
|
|
|
* @throws \BadMethodCallException |
106
|
|
|
* |
107
|
|
|
* @author David Zülke <[email protected]> |
108
|
|
|
* @since 1.0.0 |
109
|
|
|
*/ |
110
|
|
|
public function clearRedirect() |
111
|
|
|
{ |
112
|
|
|
throw new \BadMethodCallException('Redirects are not implemented for Console.'); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Set the shell exit code of this response. |
117
|
|
|
* |
118
|
|
|
* @param int $exitCode The exit code. |
119
|
|
|
* |
120
|
|
|
* @author David Zülke <[email protected]> |
121
|
|
|
* @since 1.0.0 |
122
|
|
|
*/ |
123
|
|
|
public function setExitCode($exitCode) |
124
|
|
|
{ |
125
|
|
|
$this->exitCode = (int)$exitCode; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Get the shell exit code of this response. |
130
|
|
|
* |
131
|
|
|
* @return int The exit code. |
132
|
|
|
* |
133
|
|
|
* @author David Zülke <[email protected]> |
134
|
|
|
* @since 1.0.0 |
135
|
|
|
*/ |
136
|
|
|
public function getExitCode() |
137
|
|
|
{ |
138
|
|
|
return $this->exitCode; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Determine whether the content in the response may be modified by appending |
143
|
|
|
* or prepending data using string operations. Typically false for streams, |
144
|
|
|
* and for responses like XMLRPC where the content is an array. |
145
|
|
|
* |
146
|
|
|
* @return bool If the content can be treated as / changed like a string. |
147
|
|
|
* |
148
|
|
|
* @author David Zülke <[email protected]> |
149
|
|
|
* @since 1.0.0 |
150
|
|
|
*/ |
151
|
|
|
public function isContentMutable() |
152
|
|
|
{ |
153
|
|
|
return !is_resource($this->content); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Send all response data to the client. |
158
|
|
|
* |
159
|
|
|
* @author David Zülke <[email protected]> |
160
|
|
|
* @since 1.0.0 |
161
|
|
|
*/ |
162
|
|
|
public function send(OutputType $outputType = null) |
163
|
|
|
{ |
164
|
|
|
$this->sendContent(); |
165
|
|
|
|
166
|
|
|
register_shutdown_function(array($this, 'sendExit')); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Clear all response data. |
171
|
|
|
* |
172
|
|
|
* @author David Zülke <[email protected]> |
173
|
|
|
* @since 1.0.0 |
174
|
|
|
*/ |
175
|
|
|
public function clear() |
176
|
|
|
{ |
177
|
|
|
$this->clearContent(); |
178
|
|
|
$this->setExitCode(0); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Send the content for this response |
183
|
|
|
* |
184
|
|
|
* @author David Zülke <[email protected]> |
185
|
|
|
* @since 1.0.0 |
186
|
|
|
*/ |
187
|
|
|
protected function sendContent() |
188
|
|
|
{ |
189
|
|
|
$isContentMutable = $this->isContentMutable(); |
190
|
|
|
|
191
|
|
|
parent::sendContent(); |
192
|
|
|
|
193
|
|
|
if ($isContentMutable && $this->getParameter('append_eol', true)) { |
194
|
|
|
echo PHP_EOL; |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Call exit() and submit the exit code. |
200
|
|
|
* This is called by PHP during script shutdown. |
201
|
|
|
* It gets registered as a shutdown function in ConsoleResponse::send(). |
202
|
|
|
* |
203
|
|
|
* @author David Zülke <[email protected]> |
204
|
|
|
* @since 1.0.0 |
205
|
|
|
*/ |
206
|
|
|
public function sendExit() |
207
|
|
|
{ |
208
|
|
|
exit($this->exitCode); |
|
|
|
|
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|
An exit expression should only be used in rare cases. For example, if you write a short command line script.
In most cases however, using an
exit
expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.