Skip to content

Commit

Permalink
Merge pull request #20 from veewee/wsdl-import-xsd
Browse files Browse the repository at this point in the history
Make it possible for wsdl:import tags to directly import XSD items.
  • Loading branch information
veewee authored May 17, 2024
2 parents 467e771 + 25106c8 commit 31adf06
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/Loader/StreamWrapperLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand Down
29 changes: 27 additions & 2 deletions src/Xml/Configurator/FlattenWsdlImports.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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());
}
}
8 changes: 8 additions & 0 deletions tests/Unit/Xml/Configurator/FlattenWsdlImportsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
];
}
}
8 changes: 8 additions & 0 deletions tests/fixtures/flattening/import-multi-xsd.wsdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0"?>
<definitions
xmlns="http://schemas.xmlsoap.org/wsdl/"
xmlns:tns="http://soapinterop.org/"
targetNamespace="http://soapinterop.org/">
<import namespace="http://soapinterop.org/store1" location="xsd/store1.xsd" />
<import namespace="http://soapinterop.org/store2" location="xsd/store2.xsd" />
</definitions>
7 changes: 7 additions & 0 deletions tests/fixtures/flattening/import-xsd.wsdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0"?>
<definitions
xmlns="http://schemas.xmlsoap.org/wsdl/"
xmlns:tns="http://soapinterop.org/"
targetNamespace="http://soapinterop.org/">
<import namespace="http://soapinterop.org/store1" location="xsd/store1.xsd" />
</definitions>
22 changes: 22 additions & 0 deletions tests/fixtures/flattening/result/import-multi-xsd-result.wsdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0"?>
<definitions
xmlns="http://schemas.xmlsoap.org/wsdl/"
xmlns:tns="http://soapinterop.org/"
targetNamespace="http://soapinterop.org/">
<types>
<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://soapinterop.org/store1">
<xsd:complexType name="Store">
<xsd:sequence>
<element minOccurs="1" maxOccurs="1" name="Attribute1" type="string"/>
</xsd:sequence>
</xsd:complexType>
</schema>
<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://soapinterop.org/store2">
<xsd:complexType name="Store">
<xsd:sequence>
<element minOccurs="1" maxOccurs="1" name="Attribute2" type="string"/>
</xsd:sequence>
</xsd:complexType>
</schema>
</types>
</definitions>
15 changes: 15 additions & 0 deletions tests/fixtures/flattening/result/import-xsd-result.wsdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0"?>
<definitions
xmlns="http://schemas.xmlsoap.org/wsdl/"
xmlns:tns="http://soapinterop.org/"
targetNamespace="http://soapinterop.org/">
<types>
<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://soapinterop.org/store1">
<xsd:complexType name="Store">
<xsd:sequence>
<element minOccurs="1" maxOccurs="1" name="Attribute1" type="string"/>
</xsd:sequence>
</xsd:complexType>
</schema>
</types>
</definitions>

0 comments on commit 31adf06

Please sign in to comment.