Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Http;
4
 
5
use Illuminate\Http\Exceptions\HttpResponseException;
6
use Symfony\Component\HttpFoundation\HeaderBag;
7
use Throwable;
8
 
9
trait ResponseTrait
10
{
11
    /**
12
     * The original content of the response.
13
     *
14
     * @var mixed
15
     */
16
    public $original;
17
 
18
    /**
19
     * The exception that triggered the error response (if applicable).
20
     *
21
     * @var \Throwable|null
22
     */
23
    public $exception;
24
 
25
    /**
26
     * Get the status code for the response.
27
     *
28
     * @return int
29
     */
30
    public function status()
31
    {
32
        return $this->getStatusCode();
33
    }
34
 
35
    /**
36
     * Get the status text for the response.
37
     *
38
     * @return string
39
     */
40
    public function statusText()
41
    {
42
        return $this->statusText;
43
    }
44
 
45
    /**
46
     * Get the content of the response.
47
     *
48
     * @return string
49
     */
50
    public function content()
51
    {
52
        return $this->getContent();
53
    }
54
 
55
    /**
56
     * Get the original response content.
57
     *
58
     * @return mixed
59
     */
60
    public function getOriginalContent()
61
    {
62
        $original = $this->original;
63
 
64
        return $original instanceof self ? $original->{__FUNCTION__}() : $original;
65
    }
66
 
67
    /**
68
     * Set a header on the Response.
69
     *
70
     * @param  string  $key
71
     * @param  array|string  $values
72
     * @param  bool  $replace
73
     * @return $this
74
     */
75
    public function header($key, $values, $replace = true)
76
    {
77
        $this->headers->set($key, $values, $replace);
78
 
79
        return $this;
80
    }
81
 
82
    /**
83
     * Add an array of headers to the response.
84
     *
85
     * @param  \Symfony\Component\HttpFoundation\HeaderBag|array  $headers
86
     * @return $this
87
     */
88
    public function withHeaders($headers)
89
    {
90
        if ($headers instanceof HeaderBag) {
91
            $headers = $headers->all();
92
        }
93
 
94
        foreach ($headers as $key => $value) {
95
            $this->headers->set($key, $value);
96
        }
97
 
98
        return $this;
99
    }
100
 
101
    /**
102
     * Add a cookie to the response.
103
     *
104
     * @param  \Symfony\Component\HttpFoundation\Cookie|mixed  $cookie
105
     * @return $this
106
     */
107
    public function cookie($cookie)
108
    {
109
        return $this->withCookie(...func_get_args());
110
    }
111
 
112
    /**
113
     * Add a cookie to the response.
114
     *
115
     * @param  \Symfony\Component\HttpFoundation\Cookie|mixed  $cookie
116
     * @return $this
117
     */
118
    public function withCookie($cookie)
119
    {
120
        if (is_string($cookie) && function_exists('cookie')) {
121
            $cookie = cookie(...func_get_args());
122
        }
123
 
124
        $this->headers->setCookie($cookie);
125
 
126
        return $this;
127
    }
128
 
129
    /**
130
     * Expire a cookie when sending the response.
131
     *
132
     * @param  \Symfony\Component\HttpFoundation\Cookie|mixed  $cookie
133
     * @param  string|null  $path
134
     * @param  string|null  $domain
135
     * @return $this
136
     */
137
    public function withoutCookie($cookie, $path = null, $domain = null)
138
    {
139
        if (is_string($cookie) && function_exists('cookie')) {
140
            $cookie = cookie($cookie, null, -2628000, $path, $domain);
141
        }
142
 
143
        $this->headers->setCookie($cookie);
144
 
145
        return $this;
146
    }
147
 
148
    /**
149
     * Get the callback of the response.
150
     *
151
     * @return string|null
152
     */
153
    public function getCallback()
154
    {
155
        return $this->callback ?? null;
156
    }
157
 
158
    /**
159
     * Set the exception to attach to the response.
160
     *
161
     * @param  \Throwable  $e
162
     * @return $this
163
     */
164
    public function withException(Throwable $e)
165
    {
166
        $this->exception = $e;
167
 
168
        return $this;
169
    }
170
 
171
    /**
172
     * Throws the response in a HttpResponseException instance.
173
     *
174
     * @return void
175
     *
176
     * @throws \Illuminate\Http\Exceptions\HttpResponseException
177
     */
178
    public function throwResponse()
179
    {
180
        throw new HttpResponseException($this);
181
    }
182
}