Extended SoapServerBuilder with AbstractSoapBuilder of SoapCommon library

This commit is contained in:
Francis Besset 2011-10-09 19:35:08 +02:00
parent 3bb6d3abd4
commit cd2d8d90e8
3 changed files with 112 additions and 240 deletions

View File

@ -12,127 +12,67 @@
namespace BeSimple\SoapServer; namespace BeSimple\SoapServer;
use BeSimple\SoapCommon\Cache; use BeSimple\SoapCommon\AbstractSoapBuilder;
use BeSimple\SoapCommon\Converter\TypeConverterInterface; use BeSimple\SoapCommon\Converter\TypeConverterInterface;
use BeSimple\SoapCommon\Converter\TypeConverterCollection; use BeSimple\SoapCommon\Converter\TypeConverterCollection;
/** /**
* SoapServerBuilder provides a fluent interface to configure and create a SoapServer instance. * SoapServerBuilder provides a fluent interface to configure and create a SoapServer instance.
* *
* @author Christian Kerl * @author Christian Kerl <christian-kerl@web.de>
*/ */
class SoapServerBuilder class SoapServerBuilder extends AbstractSoapBuilder
{ {
public static function createEmpty() protected $persistence;
{ protected $errorReporting;
return new self();
}
public static function createWithDefaults() protected $handlerClass;
protected $handlerObject;
/**
* @return SoapServerBuilder
*/
static public function createWithDefaults()
{ {
$builder = new self(); return parent::createWithDefaults()
$builder
->withSoapVersion12()
->withEncoding('UTF-8')
->withNoWsdlCache()
->withErrorReporting(false) ->withErrorReporting(false)
->withSingleElementArrays()
; ;
return $builder;
} }
private $options;
private $optionWsdl = null;
private $optionPersistence;
private $optionErrorReporting;
private $optionHandlerClass = null;
private $optionHandlerObject = null;
/** /**
* Initializes all options with the defaults used in the native SoapServer. * Initializes all options with the defaults used in the native SoapServer.
*/ */
private function __construct() public function __construct()
{ {
$this->optionPersistence = SOAP_PERSISTENCE_REQUEST; parent::__construct();
$this->persistence = SOAP_PERSISTENCE_REQUEST;
// TODO: this is not the default, but safer // TODO: this is not the default, but safer
$this->optionErrorReporting = false; $this->withErrorReporting(false);
$this->options = array( $this->options['classmap'] = array();
'soap_version' => SOAP_1_1, $this->options['typemap'] = array();
'cache_wsdl' => Cache::getType(),
'classmap' => array(),
'typemap' => array(),
'features' => 0
);
} }
public function build() public function build()
{ {
$this->validateOptions(); $this->validateOptions();
use_soap_error_handler($this->optionErrorReporting); use_soap_error_handler($this->errorReporting);
$server = new SoapServer($this->optionWsdl, $this->options); $server = new SoapServer($this->wsdl, $this->options);
$server->setPersistence($this->optionPersistence); $server->setPersistence($this->persistence);
if(null !== $this->optionHandlerClass) if (null !== $this->handlerClass) {
{ $server->setClass($this->handlerClass);
$server->setClass($this->optionHandlerClass); } elseif (null !== $this->handlerObject) {
} $server->setObject($this->handlerObject);
if(null !== $this->optionHandlerObject)
{
$server->setObject($this->optionHandlerObject);
} }
return $server; return $server;
} }
private function validateOptions()
{
if(null === $this->optionWsdl)
{
throw new \InvalidArgumentException('The WSDL has to be configured!');
}
if(null === $this->optionHandlerClass && null === $this->optionHandlerObject)
{
throw new \InvalidArgumentException('The handler has to be configured!');
}
}
public function withWsdl($wsdl)
{
$this->optionWsdl = $wsdl;
return $this;
}
public function withSoapVersion11()
{
$this->options['soap_version'] = SOAP_1_1;
return $this;
}
public function withSoapVersion12()
{
$this->options['soap_version'] = SOAP_1_2;
return $this;
}
public function withEncoding($encoding)
{
$this->options['encoding'] = $encoding;
return $this;
}
public function withActor($actor) public function withActor($actor)
{ {
$this->options['actor'] = $actor; $this->options['actor'] = $actor;
@ -145,7 +85,7 @@ class SoapServerBuilder
*/ */
public function withHttpSession() public function withHttpSession()
{ {
$this->optionPersistence = SOAP_PERSISTENCE_SESSION; $this->persistence = SOAP_PERSISTENCE_SESSION;
return $this; return $this;
} }
@ -157,35 +97,7 @@ class SoapServerBuilder
*/ */
public function withErrorReporting($enable = true) public function withErrorReporting($enable = true)
{ {
$this->optionErrorReporting = $enable; $this->errorReporting = $enable;
return $this;
}
public function withNoWsdlCache()
{
$this->options['cache_wsdl'] = Cache::TYPE_NONE;
return $this;
}
public function withWsdlDiskCache()
{
$this->options['cache_wsdl'] = Cache::TYPE_DISK;
return $this;
}
public function withWsdlMemoryCache()
{
$this->options['cache_wsdl'] = Cache::TYPE_MEMORY;
return $this;
}
public function withWsdlDiskAndMemoryCache()
{
$this->options['cache_wsdl'] = Cache::TYPE_DISK_MEMORY;
return $this; return $this;
} }
@ -206,57 +118,23 @@ class SoapServerBuilder
} }
/** /**
* Enables the SOAP_SINGLE_ELEMENT_ARRAYS feature.
*
* If enabled arrays containing only one element will be passed as arrays otherwise the single element is extracted and directly passed.
*/
public function withSingleElementArrays()
{
$this->options['features'] |= SOAP_SINGLE_ELEMENT_ARRAYS;
return $this;
}
/**
* Enables the SOAP_WAIT_ONE_WAY_CALLS feature.
*/
public function withWaitOneWayCalls()
{
$this->options['features'] |= SOAP_WAIT_ONE_WAY_CALLS;
return $this;
}
/**
* Enables the SOAP_USE_XSI_ARRAY_TYPE feature.
*/
public function withUseXsiArrayType()
{
$this->options['features'] |= SOAP_USE_XSI_ARRAY_TYPE;
return $this;
}
/**
*
*
* @param mixed $handler Can be either a class name or an object. * @param mixed $handler Can be either a class name or an object.
*
* @return SoapServerBuilder
*/ */
public function withHandler($handler) public function withHandler($handler)
{ {
if(is_string($handler) && class_exists($handler)) if (is_string($handler) && class_exists($handler)) {
{ $this->handlerClass = $handler;
$this->optionHandlerClass = $handler; $this->handlerObject = null;
$this->optionHandlerObject = null; } elseif (is_object($handler)) {
$this->handlerClass = null;
$this->handlerObject = $handler;
} else {
throw new \InvalidArgumentException('The handler has to be a class name or an object');
} }
if(is_object($handler)) return $this;
{
$this->optionHandlerClass = null;
$this->optionHandlerObject = $handler;
}
throw new \InvalidArgumentException('The handler has to be a class name or an object!');
} }
public function withTypeConverter(TypeConverterInterface $converter) public function withTypeConverter(TypeConverterInterface $converter)
@ -301,12 +179,9 @@ class SoapServerBuilder
*/ */
public function withTypemap($typemap, $merge = true) public function withTypemap($typemap, $merge = true)
{ {
if($merge) if ($merge) {
{
$this->options['typemap'] = array_merge($this->options['typemap'], $typemap); $this->options['typemap'] = array_merge($this->options['typemap'], $typemap);
} } else {
else
{
$this->options['typemap'] = $typemap; $this->options['typemap'] = $typemap;
} }
@ -334,15 +209,21 @@ class SoapServerBuilder
*/ */
public function withClassmap($classmap, $merge = true) public function withClassmap($classmap, $merge = true)
{ {
if($merge) if ($merge) {
{
$this->options['classmap'] = array_merge($this->options['classmap'], $classmap); $this->options['classmap'] = array_merge($this->options['classmap'], $classmap);
} } else {
else
{
$this->options['classmap'] = $classmap; $this->options['classmap'] = $classmap;
} }
return $this; return $this;
} }
protected function validateOptions()
{
$this->validateWsdl();
if (null === $this->handlerClass && null === $this->handlerObject) {
throw new \InvalidArgumentException('The handler has to be configured!');
}
}
} }

View File

@ -23,32 +23,23 @@ class SoapServerBuilderTest extends \PHPUnit_Framework_TestCase
{ {
public function testUnconfiguredWsdl() public function testUnconfiguredWsdl()
{ {
$builder = SoapServerBuilder::createEmpty(); $builder = $this->getSoapServerBuilder();
try $this->setExpectedException('InvalidArgumentException');
{
$builder->build(); $builder->build();
$this->fail('The SoapServer requires a WSDL file.');
}
catch(\InvalidArgumentException $e)
{
}
} }
public function testUnconfiguredHandler() public function testUnconfiguredHandler()
{ {
$builder = SoapServerBuilder::createEmpty(); $builder = $this->getSoapServerBuilder();
$builder->withWsdl('my.wsdl'); $builder->withWsdl('my.wsdl');
try $this->setExpectedException('InvalidArgumentException');
{
$builder->build(); $builder->build();
}
$this->fail('The SoapServer requires a handler.'); public function getSoapServerBuilder()
}
catch(\InvalidArgumentException $e)
{ {
} return new SoapServerBuilder();
} }
} }

0
vendors.php Normal file → Executable file
View File