Passed
Push — master ( 477212...0ba014 )
by Sergey
02:48
created

Response::raw()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
/**
3
 * Facades for Yii 2
4
 *
5
 * @see       https://github.com/sergeymakinen/yii2-facades
6
 * @copyright Copyright (c) 2016 Sergey Makinen (https://makinen.ru)
7
 * @license   https://github.com/sergeymakinen/yii2-facades/blob/master/LICENSE The MIT License
8
 */
9
10
namespace sergeymakinen\facades;
11
12
/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
13
/**
14
 * Response facade.
15
 *
16
 * Facades Yii::$app->get('response') component.
17
 *
18
 * @see \yii\console\Response
19
 * @see \yii\web\Response
20
 * @method static \yii\base\Behavior attachBehavior(string $name, string|array|\yii\base\Behavior $behavior) Attaches a behavior to this component.
21
 * @method static attachBehaviors(array $behaviors) Attaches a list of behaviors to the component.
22
 * @method static array behaviors() Returns a list of behaviors that this component should behave as.
23
 * @method static clear() Clears the headers, cookies, content, status code of the response.
24
 * @method static clearOutputBuffers() Removes all existing output buffers.
25
 * @method static null|\yii\base\Behavior detachBehavior(string $name) Detaches a behavior from the component.
26
 * @method static detachBehaviors() Detaches all behaviors from the component.
27
 * @method static ensureBehaviors() Makes sure that the behaviors declared in [[behaviors()]] are attached to this component.
28
 * @method static string getAcceptMimeType() Returns the MIME type (e.g. `application/json`) from the request ACCEPT header chosen for this response.
29
 * @method static array getAcceptParams() Returns the parameters (e.g. `['q' => 1, 'version' => '1.0']`) associated with the [[acceptMimeType|chosen MIME type]].
30
 * @method static null|\yii\base\Behavior getBehavior(string $name) Returns the named behavior object.
31
 * @method static \yii\base\Behavior[] getBehaviors() Returns all behaviors attached to this component.
32
 * @method static string getCharset() Returns the charset of the text response.
33
 * @method static string getContent() Returns the response content.
34
 * @method static \yii\web\CookieCollection getCookies() Returns the cookie collection.
35
 * @method static mixed getData() Returns the original response data.
36
 * @method static int getExitStatus() Returns the exit status.
37
 * @method static string getFormat() Returns the response format.
38
 * @method static array getFormatters() Returns the formatters for converting data into the response content of the specified [[format]].
39
 * @method static \yii\web\HeaderCollection getHeaders() Returns the header collection.
40
 * @method static array getHttpStatuses() Returns list of HTTP status codes and the corresponding texts.
41
 * @method static bool getIsClientError()
42
 * @method static bool getIsEmpty()
43
 * @method static bool getIsForbidden()
44
 * @method static bool getIsInformational()
45
 * @method static bool getIsInvalid()
46
 * @method static bool getIsNotFound()
47
 * @method static bool getIsOk()
48
 * @method static bool getIsRedirection()
49
 * @method static bool getIsSent() Returns whether the response has been sent.
50
 * @method static bool getIsServerError()
51
 * @method static bool getIsSuccessful()
52
 * @method static int getStatusCode()
53
 * @method static string getStatusText() Returns the HTTP status description that comes together with the status code.
54
 * @method static resource|array getStream() Returns the stream to be sent.
55
 * @method static string getVersion() Returns the version of the HTTP protocol to use.
56
 * @method static bool hasEventHandlers(string $name) Returns a value indicating whether there is any handler attached to the named event.
57
 * @method static bool off(string $name, callable $handler = null) Detaches an existing event handler from this component.
58
 * @method static on(string $name, callable $handler, mixed $data = null, bool $append = true) Attaches an event handler to an event.
59
 * @method static \yii\web\Response redirect(string|array $url, int $statusCode = 302, bool $checkAjax = true) Redirects the browser to the specified URL.
60
 * @method static \yii\web\Response refresh(string $anchor = '') Refreshes the current page.
61
 * @method static send() Sends the response to the client.
62
 * @method static \yii\web\Response sendContentAsFile(string $content, string $attachmentName, array $options = []) Sends the specified content as a file to the browser.
63
 * @method static \yii\web\Response sendFile(string $filePath, string $attachmentName = null, array $options = []) Sends a file to the browser.
64
 * @method static \yii\web\Response sendStreamAsFile(resource $handle, string $attachmentName, array $options = []) Sends the specified stream as a file to the browser.
65
 * @method static setAcceptMimeType(string $value) Sets the MIME type (e.g. `application/json`) from the request ACCEPT header chosen for this response.
66
 * @method static setAcceptParams(array $value) Sets the parameters (e.g. `['q' => 1, 'version' => '1.0']`) associated with the [[acceptMimeType|chosen MIME type]].
67
 * @method static setCharset(string $value) Sets the charset of the text response.
68
 * @method static setContent(string $value) Sets the response content.
69
 * @method static setData(mixed $value) Sets the original response data.
70
 * @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.
71
 * @method static setExitStatus(int $value) Sets the exit status.
72
 * @method static setFormat(string $value) Sets the response format.
73
 * @method static setFormatters(array $value) Sets the formatters for converting data into the response content of the specified [[format]].
74
 * @method static setHttpStatuses(array $value) Sets list of HTTP status codes and the corresponding texts.
75
 * @method static setIsSent(bool $value) Sets whether the response has been sent.
76
 * @method static setStatusCode(int $value, string $text = null) Sets the response status code.
77
 * @method static setStatusText(string $value) Sets the HTTP status description that comes together with the status code.
78
 * @method static setStream(resource|array $value) Sets the stream to be sent.
79
 * @method static setVersion(string $value) Sets the version of the HTTP protocol to use.
80
 * @method static trigger(string $name, \yii\base\Event $event = null) Triggers an event.
81
 * @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.
82
 */
83
class Response extends Facade
84
{
85
    /**
86
     * @inheritdoc
87
     */
88
    public static function getFacadeComponentId()
89
    {
90
        return 'response';
91
    }
92
93
    /**
94
     * Sends an empty response.
95
     *
96
     * @param int $statusCode the status code
97
     * @param array $headers headers to be sent along with the response.
98
     *
99
     * @return \yii\web\Response the response object itself.
100
     */
101
    public static function bare($statusCode = 204, array $headers = [])
102
    {
103
        $response = static::sendFormat(\yii\web\Response::FORMAT_RAW, null, $headers);
104
        $response->setStatusCode($statusCode);
105
        return $response;
106
    }
107
108
    /**
109
     * Sends the specified data as HTML.
110
     *
111
     * @param mixed $data data to be sent.
112
     * @param array $headers headers to be sent along with the response.
113
     *
114
     * @return \yii\web\Response the response object itself.
115
     */
116
    public static function html($data, array $headers = [])
117
    {
118
        return static::sendFormat(\yii\web\Response::FORMAT_HTML, $data, $headers);
119
    }
120
121
    /**
122
     * Sends the specified data as JSON.
123
     *
124
     * @param mixed $data data to be sent.
125
     * @param array $headers headers to be sent along with the response.
126
     *
127
     * @return \yii\web\Response the response object itself.
128
     */
129
    public static function json($data, array $headers = [])
130
    {
131
        return static::sendFormat(\yii\web\Response::FORMAT_JSON, $data, $headers);
132
    }
133
134
    /**
135
     * Sends the specified data as JSONP.
136
     *
137
     * @param mixed $data data to be sent.
138
     * @param string $callback the JSONP callback name.
139
     * @param array $headers headers to be sent along with the response.
140
     *
141
     * @return \yii\web\Response the response object itself.
142
     */
143
    public static function jsonp($data, $callback = 'callback', array $headers = [])
144
    {
145
        return static::sendFormat(\yii\web\Response::FORMAT_JSONP, [
146
            'callback' => $callback,
147
            'data' => $data
148
        ], $headers);
149
    }
150
151
    /**
152
     * Sends the specified data as is.
153
     *
154
     * @param mixed $data data to be sent.
155
     * @param array $headers headers to be sent along with the response.
156
     *
157
     * @return \yii\web\Response the response object itself.
158
     */
159
    public static function raw($data, array $headers = [])
160
    {
161
        return static::sendFormat(\yii\web\Response::FORMAT_RAW, $data, $headers);
162
    }
163
164
    /**
165
     * Sends the specified data as XML.
166
     *
167
     * @param mixed $data data to be sent.
168
     * @param array $headers headers to be sent along with the response.
169
     *
170
     * @return \yii\web\Response the response object itself.
171
     */
172
    public static function xml($data, array $headers = [])
173
    {
174
        return static::sendFormat(\yii\web\Response::FORMAT_XML, $data, $headers);
175
    }
176
177
    /**
178
     * Sends the specified data as the specified format.
179
     *
180
     * @param string $format the format to be set.
181
     * @param mixed $data data to be sent.
182
     * @param array $headers headers to be sent along with the response.
183
     *
184
     * @return \yii\web\Response the response object itself.
185
     */
186
    protected static function sendFormat($format, $data, array $headers)
187
    {
188
        /**
189
         * @var \yii\web\Response $response
190
         */
191
        $response = static::getFacadeComponent();
192
        $response->format = $format;
193
        $response->data = $data;
194
        foreach ($headers as $name => $value) {
195
            if (strtolower($name) === 'location') {
196
                /** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
197
                $value = \yii\helpers\Url::to($value);
198
                if (strpos($value, '/') === 0 && strpos($value, '//') !== 0) {
199
                    $value = Request::getHostInfo() . $value;
200
                }
201
            }
202
            $response->getHeaders()->set($name, $value);
203
        }
204
        return $response;
205
    }
206
}
207