Response::bare()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
/**
3
 * Facades for Yii 2
4
 *
5
 * Generated on Yii 2.0.12
6
 *
7
 * @see       https://github.com/sergeymakinen/yii2-facades
8
 * @copyright Copyright (c) 2016-2017 Sergey Makinen (https://makinen.ru)
9
 * @license   https://github.com/sergeymakinen/yii2-facades/blob/master/LICENSE The MIT License
10
 */
11
12
namespace sergeymakinen\facades;
13
14
/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
15
/**
16
 * Yii::$app->get('response') facade.
17
 *
18
 * Methods
19
 *
20
 * @method static \yii\base\Behavior attachBehavior(string $name, string|array|\yii\base\Behavior $behavior) Attaches a behavior to this component.
21
 * @see \yii\base\Component::attachBehavior
22
 *
23
 * @method static void attachBehaviors(array $behaviors) Attaches a list of behaviors to the component.
24
 * @see \yii\base\Component::attachBehaviors
25
 *
26
 * @method static array behaviors() Returns a list of behaviors that this component should behave as.
27
 * @see \yii\base\Component::behaviors
28
 *
29
 * @method static void clear() Clears the headers, cookies, content, status code of the response.
30
 * @see \yii\web\Response::clear
31
 *
32
 * @method static void clearOutputBuffers() Removes all existing output buffers.
33
 * @see \yii\base\Response::clearOutputBuffers
34
 *
35
 * @method static null|\yii\base\Behavior detachBehavior(string $name) Detaches a behavior from the component.
36
 * @see \yii\base\Component::detachBehavior
37
 *
38
 * @method static void detachBehaviors() Detaches all behaviors from the component.
39
 * @see \yii\base\Component::detachBehaviors
40
 *
41
 * @method static void ensureBehaviors() Makes sure that the behaviors declared in [[behaviors()]] are attached to this component.
42
 * @see \yii\base\Component::ensureBehaviors
43
 *
44
 * @method static null|\yii\base\Behavior getBehavior(string $name) Returns the named behavior object.
45
 * @see \yii\base\Component::getBehavior
46
 *
47
 * @method static \yii\base\Behavior[] getBehaviors() Returns all behaviors attached to this component.
48
 * @see \yii\base\Component::getBehaviors
49
 *
50
 * @method static \yii\web\CookieCollection getCookies() Returns the cookie collection.
51
 * @see \yii\web\Response::getCookies
52
 *
53
 * @method static \yii\web\HeaderCollection getHeaders() Returns the header collection.
54
 * @see \yii\web\Response::getHeaders
55
 *
56
 * @method static bool getIsClientError()
57
 * @see \yii\web\Response::getIsClientError
58
 *
59
 * @method static bool getIsEmpty()
60
 * @see \yii\web\Response::getIsEmpty
61
 *
62
 * @method static bool getIsForbidden()
63
 * @see \yii\web\Response::getIsForbidden
64
 *
65
 * @method static bool getIsInformational()
66
 * @see \yii\web\Response::getIsInformational
67
 *
68
 * @method static bool getIsInvalid()
69
 * @see \yii\web\Response::getIsInvalid
70
 *
71
 * @method static bool getIsNotFound()
72
 * @see \yii\web\Response::getIsNotFound
73
 *
74
 * @method static bool getIsOk()
75
 * @see \yii\web\Response::getIsOk
76
 *
77
 * @method static bool getIsRedirection()
78
 * @see \yii\web\Response::getIsRedirection
79
 *
80
 * @method static bool getIsServerError()
81
 * @see \yii\web\Response::getIsServerError
82
 *
83
 * @method static bool getIsSuccessful()
84
 * @see \yii\web\Response::getIsSuccessful
85
 *
86
 * @method static int getStatusCode()
87
 * @see \yii\web\Response::getStatusCode
88
 *
89
 * @method static bool hasEventHandlers(string $name) Returns a value indicating whether there is any handler attached to the named event.
90
 * @see \yii\base\Component::hasEventHandlers
91
 *
92
 * @method static bool off(string $name, callable $handler = null) Detaches an existing event handler from this component.
93
 * @see \yii\base\Component::off
94
 *
95
 * @method static void on(string $name, callable $handler, mixed $data = null, bool $append = true) Attaches an event handler to an event.
96
 * @see \yii\base\Component::on
97
 *
98
 * @method static \yii\web\Response redirect(string|array $url, int $statusCode = 302, bool $checkAjax = true) Redirects the browser to the specified URL.
99
 * @see \yii\web\Response::redirect
100
 *
101
 * @method static \yii\web\Response refresh(string $anchor = '') Refreshes the current page.
102
 * @see \yii\web\Response::refresh
103
 *
104
 * @method static void send() Sends the response to the client.
105
 * @see \yii\web\Response::send
106
 *
107
 * @method static \yii\web\Response sendContentAsFile(string $content, string $attachmentName, array $options = []) Sends the specified content as a file to the browser.
108
 * @see \yii\web\Response::sendContentAsFile
109
 *
110
 * @method static \yii\web\Response sendFile(string $filePath, string $attachmentName = null, array $options = []) Sends a file to the browser.
111
 * @see \yii\web\Response::sendFile
112
 *
113
 * @method static \yii\web\Response sendStreamAsFile(resource $handle, string $attachmentName, array $options = []) Sends the specified stream as a file to the browser.
114
 * @see \yii\web\Response::sendStreamAsFile
115
 *
116
 * @method static \yii\web\Response setDownloadHeaders(string $attachmentName, string $mimeType = null, bool $inline = false, int $contentLength = null) Sets a default set of HTTP headers for file downloading purpose.
117
 * @see \yii\web\Response::setDownloadHeaders
118
 *
119
 * @method static \yii\web\Response setStatusCode(int $value, string $text = null) Sets the response status code.
120
 * @see \yii\web\Response::setStatusCode
121
 *
122
 * @method static \yii\web\Response setStatusCodeByException(\Exception|\Error $e) Sets the response status code based on the exception.
123
 * @see \yii\web\Response::setStatusCodeByException
124
 *
125
 * @method static void trigger(string $name, \yii\base\Event $event = null) Triggers an event.
126
 * @see \yii\base\Component::trigger
127
 *
128
 * @method static \yii\web\Response xSendFile(string $filePath, string $attachmentName = null, array $options = []) Sends existing file to a browser as a download using x-sendfile.
129
 * @see \yii\web\Response::xSendFile
130
 *
131
 * Property accessors
132
 *
133
 * @method static string getAcceptMimeType() Returns the MIME type (e.g. `application/json`) from the request ACCEPT header chosen for this response.
134
 * @see \yii\web\Response::acceptMimeType
135
 *
136
 * @method static array getAcceptParams() Returns the parameters (e.g. `['q' => 1, 'version' => '1.0']`) associated with the [[acceptMimeType|chosen MIME type]].
137
 * @see \yii\web\Response::acceptParams
138
 *
139
 * @method static string getCharset() Returns the charset of the text response.
140
 * @see \yii\web\Response::charset
141
 *
142
 * @method static string getContent() Returns the response content.
143
 * @see \yii\web\Response::content
144
 *
145
 * @method static mixed getData() Returns the original response data.
146
 * @see \yii\web\Response::data
147
 *
148
 * @method static int getExitStatus() Returns the exit status.
149
 * @see \yii\base\Response::exitStatus
150
 *
151
 * @method static string getFormat() Returns the response format.
152
 * @see \yii\web\Response::format
153
 *
154
 * @method static array getFormatters() Returns the formatters for converting data into the response content of the specified [[format]].
155
 * @see \yii\web\Response::formatters
156
 *
157
 * @method static array getHttpStatuses() Returns list of HTTP status codes and the corresponding texts.
158
 * @see \yii\web\Response::httpStatuses
159
 *
160
 * @method static bool getIsSent() Returns whether the response has been sent.
161
 * @see \yii\web\Response::isSent
162
 *
163
 * @method static string getStatusText() Returns the HTTP status description that comes together with the status code.
164
 * @see \yii\web\Response::statusText
165
 *
166
 * @method static resource|array getStream() Returns the stream to be sent.
167
 * @see \yii\web\Response::stream
168
 *
169
 * @method static string getVersion() Returns the version of the HTTP protocol to use.
170
 * @see \yii\web\Response::version
171
 *
172
 * @method static void setAcceptMimeType(string $value) Sets the MIME type (e.g. `application/json`) from the request ACCEPT header chosen for this response.
173
 * @see \yii\web\Response::acceptMimeType
174
 *
175
 * @method static void setAcceptParams(array $value) Sets the parameters (e.g. `['q' => 1, 'version' => '1.0']`) associated with the [[acceptMimeType|chosen MIME type]].
176
 * @see \yii\web\Response::acceptParams
177
 *
178
 * @method static void setCharset(string $value) Sets the charset of the text response.
179
 * @see \yii\web\Response::charset
180
 *
181
 * @method static void setContent(string $value) Sets the response content.
182
 * @see \yii\web\Response::content
183
 *
184
 * @method static void setData(mixed $value) Sets the original response data.
185
 * @see \yii\web\Response::data
186
 *
187
 * @method static void setExitStatus(int $value) Sets the exit status.
188
 * @see \yii\base\Response::exitStatus
189
 *
190
 * @method static void setFormat(string $value) Sets the response format.
191
 * @see \yii\web\Response::format
192
 *
193
 * @method static void setFormatters(array $value) Sets the formatters for converting data into the response content of the specified [[format]].
194
 * @see \yii\web\Response::formatters
195
 *
196
 * @method static void setHttpStatuses(array $value) Sets list of HTTP status codes and the corresponding texts.
197
 * @see \yii\web\Response::httpStatuses
198
 *
199
 * @method static void setIsSent(bool $value) Sets whether the response has been sent.
200
 * @see \yii\web\Response::isSent
201
 *
202
 * @method static void setStatusText(string $value) Sets the HTTP status description that comes together with the status code.
203
 * @see \yii\web\Response::statusText
204
 *
205
 * @method static void setStream(resource|array $value) Sets the stream to be sent.
206
 * @see \yii\web\Response::stream
207
 *
208
 * @method static void setVersion(string $value) Sets the version of the HTTP protocol to use.
209
 * @see \yii\web\Response::version
210
 */
211
class Response extends Facade
212
{
213
    /**
214
     * @inheritDoc
215
     */
216 7
    public static function getFacadeComponentId()
217
    {
218 7
        return 'response';
219
    }
220
221
    /**
222
     * Sends an empty response.
223
     *
224
     * @param int $statusCode the status code
225
     * @param array $headers headers to be sent along with the response.
226
     *
227
     * @return \yii\web\Response the response object itself.
228
     */
229 1
    public static function bare($statusCode = 204, array $headers = [])
230
    {
231 1
        $response = static::sendFormat(\yii\web\Response::FORMAT_RAW, null, $headers);
232 1
        $response->setStatusCode($statusCode);
233 1
        return $response;
234
    }
235
236
    /**
237
     * Sends the specified data as HTML.
238
     *
239
     * @param mixed $data data to be sent.
240
     * @param array $headers headers to be sent along with the response.
241
     *
242
     * @return \yii\web\Response the response object itself.
243
     */
244 1
    public static function html($data, array $headers = [])
245
    {
246 1
        return static::sendFormat(\yii\web\Response::FORMAT_HTML, $data, $headers);
247
    }
248
249
    /**
250
     * Sends the specified data as JSON.
251
     *
252
     * @param mixed $data data to be sent.
253
     * @param array $headers headers to be sent along with the response.
254
     *
255
     * @return \yii\web\Response the response object itself.
256
     */
257 1
    public static function json($data, array $headers = [])
258
    {
259 1
        return static::sendFormat(\yii\web\Response::FORMAT_JSON, $data, $headers);
260
    }
261
262
    /**
263
     * Sends the specified data as JSONP.
264
     *
265
     * @param mixed $data data to be sent.
266
     * @param string $callback the JSONP callback name.
267
     * @param array $headers headers to be sent along with the response.
268
     *
269
     * @return \yii\web\Response the response object itself.
270
     */
271 1
    public static function jsonp($data, $callback = 'callback', array $headers = [])
272
    {
273 1
        return static::sendFormat(\yii\web\Response::FORMAT_JSONP, [
274 1
            'callback' => $callback,
275 1
            'data' => $data,
276 1
        ], $headers);
277
    }
278
279
    /**
280
     * Sends the specified data as is.
281
     *
282
     * @param mixed $data data to be sent.
283
     * @param array $headers headers to be sent along with the response.
284
     *
285
     * @return \yii\web\Response the response object itself.
286
     */
287 1
    public static function raw($data, array $headers = [])
288
    {
289 1
        return static::sendFormat(\yii\web\Response::FORMAT_RAW, $data, $headers);
290
    }
291
292
    /**
293
     * Sends the specified data as XML.
294
     *
295
     * @param mixed $data data to be sent.
296
     * @param array $headers headers to be sent along with the response.
297
     *
298
     * @return \yii\web\Response the response object itself.
299
     */
300 1
    public static function xml($data, array $headers = [])
301
    {
302 1
        return static::sendFormat(\yii\web\Response::FORMAT_XML, $data, $headers);
303
    }
304
305
    /**
306
     * Sends the specified data as the specified format.
307
     *
308
     * @param string $format the format to be set.
309
     * @param mixed $data data to be sent.
310
     * @param array $headers headers to be sent along with the response.
311
     *
312
     * @return \yii\web\Response the response object itself.
313
     */
314 6
    protected static function sendFormat($format, $data, array $headers)
315
    {
316
        /**
317
         * @var \yii\web\Response $response
318
         */
319 6
        $response = static::getFacadeComponent();
320 6
        $response->format = $format;
321 6
        $response->data = $data;
322 6
        foreach ($headers as $name => $value) {
323 6
            if (strtolower($name) === 'location') {
324
                /** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
325 6
                $value = \yii\helpers\Url::to($value);
326 6
                if (strpos($value, '/') === 0 && strpos($value, '//') !== 0) {
327 6
                    $value = Request::getHostInfo() . $value;
328 6
                }
329 6
            }
330 6
            $response->getHeaders()->set($name, $value);
331 6
        }
332 6
        return $response;
333
    }
334
}
335