Skip to content

Commit f2edc17

Browse files
committed
bug #39068 [DependencyInjection][Translator] Silent deprecation triggered by libxml_disable_entity_loader (jderusse)
This PR was squashed before being merged into the 4.4 branch. Discussion ---------- [DependencyInjection][Translator] Silent deprecation triggered by libxml_disable_entity_loader | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #39040 | License | MIT | Doc PR | - The XML entity loader is disabled by default since libxml 2.9 But, since PHP 8.0, calling the method `libxml_disable_entity_loader` triggers a deprecation which has been solved in symfony by calling `libxml_disable_entity_loader` only for libxml < 2.9 The issue is, some dependencies, enable the entity loader and does not restore the initial state afterward, leading to exceptions triggered by Symfony iteself. In previous versions symfony was resilient by disabling the flag before working, which is not the case anymore to avoid the deprecation. This PR restore the resiliency of Symfony for PHP < 8.0, which is not yet deprecated. But we have no way to check the status of the entity loader without triggering a deprecation with Symfony 8. Commits ------- 114b7a543a [DependencyInjection][Translator] Silent deprecation triggered by libxml_disable_entity_loader
2 parents 45d8c7b + 5979a7f commit f2edc17

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)