From 25106c8726a767bdb099bf43443e1212774024a4 Mon Sep 17 00:00:00 2001 From: Toon Verwerft Date: Fri, 17 May 2024 15:37:45 +0200 Subject: [PATCH] Make it possible for wsdl:import tags to directly import XSD items. --- src/Loader/StreamWrapperLoader.php | 4 +-- src/Xml/Configurator/FlattenWsdlImports.php | 29 +++++++++++++++++-- .../Configurator/FlattenWsdlImportsTest.php | 8 +++++ .../fixtures/flattening/import-multi-xsd.wsdl | 8 +++++ tests/fixtures/flattening/import-xsd.wsdl | 7 +++++ .../result/import-multi-xsd-result.wsdl | 22 ++++++++++++++ .../flattening/result/import-xsd-result.wsdl | 15 ++++++++++ 7 files changed, 89 insertions(+), 4 deletions(-) create mode 100644 tests/fixtures/flattening/import-multi-xsd.wsdl create mode 100644 tests/fixtures/flattening/import-xsd.wsdl create mode 100644 tests/fixtures/flattening/result/import-multi-xsd-result.wsdl create mode 100644 tests/fixtures/flattening/result/import-xsd-result.wsdl diff --git a/src/Loader/StreamWrapperLoader.php b/src/Loader/StreamWrapperLoader.php index dd90cd0..2facbc4 100644 --- a/src/Loader/StreamWrapperLoader.php +++ b/src/Loader/StreamWrapperLoader.php @@ -22,11 +22,11 @@ public function __construct($context = null) { $this->context = $context; } - + public function __invoke(string $location): string { try { - $content = file_get_contents( + $content = @file_get_contents( $location, context: is_resource($this->context) ? $this->context : null ); diff --git a/src/Xml/Configurator/FlattenWsdlImports.php b/src/Xml/Configurator/FlattenWsdlImports.php index 69a0b13..6646cc7 100644 --- a/src/Xml/Configurator/FlattenWsdlImports.php +++ b/src/Xml/Configurator/FlattenWsdlImports.php @@ -9,12 +9,14 @@ use Soap\Wsdl\Exception\UnloadableWsdlException; use Soap\Wsdl\Loader\Context\FlatteningContext; use Soap\Wsdl\Uri\IncludePathBuilder; +use Soap\Xml\Xmlns; use Soap\Xml\Xpath\WsdlPreset; use VeeWee\Xml\Dom\Configurator\Configurator; use VeeWee\Xml\Dom\Document; use VeeWee\Xml\Exception\RuntimeException; use function VeeWee\Xml\Dom\Locator\document_element; use function VeeWee\Xml\Dom\Locator\Node\children; +use function VeeWee\Xml\Dom\Manipulator\Node\append_external_node; use function VeeWee\Xml\Dom\Manipulator\Node\remove; use function VeeWee\Xml\Dom\Manipulator\Node\replace_by_external_nodes; @@ -66,11 +68,34 @@ private function importWsdlImportElement(DOMElement $import): void } $imported = Document::fromXmlString($result); - $definitions = $imported->map(document_element()); + + // A wsdl:import can be either a WSDL or an XSD file: + match ($imported->locateDocumentElement()->namespaceURI) { + Xmlns::xsd()->value() => $this->importXsdPart($import, $imported), + default => $this->importWsdlPart($import, $imported), + }; + } + + /** + * @throws RuntimeException + */ + private function importWsdlPart(DOMElement $importElement, Document $importedDocument): void + { + $definitions = $importedDocument->map(document_element()); replace_by_external_nodes( - $import, + $importElement, children($definitions) ); } + + /** + * @throws RuntimeException + */ + private function importXsdPart(DOMElement $importElement, Document $importedDocument): void + { + $types = $this->context->types(); + remove($importElement); + append_external_node($types, $importedDocument->locateDocumentElement()); + } } diff --git a/tests/Unit/Xml/Configurator/FlattenWsdlImportsTest.php b/tests/Unit/Xml/Configurator/FlattenWsdlImportsTest.php index a482ddb..3c7047f 100644 --- a/tests/Unit/Xml/Configurator/FlattenWsdlImportsTest.php +++ b/tests/Unit/Xml/Configurator/FlattenWsdlImportsTest.php @@ -46,5 +46,13 @@ public function provideTestCases() 'wsdl' => FIXTURE_DIR.'/flattening/multi-import.wsdl', 'expected' => Document::fromXmlFile(FIXTURE_DIR.'/flattening/result/multi-import-result.wsdl', comparable()), ]; + yield 'xsd-imports' => [ + 'wsdl' => FIXTURE_DIR.'/flattening/import-xsd.wsdl', + 'expected' => Document::fromXmlFile(FIXTURE_DIR.'/flattening/result/import-xsd-result.wsdl', comparable()), + ]; + yield 'multi-xsd-imports' => [ + 'wsdl' => FIXTURE_DIR.'/flattening/import-multi-xsd.wsdl', + 'expected' => Document::fromXmlFile(FIXTURE_DIR.'/flattening/result/import-multi-xsd-result.wsdl', comparable()), + ]; } } diff --git a/tests/fixtures/flattening/import-multi-xsd.wsdl b/tests/fixtures/flattening/import-multi-xsd.wsdl new file mode 100644 index 0000000..81be99a --- /dev/null +++ b/tests/fixtures/flattening/import-multi-xsd.wsdl @@ -0,0 +1,8 @@ + + + + + diff --git a/tests/fixtures/flattening/import-xsd.wsdl b/tests/fixtures/flattening/import-xsd.wsdl new file mode 100644 index 0000000..633ede7 --- /dev/null +++ b/tests/fixtures/flattening/import-xsd.wsdl @@ -0,0 +1,7 @@ + + + + diff --git a/tests/fixtures/flattening/result/import-multi-xsd-result.wsdl b/tests/fixtures/flattening/result/import-multi-xsd-result.wsdl new file mode 100644 index 0000000..59798af --- /dev/null +++ b/tests/fixtures/flattening/result/import-multi-xsd-result.wsdl @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + diff --git a/tests/fixtures/flattening/result/import-xsd-result.wsdl b/tests/fixtures/flattening/result/import-xsd-result.wsdl new file mode 100644 index 0000000..d03e341 --- /dev/null +++ b/tests/fixtures/flattening/result/import-xsd-result.wsdl @@ -0,0 +1,15 @@ + + + + + + + + + + + +