Skip to content

Commit

Permalink
Merge pull request #142 from edudobay/php8
Browse files Browse the repository at this point in the history
PHP 8 support
  • Loading branch information
l0gicgate committed Nov 20, 2020
2 parents 55a46cc + 4715329 commit 3bc9d61
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
}
],
"require": {
"php": "^7.2",
"php": "^7.2 || ^8.0",
"ext-SimpleXML": "*",
"ext-fileinfo": "*",
"ext-json": "*",
Expand All @@ -42,7 +42,7 @@
"nyholm/psr7": "^1.3",
"php-http/psr7-integration-tests": "dev-master",
"phpstan/phpstan": "^0.12.52",
"phpunit/phpunit": "^8.5",
"phpunit/phpunit": "^8.5 || ^9.3",
"squizlabs/php_codesniffer": "^3.5"
},
"autoload": {
Expand Down
38 changes: 19 additions & 19 deletions src/ServerRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ final public function __construct(ServerRequestInterface $serverRequest)
return $result;
});

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

libxml_disable_entity_loader($backup);
self::disableXmlEntityLoader($backup);
libxml_clear_errors();
libxml_use_internal_errors($backup_errors);

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

return $result;
});

$this->registerMediaTypeParser('text/xml', function ($input) {
$backup = libxml_disable_entity_loader(true);
$backup_errors = libxml_use_internal_errors(true);
$result = simplexml_load_string($input);

libxml_disable_entity_loader($backup);
libxml_clear_errors();
libxml_use_internal_errors($backup_errors);

if ($result === false) {
return null;
}
};

return $result;
});
$this->registerMediaTypeParser('application/xml', $xmlParserCallable);
$this->registerMediaTypeParser('text/xml', $xmlParserCallable);

$this->registerMediaTypeParser('application/x-www-form-urlencoded', function ($input) {
parse_str($input, $data);
Expand Down Expand Up @@ -781,4 +768,17 @@ public function isXhr(): bool
{
return $this->serverRequest->getHeaderLine('X-Requested-With') === 'XMLHttpRequest';
}

private static function disableXmlEntityLoader(bool $disable): bool
{
if (\LIBXML_VERSION >= 20900) {
// libxml >= 2.9.0 disables entity loading by default, so it is
// safe to skip the real call (deprecated in PHP 8).
return true;
}

// @codeCoverageIgnoreStart
return libxml_disable_entity_loader($disable);
// @codeCoverageIgnoreEnd
}
}

0 comments on commit 3bc9d61

Please sign in to comment.