Skip to content

added WSDL document-wrapped support and more... #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
language: php

php:
- 5.3
- 5.4
- 5.5

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
}
],
"require": {
"php": ">=5.3.0",
"php": ">=5.4.0",
"ext-soap": "*",
"ext-curl": "*",
"ass/xmlsecurity": "~1.0",
Expand Down
15 changes: 11 additions & 4 deletions src/BeSimple/SoapBundle/Controller/SoapWebServiceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,17 @@ public function exceptionAction(Request $request, FlattenException $exception, D
public function __call($method, $arguments)
{
if ($this->serviceBinder->isServiceMethod($method)) {
// @TODO Add all SoapHeaders in SoapRequest
foreach ($this->headers as $name => $value) {
if ($this->serviceBinder->isServiceHeader($method, $name)) {
$this->soapRequest->getSoapHeaders()->add($this->serviceBinder->processServiceHeader($method, $name, $value));
if (!empty($this->headers)) {
$firstHeaderName = array_keys($this->headers)[0];
if ((count($this->headers) === 1) && (substr($firstHeaderName, -6) === 'Header')) {
// headers are wrapped and returned as stdClass!
$this->headers = (array) $this->headers[$firstHeaderName];
}
// @TODO Add all SoapHeaders in SoapRequest
foreach ($this->headers as $name => $value) {
if ($this->serviceBinder->isServiceHeader($method, $name)) {
$this->soapRequest->getSoapHeaders()->add($this->serviceBinder->processServiceHeader($method, $name, $value));
}
}
}
$this->headers = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

if (!defined('SOAP_LITERAL_WRAPPED')) {
define('SOAP_LITERAL_WRAPPED', 3);
}
/**
* BeSimpleSoapExtension.
*
Expand All @@ -35,6 +38,15 @@ class BeSimpleSoapExtension extends Extension
'rpc-literal' => 'rpcliteral',
'document-wrapped' => 'documentwrapped',
);
private $styleMap = [
'rpc' => \SOAP_RPC,
'document' => \SOAP_DOCUMENT,
];
private $useMap = [
'encoded' => \SOAP_ENCODED,
'literal' => \SOAP_LITERAL,
'wrapped' => \SOAP_LITERAL_WRAPPED,
];

public function load(array $configs, ContainerBuilder $container)
{
Expand Down Expand Up @@ -152,7 +164,10 @@ private function createClient($client, ContainerBuilder $container)

private function createWebServiceContext(array $config, ContainerBuilder $container)
{
$bindingSuffix = $this->bindingConfigToServiceSuffixMap[$config['binding']];
$bindingSuffix = $this->bindingConfigToServiceSuffixMap[$config['binding']];
list($style, $use) = explode('-', $config['binding']);
$config['style'] = $this->styleMap[$style];
$config['use'] = $this->useMap[$use];
unset($config['binding']);

$contextId = 'besimple.soap.context.'.$config['name'];
Expand Down
11 changes: 9 additions & 2 deletions src/BeSimple/SoapBundle/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,15 @@ private function addServicesSection(ArrayNodeDefinition $rootNode)
->scalarNode('binding')
->defaultValue('document-wrapped')
->validate()
->ifNotInArray(array('rpc-literal', 'document-wrapped'))
->thenInvalid("Service binding style has to be either 'rpc-literal' or 'document-wrapped'")
->ifNotInArray([ 'rpc-literal', 'document-wrapped' /*, 'rpc-encoded', , 'document-literal'*/ ])
->thenInvalid("Service binding style has to be either 'rpc-literal' or 'document-wrapped'!"/* or 'rpc-encoded' or 'document-literal' */)
->end()
->end()
->scalarNode('version')
->defaultValue(2)
->validate()
->ifNotInArray([ 1, 2 ])
->thenInvalid("Service protocol version has to be either 1 or 2")
->end()
->end()
->scalarNode('cache_type')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace BeSimple\SoapBundle\ServiceBinding;

use BeSimple\SoapBundle\ServiceDefinition\Method;
use BeSimple\SoapCommon\Definition\Type\TypeRepository;
/**
* Description of DocumentLiteralWrappedRequestHeaderBinder
*
* @author Michal Mičko <[email protected]>
*/
class DocumentLiteralWrappedRequestHeaderMessageBinder extends DocumentLiteralWrappedRequestMessageBinder
{
private $header;

public function setHeader($header)
{
$this->header = $header;
}

public function processMessage(Method $messageDefinition, $message, TypeRepository $typeRepository)
{
$this->typeRepository = $typeRepository;
$headerDefinition = $messageDefinition->getHeaders()->get($this->header);

return $this->processType($headerDefinition->getType(), $message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,108 @@
namespace BeSimple\SoapBundle\ServiceBinding;

use BeSimple\SoapBundle\ServiceDefinition\Method;
use BeSimple\SoapCommon\Definition\Type\ArrayOfType;
use BeSimple\SoapCommon\Definition\Type\ComplexType;
use BeSimple\SoapCommon\Definition\Type\TypeRepository;
use BeSimple\SoapCommon\Util\MessageBinder;

/**
* @author Christian Kerl <[email protected]>
*/
class DocumentLiteralWrappedRequestMessageBinder implements MessageBinderInterface
{
public function processMessage(Method $messageDefinition, $message)
protected $typeRepository;

public function processMessage(Method $messageDefinition, $message, TypeRepository $typeRepository)
{
$this->typeRepository = $typeRepository;

if(count($message) > 1) {
throw new \InvalidArgumentException();
}

$result = array();
$message = $message[0];

foreach($messageDefinition->getArguments() as $argument) {
$result[$argument->getName()] = $message->{$argument->getName()};
foreach($messageDefinition->getInput()->all() as $argument) {
$result[$argument->getName()] = $this->processType($argument->getType(), $message->{$argument->getName()});
}

return $result;
}

protected function processType($phpType, $message)
{
$isArray = false;

$type = $this->typeRepository->getType($phpType);
if ($type instanceof ArrayOfType) {
$isArray = true;
$array = array();

$type = $this->typeRepository->getType($type->get('item')->getType());
}

// @TODO Fix array reference
if ($type instanceof ComplexType) {
$phpType = $type->getPhpType();

if ($isArray) {
if (isset($message->item)) {
foreach ($message->item as $complexType) {
$array[] = $this->checkComplexType($phpType, $complexType);
}

// See https://github.com/BeSimple/BeSimpleSoapBundle/issues/29
if (in_array('BeSimple\SoapCommon\Type\AbstractKeyValue', class_parents($phpType))) {
$assocArray = array();
foreach ($array as $keyValue) {
$assocArray[$keyValue->getKey()] = $keyValue->getValue();
}

$array = $assocArray;
}
}

$message = $array;
} else {
$message = $this->checkComplexType($phpType, $message);
}
} elseif ($isArray) {
if (isset($message->item)) {
$message = $message->item;
} else {
$message = $array;
}
}

return $message;
}

protected function checkComplexType($phpType, $message)
{
$hash = spl_object_hash($message);
if (isset($this->messageRefs[$hash])) {
return $this->messageRefs[$hash];
}

$this->messageRefs[$hash] = $message;

$messageBinder = new MessageBinder($message);
foreach ($this->typeRepository->getType($phpType)->all() as $type) {
$property = $type->getName();
$value = $messageBinder->readProperty($property);

if (null !== $value) {
$value = $this->processType($type->getType(), $value);

$messageBinder->writeProperty($property, $value);
} elseif (!$type->isNillable()) {
// @TODO use xmlType instead of phpType
throw new \SoapFault('SOAP_ERROR_COMPLEX_TYPE', sprintf('"%s:%s" cannot be null.', ucfirst($phpType), $type->getName()));
}
}

return $message;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,100 @@
namespace BeSimple\SoapBundle\ServiceBinding;

use BeSimple\SoapBundle\ServiceDefinition\Method;
use BeSimple\SoapCommon\Definition\Type\ArrayOfType;
use BeSimple\SoapCommon\Definition\Type\ComplexType;
use BeSimple\SoapCommon\Definition\Type\TypeRepository;
use BeSimple\SoapCommon\Util\MessageBinder;

/**
* @author Christian Kerl <[email protected]>
*/
class DocumentLiteralWrappedResponseMessageBinder implements MessageBinderInterface
{
public function processMessage(Method $messageDefinition, $message)
public function processMessage(Method $messageDefinition, $message, TypeRepository $typeRepository)
{
$this->typeRepository = $typeRepository;

$result = new \stdClass();
$result->{$messageDefinition->getName().'Result'} = $message;
//$result->{$messageDefinition->getName().'Result'} = $message;
foreach ($messageDefinition->getOutput()->all() as $name => $part) {
//$result->{$name} = $message;
$result->{$name} = $this->processType($part->getType(), $message);
break; // only one iteration
}

return $result;
}

private function processType($phpType, $message)
{
$isArray = false;

$type = $this->typeRepository->getType($phpType);
if ($type instanceof ArrayOfType) {
$isArray = true;

$type = $this->typeRepository->getType($type->get('item')->getType());
}

if ($type instanceof ComplexType) {
$phpType = $type->getPhpType();

if ($isArray) {
$array = array();

// See https://github.com/BeSimple/BeSimpleSoapBundle/issues/29
if (is_array($message) && in_array('BeSimple\SoapCommon\Type\AbstractKeyValue', class_parents($phpType))) {
$keyValue = array();
foreach ($message as $key => $value) {
$keyValue[] = new $phpType($key, $value);
}

$message = $keyValue;
}

foreach ($message as $complexType) {
$array[] = $this->checkComplexType($phpType, $complexType);
}

$message = $array;
} else {
$message = $this->checkComplexType($phpType, $message);
}
}

return $message;
}

private function checkComplexType($phpType, $message)
{
$hash = spl_object_hash($message);
if (isset($this->messageRefs[$hash])) {
return $this->messageRefs[$hash];
}

$this->messageRefs[$hash] = $message;

if (!$message instanceof $phpType) {
throw new \InvalidArgumentException(sprintf('The instance class must be "%s", "%s" given.', $phpType, get_class($message)));
}

$messageBinder = new MessageBinder($message);
foreach ($this->typeRepository->getType($phpType)->all() as $type) {
$property = $type->getName();
$value = $messageBinder->readProperty($property);

if (null !== $value) {
$value = $this->processType($type->getType(), $value);

$messageBinder->writeProperty($property, $value);
}

if (!$type->isNillable() && null === $value) {
throw new \InvalidArgumentException(sprintf('"%s::%s" cannot be null.', $phpType, $type->getName()));
}
}

return $message;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public function processMessage(Method $messageDefinition, $message, TypeReposito
{
$this->typeRepository = $typeRepository;

return $this->processType($messageDefinition->getOutput()->get('return')->getType(), $message);
//return $this->processType($messageDefinition->getOutput()->get('return')->getType(), $message);
return $this->processType(current($messageDefinition->getOutput()->all())->getType(), $message);
}

private function processType($phpType, $message)
Expand Down
11 changes: 11 additions & 0 deletions src/BeSimple/SoapBundle/ServiceDefinition/Annotation/Param.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Param extends Configuration implements TypedElementInterface
private $value;
private $phpType;
private $xmlType;
private $isNillable = false;

public function getValue()
{
Expand All @@ -34,6 +35,11 @@ public function getXmlType()
return $this->xmlType;
}

public function isNillable()
{
return $this->isNillable;
}

public function setValue($value)
{
$this->value = $value;
Expand All @@ -49,6 +55,11 @@ public function setXmlType($xmlType)
$this->xmlType = $xmlType;
}

public function setNillable($isNillable)
{
$this->isNillable = (bool) $isNillable;
}

public function getAliasName()
{
return 'param';
Expand Down
Loading