1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
From eb80fbb48ce47070507918fd692f8c20f65816dd Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= <dunglas@gmail.com>
Date: Thu, 9 Jul 2015 11:23:44 +0200
Subject: [PATCH 1/3] [HttpFoundation] Allow to use resources as content body
and to return resources from string content.
---
src/Symfony/Component/HttpFoundation/Request.php | 52 +++++++++++++++-------
.../Component/HttpFoundation/Tests/RequestTest.php | 20 +++++++++
2 files changed, 57 insertions(+), 15 deletions(-)
diff --git a/src/Symfony/Component/HttpFoundation/Request.php b/src/Symfony/Component/HttpFoundation/Request.php
index 84b3a69..6fa20ac 100644
--- a/src/Symfony/Component/HttpFoundation/Request.php
+++ b/src/Symfony/Component/HttpFoundation/Request.php
@@ -199,13 +199,13 @@ class Request
/**
* Constructor.
*
- * @param array $query The GET parameters
- * @param array $request The POST parameters
- * @param array $attributes The request attributes (parameters parsed from the PATH_INFO, ...)
- * @param array $cookies The COOKIE parameters
- * @param array $files The FILES parameters
- * @param array $server The SERVER parameters
- * @param string $content The raw body data
+ * @param array $query The GET parameters
+ * @param array $request The POST parameters
+ * @param array $attributes The request attributes (parameters parsed from the PATH_INFO, ...)
+ * @param array $cookies The COOKIE parameters
+ * @param array $files The FILES parameters
+ * @param array $server The SERVER parameters
+ * @param string|resource $content The raw body data
*
* @api
*/
@@ -219,13 +219,13 @@ public function __construct(array $query = array(), array $request = array(), ar
*
* This method also re-initializes all properties.
*
- * @param array $query The GET parameters
- * @param array $request The POST parameters
- * @param array $attributes The request attributes (parameters parsed from the PATH_INFO, ...)
- * @param array $cookies The COOKIE parameters
- * @param array $files The FILES parameters
- * @param array $server The SERVER parameters
- * @param string $content The raw body data
+ * @param array $query The GET parameters
+ * @param array $request The POST parameters
+ * @param array $attributes The request attributes (parameters parsed from the PATH_INFO, ...)
+ * @param array $cookies The COOKIE parameters
+ * @param array $files The FILES parameters
+ * @param array $server The SERVER parameters
+ * @param string|resource $content The raw body data
*
* @api
*/
@@ -1465,16 +1465,38 @@ public function isMethodSafe()
*/
public function getContent($asResource = false)
{
- if (PHP_VERSION_ID < 50600 && (false === $this->content || (true === $asResource && null !== $this->content))) {
+ $currentContentIsResource = is_resource($this->content);
+ if (PHP_VERSION_ID < 50600 && !$currentContentIsResource && (false === $this->content || (true === $asResource && null !== $this->content))) {
throw new \LogicException('getContent() can only be called once when using the resource return type and PHP below 5.6.');
}
if (true === $asResource) {
+ if ($currentContentIsResource) {
+ rewind($this->content);
+
+ return $this->content;
+ }
+
+ // Content passed in parameter (test)
+ if (is_string($this->content)) {
+ $resource = fopen('php://temp','r+');
+ fwrite($resource, $this->content);
+ rewind($resource);
+
+ return $resource;
+ }
+
$this->content = false;
return fopen('php://input', 'rb');
}
+ if ($currentContentIsResource) {
+ rewind($this->content);
+
+ return stream_get_contents($this->content);
+ }
+
if (null === $this->content) {
$this->content = file_get_contents('php://input');
}
diff --git a/src/Symfony/Component/HttpFoundation/Tests/RequestTest.php b/src/Symfony/Component/HttpFoundation/Tests/RequestTest.php
index 366b555..fcc73f5 100644
--- a/src/Symfony/Component/HttpFoundation/Tests/RequestTest.php
+++ b/src/Symfony/Component/HttpFoundation/Tests/RequestTest.php
@@ -923,6 +923,26 @@ public function testGetContentReturnsResource()
$this->assertTrue(feof($retval));
}
+ public function testGetContentReturnsResourceWhenContentSetInConstructor()
+ {
+ $req = new Request(array(), array(), array(), array(), array(), array(), 'MyContent');
+ $resource = $req->getContent(true);
+
+ $this->assertTrue(is_resource($resource));
+ $this->assertEquals('MyContent', stream_get_contents($resource));
+ }
+
+ public function testContentAsResource()
+ {
+ $resource = fopen('php://memory','r+');
+ fwrite($resource, 'My other content');
+ rewind($resource);
+
+ $req = new Request(array(), array(), array(), array(), array(), array(), $resource);
+ $this->assertEquals('My other content', stream_get_contents($req->getContent(true)));
+ $this->assertEquals('My other content', $req->getContent());
+ }
+
/**
* @expectedException \LogicException
* @dataProvider getContentCantBeCalledTwiceWithResourcesProvider
From bb6db5768b5bb7e3b1f5e71656d3ddf779151006 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= <dunglas@gmail.com>
Date: Thu, 16 Jul 2015 08:17:59 +0200
Subject: [PATCH 2/3] Simplify condition
---
src/Symfony/Component/HttpFoundation/Request.php | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/Symfony/Component/HttpFoundation/Request.php b/src/Symfony/Component/HttpFoundation/Request.php
index 6fa20ac..2d28251 100644
--- a/src/Symfony/Component/HttpFoundation/Request.php
+++ b/src/Symfony/Component/HttpFoundation/Request.php
@@ -1466,7 +1466,7 @@ public function isMethodSafe()
public function getContent($asResource = false)
{
$currentContentIsResource = is_resource($this->content);
- if (PHP_VERSION_ID < 50600 && !$currentContentIsResource && (false === $this->content || (true === $asResource && null !== $this->content))) {
+ if (PHP_VERSION_ID < 50600 && false === $this->content) {
throw new \LogicException('getContent() can only be called once when using the resource return type and PHP below 5.6.');
}
From fc90cfa0c70677b5b119cbbd882059552468d84b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= <dunglas@gmail.com>
Date: Thu, 16 Jul 2015 08:34:36 +0200
Subject: [PATCH 3/3] Fix test
---
src/Symfony/Component/HttpFoundation/Tests/RequestTest.php | 1 -
1 file changed, 1 deletion(-)
diff --git a/src/Symfony/Component/HttpFoundation/Tests/RequestTest.php b/src/Symfony/Component/HttpFoundation/Tests/RequestTest.php
index fcc73f5..797a00a 100644
--- a/src/Symfony/Component/HttpFoundation/Tests/RequestTest.php
+++ b/src/Symfony/Component/HttpFoundation/Tests/RequestTest.php
@@ -987,7 +987,6 @@ public function getContentCantBeCalledTwiceWithResourcesProvider()
return array(
'Resource then fetch' => array(true, false),
'Resource then resource' => array(true, true),
- 'Fetch then resource' => array(false, true),
);
}
|