Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions src/Fault/Encoder/Soap11FaultEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@

use Soap\Encoding\Fault\Soap11Fault;
use Soap\Encoding\Restriction\WhitespaceRestriction;
use Soap\Encoding\Xml\Writer\InMemoryXmlWriter;
use Soap\Xml\Xmlns;
use VeeWee\Reflecta\Iso\Iso;
use VeeWee\Xml\Dom\Document;
use VeeWee\Xml\Writer\Writer;
use function Psl\invariant;
use function VeeWee\Xml\Dom\Xpath\Configurator\namespaces;
use function VeeWee\Xml\Writer\Builder\children;
use function VeeWee\Xml\Writer\Builder\element;
use function VeeWee\Xml\Writer\Builder\namespaced_element;
use function VeeWee\Xml\Writer\Builder\raw;
use function VeeWee\Xml\Writer\Builder\value;
use function VeeWee\Xml\Writer\Mapper\memory_output;

/**
* @implements SoapFaultEncoder<Soap11Fault>
Expand All @@ -41,9 +40,8 @@ private function to(Soap11Fault $fault): string
{
$envNamespace = Xmlns::soap11Envelope()->value();

/** @var non-empty-string */
return Writer::inMemory()
->write(children([
return (new InMemoryXmlWriter())(
children([
namespaced_element(
$envNamespace,
'env',
Expand All @@ -70,8 +68,8 @@ private function to(Soap11Fault $fault): string
...($fault->detail !== null ? [raw($fault->detail)] : []),
])
)
]))
->map(memory_output());
])
);
}

/**
Expand Down
12 changes: 5 additions & 7 deletions src/Fault/Encoder/Soap12FaultEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@

use Soap\Encoding\Fault\Soap12Fault;
use Soap\Encoding\Restriction\WhitespaceRestriction;
use Soap\Encoding\Xml\Writer\InMemoryXmlWriter;
use VeeWee\Reflecta\Iso\Iso;
use VeeWee\Xml\Dom\Document;
use VeeWee\Xml\Writer\Writer;
use function Psl\invariant;
use function VeeWee\Xml\Dom\Xpath\Configurator\namespaces;
use function VeeWee\Xml\Writer\Builder\children;
use function VeeWee\Xml\Writer\Builder\namespaced_element;
use function VeeWee\Xml\Writer\Builder\prefixed_element;
use function VeeWee\Xml\Writer\Builder\raw;
use function VeeWee\Xml\Writer\Builder\value;
use function VeeWee\Xml\Writer\Mapper\memory_output;

/**
* @implements SoapFaultEncoder<Soap12Fault>
Expand All @@ -40,9 +39,8 @@ public function iso(): Iso
*/
private function to(Soap12Fault $fault): string
{
/** @var non-empty-string */
return Writer::inMemory()
->write(children([
return (new InMemoryXmlWriter())(
children([
namespaced_element(
self::ENV_NAMESPACE,
'env',
Expand Down Expand Up @@ -113,8 +111,8 @@ private function to(Soap12Fault $fault): string
...($fault->detail !== null ? [raw($fault->detail)] : []),
])
)
]))
->map(memory_output());
])
);
}

/**
Expand Down
62 changes: 62 additions & 0 deletions src/Xml/Writer/InMemoryXmlWriter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
declare(strict_types=1);

namespace Soap\Encoding\Xml\Writer;

use Generator;
use Throwable;
use VeeWee\Xml\Exception\RuntimeException;
use XMLWriter;
use function count;
use function VeeWee\Xml\ErrorHandling\issue_collection_from_xml_errors;

/**
* Runs once per encoded element, so it drives XMLWriter directly: cheaper than VeeWee\Xml\Writer\Writer,
* while throwing the same exceptions as Writer::write() with disallow_issues().
*/
final class InMemoryXmlWriter
{
/**
* @param callable(XMLWriter): Generator<bool> $writer
*
* @throws RuntimeException
*
* @return non-empty-string
*/
public function __invoke(callable $writer): string
{
$xmlWriter = XMLWriter::toMemory();

$previousErrorReporting = libxml_use_internal_errors(true);
libxml_clear_errors();

$failure = null;
try {
foreach ($writer($xmlWriter) as $written) {
if ($written === false) {
throw RuntimeException::withMessage('Could not write the provided XML to the stream.');
}
}
} catch (Throwable $exception) {
$failure = $exception;
}

$errors = libxml_get_errors();
libxml_clear_errors();
libxml_use_internal_errors($previousErrorReporting);

if ($failure !== null) {
throw RuntimeException::combineExceptionWithIssues($failure, issue_collection_from_xml_errors($errors));
}

if ($errors) {
$issues = issue_collection_from_xml_errors($errors);
if (count($issues)) {
throw RuntimeException::fromIssues('XML issues detected: ', $issues);
}
}

/** @psalm-var non-empty-string */
return $xmlWriter->outputMemory();
}
}
66 changes: 31 additions & 35 deletions src/Xml/Writer/SoapEnvelopeWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@
use Soap\WsdlReader\Model\Definitions\SoapVersion;
use Soap\Xml\Xmlns;
use VeeWee\Xml\Exception\RuntimeException as XmlRuntimeException;
use VeeWee\Xml\Writer\Writer;
use XMLWriter;
use function Psl\Vec\filter_nulls;
use function VeeWee\Xml\Writer\Builder\children;
use function VeeWee\Xml\Writer\Builder\namespace_attribute;
use function VeeWee\Xml\Writer\Builder\namespaced_attribute;
use function VeeWee\Xml\Writer\Builder\namespaced_element;
use function VeeWee\Xml\Writer\Mapper\memory_output;

final class SoapEnvelopeWriter
{
Expand All @@ -45,45 +43,43 @@ public function __invoke(): string
};

try {
return Writer::inMemory()
->write(
return (new InMemoryXmlWriter())(
namespaced_element(
$envelopeNamespace,
'SOAP-ENV',
'Envelope',
namespaced_element(
$envelopeNamespace,
'SOAP-ENV',
'Envelope',
namespaced_element(
$envelopeNamespace,
'SOAP-ENV',
'Body',
children(
filter_nulls([
// In SOAP 1.2 the position of the encoding attributes is limited:
// See: https://www.w3.org/TR/soap12-part1/#soapencattr
// For SOAP 1.1 it can be everywhere:
// See: https://www.w3.org/TR/2000/NOTE-SOAP-20000508/#_Toc478383495
$this->encodingStyle
->filter(fn (): bool => $this->bindingUse === BindingUse::ENCODED)
->map(
static fn (EncodingStyle $encodingStyle) => children([
namespace_attribute(
$encodingStyle->value,
'SOAP-ENC'
),
namespaced_attribute(
$envelopeNamespace,
'SOAP-ENV',
'encodingStyle',
$encodingStyle->value
)
])
)->unwrapOr(null),
$this->children
])
)
'Body',
children(
filter_nulls([
// In SOAP 1.2 the position of the encoding attributes is limited:
// See: https://www.w3.org/TR/soap12-part1/#soapencattr
// For SOAP 1.1 it can be everywhere:
// See: https://www.w3.org/TR/2000/NOTE-SOAP-20000508/#_Toc478383495
$this->encodingStyle
->filter(fn (): bool => $this->bindingUse === BindingUse::ENCODED)
->map(
static fn (EncodingStyle $encodingStyle) => children([
namespace_attribute(
$encodingStyle->value,
'SOAP-ENC'
),
namespaced_attribute(
$envelopeNamespace,
'SOAP-ENV',
'encodingStyle',
$encodingStyle->value
)
])
)->unwrapOr(null),
$this->children
])
)
)
)
->map(memory_output());
);
} catch (XmlRuntimeException $e) {
throw ($e->getPrevious() instanceof EncodingExceptionInterface) ? $e->getPrevious() : $e;
}
Expand Down
10 changes: 4 additions & 6 deletions src/Xml/Writer/XsdTypeXmlElementWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,20 @@

use Generator;
use Soap\Encoding\Encoder\Context;
use VeeWee\Xml\Writer\Writer;
use VeeWee\Xml\Exception\RuntimeException;
use XMLWriter;
use function VeeWee\Xml\Writer\Mapper\memory_output;

final class XsdTypeXmlElementWriter
{
/**
* @param callable(XMLWriter): Generator<bool> $children
*
* @throws RuntimeException
*
* @return non-empty-string
*/
public function __invoke(Context $context, callable $children): string
{
/** @psalm-var non-empty-string */
return Writer::inMemory()
->write(new ElementBuilder($context, $children(...)))
->map(memory_output());
return (new InMemoryXmlWriter())(new ElementBuilder($context, $children(...)));
}
}
15 changes: 15 additions & 0 deletions tests/Unit/Fault/Encoder/Soap11FaultEncoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,21 @@
#[CoversClass(Soap11Fault::class)]
final class Soap11FaultEncoderTest extends AbstractFaultEncoderTests
{
public function test_it_writes_the_exact_fault_bytes(): void
{
$actual = (new Soap11FaultEncoder())->iso()->to(new Soap11Fault(
faultCode: 'env:Server',
faultString: 'a < b',
faultActor: 'uri:actor',
detail: '<detail><element>value</element></detail>',
));

static::assertSame(
'<env:Fault xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"><faultcode>env:Server</faultcode><faultstring>a &lt; b</faultstring><faultactor>uri:actor</faultactor><detail><element>value</element></detail></env:Fault>',
$actual
);
}

public static function provideIsomorphicCases(): iterable
{
$baseConfig = [
Expand Down
17 changes: 17 additions & 0 deletions tests/Unit/Fault/Encoder/Soap12FaultEncoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,23 @@
#[CoversClass(Soap12Fault::class)]
final class Soap12FaultEncoderTest extends AbstractFaultEncoderTests
{
public function test_it_writes_the_exact_fault_bytes(): void
{
$actual = (new Soap12FaultEncoder())->iso()->to(new Soap12Fault(
code: 'env:Sender',
reason: 'a < b',
subCode: 'm:Invalid',
node: 'uri:node',
role: 'uri:role',
detail: '<env:Detail><element>value</element></env:Detail>',
));

static::assertSame(
'<env:Fault xmlns:env="http://www.w3.org/2003/05/soap-envelope"><env:Code><env:Value>env:Sender</env:Value><env:Subcode><env:Value>m:Invalid</env:Value></env:Subcode></env:Code><env:Reason><env:Text>a &lt; b</env:Text></env:Reason><env:Node>uri:node</env:Node><env:Role>uri:role</env:Role><env:Detail><element>value</element></env:Detail></env:Fault>',
$actual
);
}

public static function provideIsomorphicCases(): iterable
{
$baseConfig = [
Expand Down
Loading
Loading