|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* File containing the Visitor class. |
|
5
|
|
|
* |
|
6
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
|
7
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
|
8
|
|
|
* |
|
9
|
|
|
* @version //autogentag// |
|
10
|
|
|
*/ |
|
11
|
|
|
namespace eZ\Publish\Core\REST\Common\Output; |
|
12
|
|
|
|
|
13
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Visits a value object into an HTTP Response. |
|
17
|
|
|
*/ |
|
18
|
|
|
class Visitor |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var \eZ\Publish\Core\REST\Common\Output\ValueObjectVisitorDispatcher |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $valueObjectVisitorDispatcher = array(); |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Generator. |
|
27
|
|
|
* |
|
28
|
|
|
* @var \eZ\Publish\Core\REST\Common\Output\Generator |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $generator; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* HTTP Response Object. |
|
34
|
|
|
* |
|
35
|
|
|
* @var Response |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $response; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Used to ensure that the status code can't be overwritten. |
|
41
|
|
|
* |
|
42
|
|
|
* @var int |
|
43
|
|
|
*/ |
|
44
|
|
|
private $statusCode; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Construct from Generator and an array of concrete view model visitors. |
|
48
|
|
|
* |
|
49
|
|
|
* @param \eZ\Publish\Core\REST\Common\Output\Generator $generator |
|
50
|
|
|
* @param \eZ\Publish\Core\REST\Common\Output\ValueObjectVisitorDispatcher $valueObjectVisitorDispatcher |
|
51
|
|
|
* |
|
52
|
|
|
* @internal param array $visitors |
|
53
|
|
|
*/ |
|
54
|
|
|
public function __construct(Generator $generator, ValueObjectVisitorDispatcher $valueObjectVisitorDispatcher) |
|
55
|
|
|
{ |
|
56
|
|
|
$this->generator = $generator; |
|
57
|
|
|
$this->valueObjectVisitorDispatcher = $valueObjectVisitorDispatcher; |
|
58
|
|
|
$this->response = new Response('', 200); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Set HTTP response header. |
|
63
|
|
|
* |
|
64
|
|
|
* Does not allow overwriting of response headers. The first definition of |
|
65
|
|
|
* a header will be used. |
|
66
|
|
|
* |
|
67
|
|
|
* @param string $name |
|
68
|
|
|
* @param string $value |
|
69
|
|
|
*/ |
|
70
|
|
|
public function setHeader($name, $value) |
|
71
|
|
|
{ |
|
72
|
|
|
if (!$this->response->headers->has($name)) { |
|
73
|
|
|
$this->response->headers->set($name, $value); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Sets the given status code in the corresponding header. |
|
79
|
|
|
* |
|
80
|
|
|
* Note that headers are generally not overwritten! |
|
81
|
|
|
* |
|
82
|
|
|
* @param int $statusCode |
|
83
|
|
|
*/ |
|
84
|
|
|
public function setStatus($statusCode) |
|
85
|
|
|
{ |
|
86
|
|
|
if ($this->statusCode === null) { |
|
87
|
|
|
$this->statusCode = $statusCode; |
|
88
|
|
|
$this->response->setStatusCode($statusCode); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Visit struct returned by controllers. |
|
94
|
|
|
* |
|
95
|
|
|
* @param mixed $data |
|
96
|
|
|
* |
|
97
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
|
98
|
|
|
*/ |
|
99
|
|
|
public function visit($data) |
|
100
|
|
|
{ |
|
101
|
|
|
$this->generator->reset(); |
|
102
|
|
|
$this->generator->startDocument($data); |
|
103
|
|
|
|
|
104
|
|
|
$this->visitValueObject($data); |
|
105
|
|
|
|
|
106
|
|
|
//@todo Needs refactoring! |
|
107
|
|
|
// A hackish solution to enable outer visitors to disable setting |
|
108
|
|
|
// of certain headers in inner visitors, for example Accept-Patch header |
|
109
|
|
|
// which is valid in GET/POST/PATCH for a resource, but must not appear |
|
110
|
|
|
// in the list of resources |
|
111
|
|
|
foreach ($this->response->headers->all() as $headerName => $headerValue) { |
|
112
|
|
|
if ($headerValue[0] === false) { |
|
113
|
|
|
$this->response->headers->remove($headerName); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
$response = clone $this->response; |
|
118
|
|
|
|
|
119
|
|
|
$response->setContent($this->generator->isEmpty() ? null : $this->generator->endDocument($data)); |
|
120
|
|
|
|
|
121
|
|
|
// reset the inner response |
|
122
|
|
|
$this->response = new Response(null, 200); |
|
123
|
|
|
$this->statusCode = null; |
|
124
|
|
|
|
|
125
|
|
|
return $response; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Visit struct returned by controllers. |
|
130
|
|
|
* |
|
131
|
|
|
* Can be called by sub-visitors to visit nested objects. |
|
132
|
|
|
* |
|
133
|
|
|
* @param object $data |
|
134
|
|
|
* |
|
135
|
|
|
* @return mixed |
|
136
|
|
|
*/ |
|
137
|
|
|
public function visitValueObject($data, Generator $generator = null, Visitor $visitor = null) |
|
138
|
|
|
{ |
|
139
|
|
|
$this->valueObjectVisitorDispatcher->setOutputGenerator($this->generator); |
|
140
|
|
|
|
|
141
|
|
|
return $this->valueObjectVisitorDispatcher->visit( |
|
142
|
|
|
$data, |
|
143
|
|
|
$generator ?: $this->generator, |
|
144
|
|
|
$visitor ?: $this |
|
145
|
|
|
); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Generates a media type for $type based on the used generator. |
|
150
|
|
|
* |
|
151
|
|
|
* @param string $type |
|
152
|
|
|
* |
|
153
|
|
|
* @see \eZ\Publish\Core\REST\Common\Generator::getMediaType() |
|
154
|
|
|
* |
|
155
|
|
|
* @return string |
|
156
|
|
|
*/ |
|
157
|
|
|
public function getMediaType($type) |
|
158
|
|
|
{ |
|
159
|
|
|
return $this->generator->getMediaType($type); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* @return Response |
|
164
|
|
|
*/ |
|
165
|
|
|
public function getResponse() |
|
166
|
|
|
{ |
|
167
|
|
|
return $this->response; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
public function addVary($vary) |
|
171
|
|
|
{ |
|
172
|
|
|
$this->response->setVary($vary); |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
|