Skip to content

Commit

Permalink
Allow installing under PHP 8
Browse files Browse the repository at this point in the history
1. Allow PHPUnit 9 (needed for PHP 8)

2. Shim deprecated libxml functions

Make the call to libxml_disable_entity_loader conditional.  This
function has been deprecated in PHP 8.0 and the new default is
equivalent to calling that function with `true`.

https://php.watch/versions/8.0/libxml_disable_entity_loader-deprecation

Note: there were two instances of exactly the same XML-parsing anonymous
function. I've traced this duplication back to 2015 when the functions
were one line long and explicitness might have been better than
deduplication. Over the years these functions have been growing together
and I see no obvious reason why two copies should be maintained.
  • Loading branch information
edudobay committed Nov 20, 2020
1 parent 55a46cc commit 4715329
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 4715329

Please sign in to comment.