Skip to content

Commit 5979a7f

Browse files
jderussefabpot
authored andcommitted
[DependencyInjection][Translator] Silent deprecation triggered by libxml_disable_entity_loader
1 parent eaddaff commit 5979a7f

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

Loader/XmlFileLoader.php

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -634,21 +634,50 @@ public function validateSchema(\DOMDocument $dom)
634634
EOF
635635
;
636636

637-
if (\LIBXML_VERSION < 20900) {
637+
if ($this->shouldEnableEntityLoader()) {
638638
$disableEntities = libxml_disable_entity_loader(false);
639639
$valid = @$dom->schemaValidateSource($source);
640640
libxml_disable_entity_loader($disableEntities);
641641
} else {
642642
$valid = @$dom->schemaValidateSource($source);
643643
}
644-
645644
foreach ($tmpfiles as $tmpfile) {
646645
@unlink($tmpfile);
647646
}
648647

649648
return $valid;
650649
}
651650

651+
private function shouldEnableEntityLoader(): bool
652+
{
653+
// Version prior to 8.0 can be enabled without deprecation
654+
if (\PHP_VERSION_ID < 80000) {
655+
return true;
656+
}
657+
658+
static $dom, $schema;
659+
if (null === $dom) {
660+
$dom = new \DOMDocument();
661+
$dom->loadXML('<?xml version="1.0"?><test/>');
662+
663+
$tmpfile = tempnam(sys_get_temp_dir(), 'symfony');
664+
register_shutdown_function(static function () use ($tmpfile) {
665+
@unlink($tmpfile);
666+
});
667+
$schema = '<?xml version="1.0" encoding="utf-8"?>
668+
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
669+
<xsd:include schemaLocation="file:///'.str_replace('\\', '/', $tmpfile).'" />
670+
</xsd:schema>';
671+
file_put_contents($tmpfile, '<?xml version="1.0" encoding="utf-8"?>
672+
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
673+
<xsd:element name="test" type="testType" />
674+
<xsd:complexType name="testType"/>
675+
</xsd:schema>');
676+
}
677+
678+
return !@$dom->schemaValidateSource($schema);
679+
}
680+
652681
private function validateAlias(\DOMElement $alias, string $file)
653682
{
654683
foreach ($alias->attributes as $name => $node) {

0 commit comments

Comments
 (0)