-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce message encoders for building soap servers
- Loading branch information
Showing
15 changed files
with
1,400 additions
and
46 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php declare(strict_types=1); | ||
|
||
require_once \dirname(__DIR__) . '/vendor/autoload.php'; | ||
|
||
use Soap\Encoding\Encoder\Method\MethodContext; | ||
use Soap\Encoding\Encoder\Method\RequestEncoder; | ||
use Soap\Encoding\Encoder\Method\ResponseEncoder; | ||
use Soap\Encoding\EncoderRegistry; | ||
use Soap\Wsdl\Loader\StreamWrapperLoader; | ||
use Soap\WsdlReader\Locator\ServiceSelectionCriteria; | ||
use Soap\WsdlReader\Metadata\Wsdl1MetadataProvider; | ||
use Soap\WsdlReader\Wsdl1Reader; | ||
|
||
$wsdlLocation = __DIR__ . '/calc.wsdl'; | ||
$wsdl = (new Wsdl1Reader(new StreamWrapperLoader()))($wsdlLocation); | ||
$registry ??= EncoderRegistry::default() | ||
->addClassMap('http://tempuri.org/', 'Add', Add::class) | ||
->addClassMap('http://tempuri.org/', 'AddResponse', AddResponse::class); | ||
$metadataProvider = new Wsdl1MetadataProvider($wsdl, ServiceSelectionCriteria::defaults()); | ||
$metadata = $metadataProvider->getMetadata(); | ||
|
||
$soapAction = 'Add'; | ||
$methodContext = new MethodContext( | ||
$metadata->getMethods()->fetchByName($soapAction), | ||
$metadata, | ||
$registry, | ||
$wsdl->namespaces, | ||
); | ||
|
||
$request = <<<EOXML | ||
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> | ||
<soap:Body> | ||
<Add xmlns="http://tempuri.org/"> | ||
<a>1</a> | ||
<b>2</b> | ||
</Add> | ||
</soap:Body> | ||
</soap:Envelope> | ||
EOXML; | ||
|
||
$requestEncoder = new RequestEncoder(); | ||
$requestIso = $requestEncoder->iso($methodContext); | ||
$arguments = $requestIso->from($request); | ||
|
||
var_dump($arguments); | ||
|
||
final class Add | ||
{ | ||
public int $a; | ||
public int $b; | ||
} | ||
final class AddResponse | ||
{ | ||
public function __construct( | ||
public int $AddResult, | ||
) { | ||
} | ||
} | ||
|
||
$myCalculator = new class() { | ||
public function Add(Add $add): AddResponse | ||
{ | ||
return new AddResponse($add->a + $add->b); | ||
} | ||
}; | ||
|
||
|
||
$result = $myCalculator->{$soapAction}(...$arguments); | ||
|
||
var_dump($result); | ||
|
||
|
||
$responseEncoder = new ResponseEncoder(); | ||
$responseIso = $responseEncoder->iso($methodContext); | ||
$response = $responseIso->to([$result]); | ||
|
||
var_dump($response); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php declare(strict_types=1); | ||
|
||
use GoetasWebservices\XML\XSDReader\SchemaReader; | ||
use Soap\Encoding\Encoder\Context; | ||
use Soap\Encoding\EncoderRegistry; | ||
use Soap\Engine\Metadata\Collection\MethodCollection; | ||
use Soap\Engine\Metadata\InMemoryMetadata; | ||
use Soap\WsdlReader\Metadata\Converter\SchemaToTypesConverter; | ||
use Soap\WsdlReader\Metadata\Converter\Types\TypesConverterContext; | ||
use Soap\WsdlReader\Parser\Definitions\NamespacesParser; | ||
use VeeWee\Xml\Dom\Document; | ||
|
||
require_once \dirname(__DIR__) . '/vendor/autoload.php'; | ||
|
||
// Load the XSD with the goetas-webservices/xsd-reader package and transform it to a metadata object: | ||
$xsd = Document::fromXmlFile($file = __DIR__.'/calc.xsd'); | ||
$reader = new SchemaReader(); | ||
$schema = $reader->readNode($xsd->locateDocumentElement(), $file); | ||
$namespaces = NamespacesParser::tryParse($xsd); | ||
$types = (new SchemaToTypesConverter())( | ||
$schema, | ||
TypesConverterContext::default($namespaces) | ||
); | ||
$metadata = new InMemoryMetadata($types, new MethodCollection()); | ||
|
||
// Create an encoder for the Add type context: | ||
$registry = EncoderRegistry::default(); | ||
$encoder = $registry->detectEncoderForContext( | ||
$context = new Context( | ||
$types->fetchFirstByName('Add')->getXsdType(), | ||
$metadata, | ||
$registry, | ||
$namespaces, | ||
) | ||
); | ||
|
||
// Decode + Encode the Add type: | ||
var_dump($data = $encoder->iso($context)->from( | ||
<<<EOXML | ||
<Add xmlns="http://tempuri.org/"> | ||
<a>1</a> | ||
<b>2</b> | ||
</Add> | ||
EOXML | ||
)); | ||
|
||
var_dump($encoder->iso($context)->to($data)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<s:schema elementFormDefault="qualified" xmlns:tns="http://tempuri.org/" targetNamespace="http://tempuri.org/" | ||
xmlns:s="http://www.w3.org/2001/XMLSchema"> | ||
<s:element name="Add"> | ||
<s:complexType> | ||
<s:sequence> | ||
<s:element minOccurs="1" maxOccurs="1" name="a" type="s:int"/> | ||
<s:element minOccurs="1" maxOccurs="1" name="b" type="s:int"/> | ||
</s:sequence> | ||
</s:complexType> | ||
</s:element> | ||
<s:element name="AddResponse"> | ||
<s:complexType> | ||
<s:sequence> | ||
<s:element minOccurs="1" maxOccurs="1" name="AddResult" type="s:int"/> | ||
</s:sequence> | ||
</s:complexType> | ||
</s:element> | ||
<s:element name="Subtract"> | ||
<s:complexType> | ||
<s:sequence> | ||
<s:element minOccurs="1" maxOccurs="1" name="a" type="s:int"/> | ||
<s:element minOccurs="1" maxOccurs="1" name="b" type="s:int"/> | ||
</s:sequence> | ||
</s:complexType> | ||
</s:element> | ||
<s:element name="SubtractResponse"> | ||
<s:complexType> | ||
<s:sequence> | ||
<s:element minOccurs="1" maxOccurs="1" name="SubtractResult" type="s:int"/> | ||
</s:sequence> | ||
</s:complexType> | ||
</s:element> | ||
</s:schema> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Soap\Encoding\Encoder\Method; | ||
|
||
use Soap\Encoding\Encoder\Context; | ||
use Soap\Encoding\EncoderRegistry; | ||
use Soap\Engine\Metadata\Metadata; | ||
use Soap\Engine\Metadata\Model\Method; | ||
use Soap\Engine\Metadata\Model\XsdType; | ||
use Soap\WsdlReader\Model\Definitions\BindingUse; | ||
use Soap\WsdlReader\Model\Definitions\Namespaces; | ||
|
||
final class MethodContext | ||
{ | ||
public function __construct( | ||
public readonly Method $method, | ||
public readonly Metadata $metadata, | ||
public readonly EncoderRegistry $registry, | ||
public readonly Namespaces $namespaces, | ||
public readonly BindingUse $bindingUse = BindingUse::LITERAL, | ||
) { | ||
} | ||
|
||
public function createXmlEncoderContextForType(XsdType $type): Context | ||
{ | ||
return new Context( | ||
$type, | ||
$this->metadata, | ||
$this->registry, | ||
$this->namespaces, | ||
$this->bindingUse | ||
); | ||
} | ||
|
||
public function withBindingUse(BindingUse $bindingUse): self | ||
{ | ||
return new self( | ||
$this->method, | ||
$this->metadata, | ||
$this->registry, | ||
$this->namespaces, | ||
$bindingUse | ||
); | ||
} | ||
} |
Oops, something went wrong.