Skip to content

Commit 3bc9d61

Browse files
authored
Merge pull request #142 from edudobay/php8
PHP 8 support
2 parents 55a46cc + 4715329 commit 3bc9d61

File tree

2 files changed

+21
-21
lines changed

2 files changed

+21
-21
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
}
2929
],
3030
"require": {
31-
"php": "^7.2",
31+
"php": "^7.2 || ^8.0",
3232
"ext-SimpleXML": "*",
3333
"ext-fileinfo": "*",
3434
"ext-json": "*",
@@ -42,7 +42,7 @@
4242
"nyholm/psr7": "^1.3",
4343
"php-http/psr7-integration-tests": "dev-master",
4444
"phpstan/phpstan": "^0.12.52",
45-
"phpunit/phpunit": "^8.5",
45+
"phpunit/phpunit": "^8.5 || ^9.3",
4646
"squizlabs/php_codesniffer": "^3.5"
4747
},
4848
"autoload": {

src/ServerRequest.php

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@ final public function __construct(ServerRequestInterface $serverRequest)
6161
return $result;
6262
});
6363

64-
$this->registerMediaTypeParser('application/xml', function ($input) {
65-
$backup = libxml_disable_entity_loader(true);
64+
$xmlParserCallable = function ($input) {
65+
$backup = self::disableXmlEntityLoader(true);
6666
$backup_errors = libxml_use_internal_errors(true);
6767
$result = simplexml_load_string($input);
6868

69-
libxml_disable_entity_loader($backup);
69+
self::disableXmlEntityLoader($backup);
7070
libxml_clear_errors();
7171
libxml_use_internal_errors($backup_errors);
7272

@@ -75,23 +75,10 @@ final public function __construct(ServerRequestInterface $serverRequest)
7575
}
7676

7777
return $result;
78-
});
79-
80-
$this->registerMediaTypeParser('text/xml', function ($input) {
81-
$backup = libxml_disable_entity_loader(true);
82-
$backup_errors = libxml_use_internal_errors(true);
83-
$result = simplexml_load_string($input);
84-
85-
libxml_disable_entity_loader($backup);
86-
libxml_clear_errors();
87-
libxml_use_internal_errors($backup_errors);
88-
89-
if ($result === false) {
90-
return null;
91-
}
78+
};
9279

93-
return $result;
94-
});
80+
$this->registerMediaTypeParser('application/xml', $xmlParserCallable);
81+
$this->registerMediaTypeParser('text/xml', $xmlParserCallable);
9582

9683
$this->registerMediaTypeParser('application/x-www-form-urlencoded', function ($input) {
9784
parse_str($input, $data);
@@ -781,4 +768,17 @@ public function isXhr(): bool
781768
{
782769
return $this->serverRequest->getHeaderLine('X-Requested-With') === 'XMLHttpRequest';
783770
}
771+
772+
private static function disableXmlEntityLoader(bool $disable): bool
773+
{
774+
if (\LIBXML_VERSION >= 20900) {
775+
// libxml >= 2.9.0 disables entity loading by default, so it is
776+
// safe to skip the real call (deprecated in PHP 8).
777+
return true;
778+
}
779+
780+
// @codeCoverageIgnoreStart
781+
return libxml_disable_entity_loader($disable);
782+
// @codeCoverageIgnoreEnd
783+
}
784784
}

0 commit comments

Comments
 (0)