Used the object hash in RpcLiteralRequestMessageBinder to return a same instance of an object

This commit is contained in:
Francis Besset 2011-07-27 22:53:33 +02:00
parent 2994129b8d
commit 8cd07acc11
2 changed files with 60 additions and 24 deletions

View File

@ -20,29 +20,25 @@ use BeSimple\SoapBundle\ServiceDefinition\Strategy\PropertyComplexType;
*/ */
class RpcLiteralRequestMessageBinder implements MessageBinderInterface class RpcLiteralRequestMessageBinder implements MessageBinderInterface
{ {
private $definitionComplexTypes; private $messageRefs = array();
public function processMessage(Method $messageDefinition, $message, array $definitionComplexTypes = array()) public function processMessage(Method $messageDefinition, $message, array $definitionComplexTypes = array())
{ {
$this->definitionComplexTypes = $definitionComplexTypes;
$result = array(); $result = array();
$i = 0; $i = 0;
foreach($messageDefinition->getArguments() as $argument) { foreach($messageDefinition->getArguments() as $argument) {
if (isset($message[$i])) { if (isset($message[$i])) {
$result[$argument->getName()] = $this->processType($argument->getType()->getPhpType(), $message[$i]); $result[$argument->getName()] = $this->processType($argument->getType()->getPhpType(), $message[$i], $definitionComplexTypes);
} }
$i++; $i++;
} }
$this->definitionComplexTypes = array();
return $result; return $result;
} }
private function processType($phpType, $message) private function processType($phpType, $message, array $definitionComplexTypes)
{ {
if (preg_match('/^([^\[]+)\[\]$/', $phpType, $match)) { if (preg_match('/^([^\[]+)\[\]$/', $phpType, $match)) {
$isArray = true; $isArray = true;
@ -52,17 +48,17 @@ class RpcLiteralRequestMessageBinder implements MessageBinderInterface
$type = $phpType; $type = $phpType;
} }
if (isset($this->definitionComplexTypes[$type])) { if (isset($definitionComplexTypes[$type])) {
if ($isArray) { if ($isArray) {
$array = array(); $array = array();
foreach ($message->item as $complexType) { foreach ($message->item as $complexType) {
$array[] = $this->getInstanceOfType($type, $complexType); $array[] = $this->getInstanceOfType($type, $complexType, $definitionComplexTypes);
} }
$message = $array; $message = $array;
} else { } else {
$message = $this->getInstanceOfType($type, $message); $message = $this->getInstanceOfType($type, $message, $definitionComplexTypes);
} }
} elseif ($isArray) { } elseif ($isArray) {
$message = $message->item; $message = $message->item;
@ -71,12 +67,18 @@ class RpcLiteralRequestMessageBinder implements MessageBinderInterface
return $message; return $message;
} }
private function getInstanceOfType($phpType, $message) private function getInstanceOfType($phpType, $message, array $definitionComplexTypes)
{ {
$instanceType = new $phpType(); $hash = spl_object_hash($message);
if (isset($this->messageRefs[$hash])) {
return $this->messageRefs[$hash];
}
foreach ($this->definitionComplexTypes[$phpType] as $type) { $this->messageRefs[$hash] =
$value = $this->processType($type->getValue(), $message->{$type->getName()}); $instanceType = new $phpType();
foreach ($definitionComplexTypes[$phpType] as $type) {
$value = $this->processType($type->getValue(), $message->{$type->getName()}, $definitionComplexTypes);
if ($type instanceof PropertyComplexType) { if ($type instanceof PropertyComplexType) {
$instanceType->{$type->getOriginalName()} = $value; $instanceType->{$type->getOriginalName()} = $value;

View File

@ -38,18 +38,17 @@ class RpcLiteralRequestMessageBinderTest extends \PHPUnit_Framework_TestCase
public function testProcessMessageWithComplexType() public function testProcessMessageWithComplexType()
{ {
$definitionComplexTypes = $this->getDefinitionComplexTypes();
$attributes = new \stdClass(); $attributes = new \stdClass();
$attributes->foo = 'bar'; $attributes->foo = 'bar';
$attributes->bar = 10; $attributes->bar = 10;
$messageBinder = new RpcLiteralRequestMessageBinder(); $messageBinder = new RpcLiteralRequestMessageBinder();
$result = $messageBinder->processMessage( $result = $messageBinder->processMessage(
new Method('complextype_argument', null, array( new Method('complextype_argument', null, array(
new Argument('attributes', new Type('\BeSimple\SoapBundle\Tests\ServiceBinding\fixtures\Attributes')), new Argument('attributes', new Type('\BeSimple\SoapBundle\Tests\ServiceBinding\fixtures\Attributes')),
)), )),
array($attributes), array($attributes),
$definitionComplexTypes $this->getDefinitionComplexTypes()
); );
$this->assertInstanceOf('BeSimple\SoapBundle\Tests\ServiceBinding\fixtures\Attributes', $result['attributes']); $this->assertInstanceOf('BeSimple\SoapBundle\Tests\ServiceBinding\fixtures\Attributes', $result['attributes']);
@ -62,15 +61,17 @@ class RpcLiteralRequestMessageBinderTest extends \PHPUnit_Framework_TestCase
$attributes2 = new \stdClass(); $attributes2 = new \stdClass();
$attributes2->foo = 'barfoo'; $attributes2->foo = 'barfoo';
$attributes2->bar = 12; $attributes2->bar = 12;
$message = new \stdClass(); $message = new \stdClass();
$message->item = array($attributes1, $attributes2); $message->item = array($attributes1, $attributes2);
$messageBinder = new RpcLiteralRequestMessageBinder(); $messageBinder = new RpcLiteralRequestMessageBinder();
$result = $messageBinder->processMessage( $result = $messageBinder->processMessage(
new Method('complextype_argument', null, array( new Method('complextype_argument', null, array(
new Argument('attributes', new Type('\BeSimple\SoapBundle\Tests\ServiceBinding\fixtures\Attributes[]')), new Argument('attributes', new Type('\BeSimple\SoapBundle\Tests\ServiceBinding\fixtures\Attributes[]')),
)), )),
array($message), array($message),
$definitionComplexTypes $this->getDefinitionComplexTypes()
); );
$this->assertInstanceOf('BeSimple\SoapBundle\Tests\ServiceBinding\fixtures\Attributes', $result['attributes'][0]); $this->assertInstanceOf('BeSimple\SoapBundle\Tests\ServiceBinding\fixtures\Attributes', $result['attributes'][0]);
@ -83,18 +84,17 @@ class RpcLiteralRequestMessageBinderTest extends \PHPUnit_Framework_TestCase
public function testProcessMessageWithComplexTypeIntoComplexType() public function testProcessMessageWithComplexTypeIntoComplexType()
{ {
$definitionComplexTypes = $this->getDefinitionComplexTypes();
$methods = new \stdClass(); $methods = new \stdClass();
$methods->foo = 'bar'; $methods->foo = 'bar';
$methods->bar = 23; $methods->bar = 23;
$messageBinder = new RpcLiteralRequestMessageBinder(); $messageBinder = new RpcLiteralRequestMessageBinder();
$result = $messageBinder->processMessage( $result = $messageBinder->processMessage(
new Method('complextype_methods', null, array( new Method('complextype_methods', null, array(
new Argument('setters', new Type('\BeSimple\SoapBundle\Tests\ServiceBinding\fixtures\Setters')), new Argument('setters', new Type('\BeSimple\SoapBundle\Tests\ServiceBinding\fixtures\Setters')),
)), )),
array($methods), array($methods),
$definitionComplexTypes $this->getDefinitionComplexTypes()
); );
$this->assertInstanceOf('BeSimple\SoapBundle\Tests\ServiceBinding\fixtures\Setters', $result['setters']); $this->assertInstanceOf('BeSimple\SoapBundle\Tests\ServiceBinding\fixtures\Setters', $result['setters']);
@ -104,22 +104,22 @@ class RpcLiteralRequestMessageBinderTest extends \PHPUnit_Framework_TestCase
public function testProcessMessageWithComplexTypeMethods() public function testProcessMessageWithComplexTypeMethods()
{ {
$definitionComplexTypes = $this->getDefinitionComplexTypes();
$complexType = new \stdClass(); $complexType = new \stdClass();
$foo = $complexType->foo = new \stdClass(); $foo = $complexType->foo = new \stdClass();
$foo->foo = 'hello'; $foo->foo = 'hello';
$foo->bar = 24; $foo->bar = 24;
$bar = $complexType->bar = new \stdClass(); $bar = $complexType->bar = new \stdClass();
$bar->foo = 'bonjour'; $bar->foo = 'bonjour';
$bar->bar = 1012; $bar->bar = 1012;
$messageBinder = new RpcLiteralRequestMessageBinder(); $messageBinder = new RpcLiteralRequestMessageBinder();
$result = $messageBinder->processMessage( $result = $messageBinder->processMessage(
new Method('complextype_complextype', null, array( new Method('complextype_complextype', null, array(
new Argument('complex_type', new Type('\BeSimple\SoapBundle\Tests\ServiceBinding\fixtures\ComplexType')), new Argument('complex_type', new Type('\BeSimple\SoapBundle\Tests\ServiceBinding\fixtures\ComplexType')),
)), )),
array($complexType), array($complexType),
$definitionComplexTypes $this->getDefinitionComplexTypes()
); );
$this->assertInstanceOf('BeSimple\SoapBundle\Tests\ServiceBinding\fixtures\ComplexType', $result['complex_type']); $this->assertInstanceOf('BeSimple\SoapBundle\Tests\ServiceBinding\fixtures\ComplexType', $result['complex_type']);
@ -133,6 +133,40 @@ class RpcLiteralRequestMessageBinderTest extends \PHPUnit_Framework_TestCase
$this->assertSame(1012, $result['complex_type']->bar->getBar()); $this->assertSame(1012, $result['complex_type']->bar->getBar());
} }
public function testProcessMessageWithComplexTypeReferences()
{
$complexType1 = new \stdClass();
$foo = $complexType1->foo = new \stdClass();
$foo->foo = 'hello';
$foo->bar = 24;
$bar = $complexType1->bar = new \stdClass();
$bar->foo = 'bonjour';
$bar->bar = 1012;
$complexType2 = new \stdClass();
$complexType2->foo = $foo;
$complexType2->bar = $bar;
$complexTypes = new \stdClass();
$complexTypes->item = array($complexType1, $complexType2);
$messageBinder = new RpcLiteralRequestMessageBinder();
$result = $messageBinder->processMessage(
new Method('complextypes_references', null, array(
new Argument('complex_types', new Type('\BeSimple\SoapBundle\Tests\ServiceBinding\fixtures\ComplexType[]')),
)),
array($complexTypes),
$this->getDefinitionComplexTypes()
);
$this->assertInstanceOf('BeSimple\SoapBundle\Tests\ServiceBinding\fixtures\ComplexType', $result['complex_types'][0]);
$this->assertInstanceOf('BeSimple\SoapBundle\Tests\ServiceBinding\fixtures\ComplexType', $result['complex_types'][1]);
$this->assertSame($result['complex_types'][0]->getFoo(), $result['complex_types'][1]->getFoo());
$this->assertSame($result['complex_types'][0]->bar, $result['complex_types'][1]->bar);
}
public function messageProvider() public function messageProvider()
{ {
$messages = array(); $messages = array();