Skip to content

Commit 6ae5dba

Browse files
committed
Avoid lots of uncessary exceptions during layout XML merge loading
* Introduce a new function (for B/C compat we don't overwrite the protected _loadXmlString) to load and not throw an exception * Use it in extract handlers -> no need to catch the exception any more Reason: When using tools like Sentry, because of there inner workings, such exceptions still might be reported. While this is not exactly the root cause, it's always cleaner to not throw an exception and immediately catch it, if invalid cases can happen often. This might even improve performance a bit and improves developer's experience.
1 parent 1c874bd commit 6ae5dba

File tree

1 file changed

+12
-9
lines changed
  • lib/internal/Magento/Framework/View/Model/Layout

1 file changed

+12
-9
lines changed

lib/internal/Magento/Framework/View/Model/Layout/Merge.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,17 @@ protected function _loadXmlString($xmlString)
564564
return simplexml_load_string($xmlString, \Magento\Framework\View\Layout\Element::class);
565565
}
566566

567+
/**
568+
* Return object representation of XML string, or false, if XML was invalid
569+
*
570+
* @param string $xmlString
571+
* @return \SimpleXMLElement
572+
*/
573+
protected function _safeLoadXmlString($xmlString)
574+
{
575+
return simplexml_load_string($xmlString, \Magento\Framework\View\Layout\Element::class, LIBXML_NOWARNING | LIBXML_NOERROR);
576+
}
577+
567578
/**
568579
* Merge layout update by handle
569580
*
@@ -988,15 +999,7 @@ public function getCacheId()
988999
private function extractHandlers(): void
9891000
{
9901001
foreach ($this->updates as $update) {
991-
$updateXml = null;
992-
993-
try {
994-
$updateXml = is_string($update) ? $this->_loadXmlString($update) : false;
995-
// phpcs:ignore Magento2.CodeAnalysis.EmptyBlock
996-
} catch (\Exception $exception) {
997-
// ignore invalid
998-
}
999-
1002+
$updateXml = is_string($update) ? $this->_safeLoadXmlString($update) : false;
10001003
if ($updateXml && strtolower($updateXml->getName()) == 'update' && isset($updateXml['handle'])) {
10011004
$this->addHandle((string)$updateXml['handle']);
10021005
}

0 commit comments

Comments
 (0)