44
55namespace DocbookCS \Config ;
66
7+ use DocbookCS \Xml \XmlParser ;
8+
79final class ConfigParser
810{
911 private const string NAMESPACE_URI = 'https://php.github.io/docbook-cs/config ' ;
1012
13+ public function __construct (private readonly XmlParser $ xmlParser = new XmlParser ())
14+ {
15+ }
16+
1117 /**
1218 * @throws ConfigParserException if the file cannot be read or contains invalid XML.
1319 * @throws \InvalidArgumentException if a SniffEntry is constructed with an invalid class name.
@@ -19,21 +25,13 @@ public function parseFile(string $filePath): ConfigData
1925 }
2026
2127 $ basePath = dirname (realpath ($ filePath ) ?: '' );
28+ $ xml = $ this ->xmlParser ->parseElement (file_get_contents ($ filePath ) ?: '' );
2229
23- $ previousUseErrors = libxml_use_internal_errors (true );
24- $ xml = simplexml_load_string (file_get_contents ($ filePath ) ?: '' );
25- $ errors = libxml_get_errors ();
26- libxml_clear_errors ();
27- libxml_use_internal_errors ($ previousUseErrors );
28-
29- if ($ xml === false ) {
30- $ message = $ errors !== []
31- ? $ errors [0 ]->message
32- : 'Unknown parse error ' ; // @codeCoverageIgnore
33- throw ConfigParserException::invalidXml ($ filePath , trim ($ message ));
30+ if ($ xml instanceof \LibXMLError) {
31+ throw ConfigParserException::invalidXml ($ filePath , trim ($ xml ->message ));
3432 }
3533
36- return $ this ->parse ($ xml , $ basePath );
34+ return $ this ->createConfigData ($ xml , $ basePath );
3735 }
3836
3937 /**
@@ -42,27 +40,20 @@ public function parseFile(string $filePath): ConfigData
4240 */
4341 public function parseString (string $ xmlContent , string $ basePath ): ConfigData
4442 {
45- $ previousUseErrors = libxml_use_internal_errors (true );
46- $ xml = simplexml_load_string ($ xmlContent );
47- $ errors = libxml_get_errors ();
48- libxml_clear_errors ();
49- libxml_use_internal_errors ($ previousUseErrors );
50-
51- if ($ xml === false ) {
52- $ message = $ errors !== []
53- ? $ errors [0 ]->message
54- : 'Unknown parse error ' ; // @codeCoverageIgnore
55- throw ConfigParserException::invalidXml ('(string) ' , trim ($ message ));
43+ $ xml = $ this ->xmlParser ->parseElement ($ xmlContent );
44+
45+ if ($ xml instanceof \LibXMLError) {
46+ throw ConfigParserException::invalidXml ('(string) ' , trim ($ xml ->message ));
5647 }
5748
58- return $ this ->parse ($ xml , $ basePath );
49+ return $ this ->createConfigData ($ xml , $ basePath );
5950 }
6051
6152 /**
6253 * @throws ConfigParserException if the XML is invalid or required elements/attributes are missing.
6354 * @throws \InvalidArgumentException if a SniffEntry is constructed with an invalid class name.
6455 */
65- private function parse (\SimpleXMLElement $ root , string $ basePath ): ConfigData
56+ private function createConfigData (\SimpleXMLElement $ root , string $ basePath ): ConfigData
6657 {
6758 // Register the namespace for xpath queries.
6859 $ root ->registerXPathNamespace ('d ' , self ::NAMESPACE_URI );
0 commit comments