Skip to content

Commit da6ebfa

Browse files
committed
Add last-ditch attempt to recover from invalid XHTML when things go south
1 parent 8316ad3 commit da6ebfa

File tree

3 files changed

+34
-10
lines changed

3 files changed

+34
-10
lines changed

Integration/XPathHelperFactory.php

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,47 @@
88

99
namespace Webfactory\Bundle\LegacyIntegrationBundle\Integration;
1010

11+
use Psr\Log\LoggerInterface;
12+
use Psr\Log\NullLogger;
1113
use Webfactory\Dom\BaseParsingHelper;
14+
use Webfactory\Dom\Exception\ParsingException;
1215

1316
class XPathHelperFactory
1417
{
15-
1618
/** @var BaseParsingHelper */
1719
protected $parser;
1820

1921
/** @var LegacyApplication */
2022
protected $legacyApplication;
2123

22-
public function __construct(BaseParsingHelper $parser, LegacyApplication $legacy)
24+
/**
25+
* @var LoggerInterface
26+
*/
27+
private $logger;
28+
29+
public function __construct(BaseParsingHelper $parser, LegacyApplication $legacy, LoggerInterface $logger = null)
2330
{
2431
$this->parser = $parser;
2532
$this->legacyApplication = $legacy;
33+
$this->logger = $logger ?: new NullLogger();
2634
}
2735

2836
public function createHelper()
2937
{
30-
return new XPathHelper($this->parser, $this->legacyApplication->getResponse()->getContent());
38+
$content = $this->legacyApplication->getResponse()->getContent();
39+
40+
try {
41+
return new XPathHelper($this->parser, $content);
42+
} catch (ParsingException $exception) {
43+
if (function_exists('tidy_repair_string')) {
44+
$this->logger->notice('Failed parsing the legacy response as XHTML, trying to clean up with Tidy.', ['exception' => $exception, 'legacy_response' => $exception->getXmlInput()]);
45+
46+
return new XPathHelper($this->parser, tidy_repair_string($content, ['output-xhtml' => true], 'utf8'));
47+
} else {
48+
$this->logger->warning('Failed to process legacy response as XHTML.', ['exception' => $exception, 'legacy_response' => $exception->getXmlInput()]);
49+
50+
throw $exception;
51+
}
52+
}
3153
}
3254
}

Resources/config/services.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
<service class="%webfactory_legacy_integration.parser_class%"/>
4242
</argument>
4343
<argument type="service" id="webfactory_legacy_integration.legacy_application"/>
44+
<argument type="service" id="logger" on-invalid="null" />
45+
<tag name="monolog.logger" channel="webfactory_legacy_integration" />
4446
</service>
4547

4648
<service id="webfactory_legacy_integration.xpath_helper"

composer.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@
1717
},
1818

1919
"require": {
20-
"symfony/framework-bundle": "~2.2",
21-
"symfony/dependency-injection": "~2.6",
20+
"doctrine/annotations": "~1.0",
21+
"psr/log": "^1.0",
2222
"symfony/config": "~2.2",
23-
"symfony/http-kernel": "~2.2",
23+
"symfony/dependency-injection": "~2.6",
24+
"symfony/framework-bundle": "~2.2",
2425
"symfony/http-foundation": "~2.2",
25-
"doctrine/annotations": "~1.0",
26-
"webfactory/dom": "~1.0, >= 1.0.15",
27-
"twig/twig": "^1.23"
28-
26+
"symfony/http-kernel": "~2.2",
27+
"twig/twig": "^1.23",
28+
"webfactory/dom": "~1.0, >= 1.0.15"
2929
}
3030

3131
}

0 commit comments

Comments
 (0)