44
55namespace Propel \Generator \Builder \Util ;
66
7+ use Exception ;
78use Propel \Generator \Config \AbstractGeneratorConfig ;
89use Propel \Generator \Exception \SchemaException ;
910use Propel \Generator \Model \Index ;
2425use function strpos ;
2526use function strtolower ;
2627use function version_compare ;
27- use function vsprintf ;
2828use function xml_error_string ;
2929use function xml_get_current_column_number ;
3030use function xml_get_current_line_number ;
@@ -54,7 +54,7 @@ class SchemaReader
5454 private $ schema ;
5555
5656 /**
57- * @var \XMLParser|resource
57+ * @var \XMLParser
5858 */
5959 private $ parser ;
6060
@@ -201,14 +201,13 @@ public function parseString(string $xmlString, ?string $xmlFile = null): ?Schema
201201 $ this ->parser = xml_parser_create ();
202202 xml_parser_set_option ($ this ->parser , XML_OPTION_CASE_FOLDING , 0 );
203203 xml_set_element_handler ($ this ->parser , [$ this , 'startElement ' ], [$ this , 'endElement ' ]);
204- if (!xml_parse ($ this ->parser , $ xmlString )) {
205- throw new SchemaException (
206- sprintf (
207- 'XML error: %s at line %d ' ,
208- xml_error_string (xml_get_error_code ($ this ->parser )),
209- xml_get_current_line_number ($ this ->parser ),
210- ),
211- );
204+ try {
205+ $ success = xml_parse ($ this ->parser , $ xmlString );
206+ } catch (Exception $ e ) {
207+ throw new SchemaException ('XML parser failed at ' . $ this ->getLocationDescription () . ': ' . $ e ->getMessage (), 0 , $ e );
208+ }
209+ if (!$ success ) {
210+ $ this ->throwParserError ();
212211 }
213212 if (version_compare (phpversion (), '8.1 ' , '< ' )) {
214213 xml_parser_free ($ this ->parser );
@@ -221,7 +220,7 @@ public function parseString(string $xmlString, ?string $xmlFile = null): ?Schema
221220 }
222221
223222 /**
224- * @param resource $parser
223+ * @param \XMLParser $parser
225224 * @param string $tagName
226225 * @param array $attributes
227226 *
@@ -236,10 +235,7 @@ public function startElement($parser, string $tagName, array $attributes): void
236235 switch ($ tagName ) {
237236 case 'database ' :
238237 if ($ this ->isExternalSchema ()) {
239- $ this ->currentPackage = $ attributes ['package ' ] ?? null ;
240- if ($ this ->currentPackage === null ) {
241- $ this ->currentPackage = $ this ->defaultPackage ;
242- }
238+ $ this ->currentPackage = $ attributes ['package ' ] ?? $ this ->defaultPackage ;
243239 } else {
244240 $ this ->currDB = $ this ->schema ->addDatabase ($ attributes );
245241 }
@@ -441,28 +437,27 @@ public function startElement($parser, string $tagName, array $attributes): void
441437 /**
442438 * @param string $tagName
443439 *
440+ * @throws \Propel\Generator\Exception\SchemaException
441+ *
444442 * @return void
445443 */
446444 protected function throwInvalidTagException (string $ tagName ): void
447445 {
448- $ this -> throwSchemaExceptionWithLocation ( ' Unexpected tag <%s> ' , $ tagName );
446+ throw new SchemaException ( " Unexpected tag $ tagName" );
449447 }
450448
451449 /**
452- * @param string $format
453- * @param mixed $args sprintf arguments
454- *
455450 * @throws \Propel\Generator\Exception\SchemaException
456451 *
457452 * @return void
458453 */
459- private function throwSchemaExceptionWithLocation ( string $ format , ... $ args ): void
454+ protected function throwParserError ( ): void
460455 {
461- $ format .= ' in %s ' ;
462- $ args [] = $ this -> getLocationDescription ( );
463- $ message = vsprintf ( $ format , $ args );
456+ $ errorCode = xml_get_error_code ( $ this -> parser ) ;
457+ $ errorMessage = xml_error_string ( $ errorCode );
458+ $ location = $ this -> getLocationDescription ( );
464459
465- throw new SchemaException ($ message );
460+ throw new SchemaException (" XML error: $ errorMessage at $ location " );
466461 }
467462
468463 /**
@@ -612,6 +607,8 @@ private function addAttributeToParameterListItem(array $attributes): void
612607 /**
613608 * Feeds the current parameter list to its parent and clears the collector.
614609 *
610+ * @throws \Propel\Generator\Exception\SchemaException
611+ *
615612 * @return void
616613 */
617614 private function finalizeParameterList (): void
@@ -620,7 +617,7 @@ private function finalizeParameterList(): void
620617 if ($ parentTag === 'behavior ' ) {
621618 $ this ->currBehavior ->addParameter ($ this ->currParameterListCollector );
622619 } else {
623- $ this -> throwSchemaExceptionWithLocation ( ' Cannot add parameter list to tag <%s> ' , $ parentTag );
620+ throw new SchemaException ( " Cannot add parameter list to tag ` $ parentTag` " );
624621 }
625622
626623 $ this ->currParameterListCollector = null ;
@@ -632,12 +629,14 @@ private function finalizeParameterList(): void
632629 * @param array $attributes
633630 * @param string $key
634631 *
632+ * @throws \Propel\Generator\Exception\SchemaException
633+ *
635634 * @return string the non-empty value
636635 */
637636 private function getExpectedValue (array $ attributes , string $ key ): string
638637 {
639638 if (empty ($ attributes [$ key ])) {
640- $ this -> throwSchemaExceptionWithLocation ( ' Parameter misses expected attribute "%s" ' , $ key );
639+ throw new SchemaException ( " Parameter misses expected attribute ` $ key` " );
641640 }
642641
643642 return $ attributes [$ key ];
0 commit comments