Skip to content

Commit d0f4799

Browse files
committed
[1.x] Support PSR-7 v2 along side v1
This changeset introduces our PSR-7 v2 implementation alongside our PSR-7 v1 implementation and goes with v1 or v2 depending on which `psr/http-message` version is installed. This is admittedly not my best code, nor am I proud of it, but it gets the job done. This relies on clue/reactphp-http-proxy#65 being merged first. Implements reactphp#513 for `react/http` v1.
1 parent b489e05 commit d0f4799

27 files changed

Lines changed: 2415 additions & 8 deletions

‎composer.json‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,17 @@
2929
"php": ">=5.3.0",
3030
"evenement/evenement": "^3.0 || ^2.0 || ^1.0",
3131
"fig/http-message-util": "^1.1",
32-
"psr/http-message": "^1.0",
32+
"psr/http-message": "^2.0 || ^1.0",
3333
"react/event-loop": "^1.2",
3434
"react/promise": "^3.2 || ^2.3 || ^1.2.1",
3535
"react/socket": "^1.16",
3636
"react/stream": "^1.4"
3737
},
3838
"require-dev": {
39-
"clue/http-proxy-react": "^1.8",
39+
"clue/http-proxy-react": "^1.10",
4040
"clue/reactphp-ssh-proxy": "^1.4",
4141
"clue/socks-react": "^1.4",
42-
"phpunit/phpunit": "^9.6 || ^5.7 || ^4.8.36",
42+
"phpunit/phpunit": "^9.6 || ^8.5 || ^5.7 || ^4.8.36",
4343
"react/async": "^4.2 || ^3 || ^2",
4444
"react/promise-stream": "^1.4",
4545
"react/promise-timer": "^1.11"

‎phpunit.xml.dist‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
</include>
1919
</coverage>
2020
<php>
21+
<ini name="memory_limit" value="-1" />
2122
<ini name="error_reporting" value="-1" />
2223
<!-- Evaluate assertions, requires running with "php -d zend.assertions=1 vendor/bin/phpunit" -->
2324
<!-- <ini name="zend.assertions" value="1" /> -->

‎phpunit.xml.legacy‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
</whitelist>
1717
</filter>
1818
<php>
19+
<ini name="memory_limit" value="-1" />
1920
<ini name="error_reporting" value="-1" />
2021
<!-- Evaluate assertions, requires running with "php -d zend.assertions=1 vendor/bin/phpunit" -->
2122
<!-- <ini name="zend.assertions" value="1" /> -->

‎src/Io/AbstractMessage.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Psr\Http\Message\MessageInterface;
66
use Psr\Http\Message\StreamInterface;
77

8+
if (PHP_VERSION_ID < 70000 || !(new \ReflectionMethod('\Psr\Http\Message\MessageInterface','withProtocolVersion'))->hasReturnType()) {
89
/**
910
* [Internal] Abstract HTTP message base class (PSR-7)
1011
*
@@ -170,3 +171,6 @@ public function withBody(StreamInterface $body)
170171
return $message;
171172
}
172173
}
174+
} else {
175+
class_alias(__NAMESPACE__ . '\\V2\\AbstractMessage', __NAMESPACE__ . '\\AbstractMessage');
176+
}

‎src/Io/AbstractRequest.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Psr\Http\Message\UriInterface;
88
use React\Http\Message\Uri;
99

10+
if (PHP_VERSION_ID < 70000 || !(new \ReflectionMethod('\Psr\Http\Message\RequestInterface','withRequestTarget'))->hasReturnType()) {
1011
/**
1112
* [Internal] Abstract HTTP request base class (PSR-7)
1213
*
@@ -154,3 +155,6 @@ public function withUri(UriInterface $uri, $preserveHost = false)
154155
return $request;
155156
}
156157
}
158+
} else {
159+
class_alias(__NAMESPACE__ . '\\V2\\AbstractRequest', __NAMESPACE__ . '\\AbstractRequest');
160+
}

‎src/Io/BufferedBody.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Psr\Http\Message\StreamInterface;
66

7+
if (PHP_VERSION_ID < 70000 || !(new \ReflectionMethod('\Psr\Http\Message\StreamInterface','eof'))->hasReturnType()) {
78
/**
89
* [Internal] PSR-7 message body implementation using an in-memory buffer
910
*
@@ -177,3 +178,6 @@ public function getMetadata($key = null)
177178
return $key === null ? array() : null;
178179
}
179180
}
181+
} else {
182+
class_alias(__NAMESPACE__ . '\\V2\\BufferedBody', __NAMESPACE__ . '\\BufferedBody');
183+
}

‎src/Io/EmptyBodyStream.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use React\Stream\Util;
99
use React\Stream\WritableStreamInterface;
1010

11+
if (PHP_VERSION_ID < 70000 || !(new \ReflectionMethod('\Psr\Http\Message\StreamInterface','eof'))->hasReturnType()) {
1112
/**
1213
* [Internal] Bridge between an empty StreamInterface from PSR-7 and ReadableStreamInterface from ReactPHP
1314
*
@@ -140,3 +141,6 @@ public function getMetadata($key = null)
140141
return ($key === null) ? array() : null;
141142
}
142143
}
144+
} else {
145+
class_alias(__NAMESPACE__ . '\\V2\\EmptyBodyStream', __NAMESPACE__ . '\\EmptyBodyStream');
146+
}

‎src/Io/HttpBodyStream.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use React\Stream\Util;
99
use React\Stream\WritableStreamInterface;
1010

11+
if (PHP_VERSION_ID < 70000 || !(new \ReflectionMethod('\Psr\Http\Message\StreamInterface','eof'))->hasReturnType()) {
1112
/**
1213
* [Internal] Bridge between StreamInterface from PSR-7 and ReadableStreamInterface from ReactPHP
1314
*
@@ -180,3 +181,6 @@ public function handleEnd()
180181
}
181182
}
182183
}
184+
} else {
185+
class_alias(__NAMESPACE__ . '\\V2\\HttpBodyStream', __NAMESPACE__ . '\\HttpBodyStream');
186+
}

‎src/Io/ReadableBodyStream.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use React\Stream\Util;
99
use React\Stream\WritableStreamInterface;
1010

11+
$reflectedMethod = new \ReflectionMethod('\Psr\Http\Message\StreamInterface','eof');
12+
if (!(PHP_VERSION_ID >= 70000 && $reflectedMethod->hasReturnType())) {
1113
/**
1214
* @internal
1315
*/
@@ -151,3 +153,6 @@ public function handleEnd()
151153
$this->close();
152154
}
153155
}
156+
} else {
157+
class_alias(__NAMESPACE__ . '\\V2\\ReadableBodyStream', __NAMESPACE__ . '\\ReadableBodyStream');
158+
}

‎src/Io/UploadedFile.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use InvalidArgumentException;
88
use RuntimeException;
99

10+
if (PHP_VERSION_ID < 70000 || !(new \ReflectionMethod('\Psr\Http\Message\UploadedFileInterface','getStream'))->hasReturnType()) {
1011
/**
1112
* [Internal] Implementation of the PSR-7 `UploadedFileInterface`
1213
*
@@ -128,3 +129,6 @@ public function getClientMediaType()
128129
return $this->mediaType;
129130
}
130131
}
132+
} else {
133+
class_alias(__NAMESPACE__ . '\\V2\\UploadedFile', __NAMESPACE__ . '\\UploadedFile');
134+
}

0 commit comments

Comments
 (0)