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,270 +12,148 @@
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;
protected $handlerClass;
protected $handlerObject;
/**
* @return SoapServerBuilder
*/
static public function createWithDefaults()
{ {
return new self(); return parent::createWithDefaults()
}
public static function createWithDefaults()
{
$builder = new self();
$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;
return $this; return $this;
} }
/** /**
* Enables the HTTP session. The handler object is persisted between multiple requests in a session. * Enables the HTTP session. The handler object is persisted between multiple requests in a session.
*/ */
public function withHttpSession() public function withHttpSession()
{ {
$this->optionPersistence = SOAP_PERSISTENCE_SESSION; $this->persistence = SOAP_PERSISTENCE_SESSION;
return $this; return $this;
} }
/** /**
* Enables reporting of internal errors to clients. This should only be enabled in development environments. * Enables reporting of internal errors to clients. This should only be enabled in development environments.
* *
* @param boolean $enable * @param boolean $enable
*/ */
public function withErrorReporting($enable = true) public function withErrorReporting($enable = true)
{ {
$this->optionErrorReporting = $enable; $this->errorReporting = $enable;
return $this; 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;
}
public function withBase64Attachments() public function withBase64Attachments()
{ {
return $this; return $this;
} }
public function withSwaAttachments() public function withSwaAttachments()
{ {
return $this; return $this;
} }
public function withMtomAttachments() public function withMtomAttachments()
{ {
return $this; return $this;
} }
/** /**
* 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)
{ {
$this->withTypeMapping($converter->getTypeNamespace(), $converter->getTypeName(), array($converter, 'convertXmlToPhp'), array($converter, 'convertPhpToXml')); $this->withTypeMapping($converter->getTypeNamespace(), $converter->getTypeName(), array($converter, 'convertXmlToPhp'), array($converter, 'convertPhpToXml'));
return $this; return $this;
} }
public function withTypeConverters(TypeConverterCollection $converters, $merge = true) public function withTypeConverters(TypeConverterCollection $converters, $merge = true)
{ {
$this->withTypemap($converters->getTypemap(), $merge); $this->withTypemap($converters->getTypemap(), $merge);
return $this; return $this;
} }
/** /**
* Adds a type mapping to the typemap. * Adds a type mapping to the typemap.
* *
* @param string $xmlNamespace * @param string $xmlNamespace
* @param string $xmlType * @param string $xmlType
* @param callable $fromXmlCallback * @param callable $fromXmlCallback
@ -284,65 +162,68 @@ class SoapServerBuilder
public function withTypeMapping($xmlNamespace, $xmlType, $fromXmlCallback, $toXmlCallback) public function withTypeMapping($xmlNamespace, $xmlType, $fromXmlCallback, $toXmlCallback)
{ {
$this->options['typemap'][] = array( $this->options['typemap'][] = array(
'type_ns' => $xmlNamespace, 'type_ns' => $xmlNamespace,
'type_name' => $xmlType, 'type_name' => $xmlType,
'from_xml' => $fromXmlCallback, 'from_xml' => $fromXmlCallback,
'to_xml' => $toXmlCallback 'to_xml' => $toXmlCallback
); );
return $this; return $this;
} }
/** /**
* Sets the typemap. * Sets the typemap.
* *
* @param array $typemap The typemap. * @param array $typemap The typemap.
* @param boolean $merge If true the given typemap is merged into the existing one, otherwise the existing one is overwritten. * @param boolean $merge If true the given typemap is merged into the existing one, otherwise the existing one is overwritten.
*/ */
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;
} }
return $this; return $this;
} }
/** /**
* Adds a class mapping to the classmap. * Adds a class mapping to the classmap.
* *
* @param string $xmlType * @param string $xmlType
* @param string $phpType * @param string $phpType
*/ */
public function withClassMapping($xmlType, $phpType) public function withClassMapping($xmlType, $phpType)
{ {
$this->options['classmap'][$xmlType] = $phpType; $this->options['classmap'][$xmlType] = $phpType;
return $this; return $this;
} }
/** /**
* Sets the classmap. * Sets the classmap.
* *
* @param array $classmap The classmap. * @param array $classmap The classmap.
* @param boolean $merge If true the given classmap is merged into the existing one, otherwise the existing one is overwritten. * @param boolean $merge If true the given classmap is merged into the existing one, otherwise the existing one is overwritten.
*/ */
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

@ -16,39 +16,30 @@ use BeSimple\SoapServer\SoapServerBuilder;
/** /**
* UnitTest for \BeSimple\SoapServer\SoapServerBuilder * UnitTest for \BeSimple\SoapServer\SoapServerBuilder
* *
* @author Christian Kerl * @author Christian Kerl
*/ */
class SoapServerBuilderTest extends \PHPUnit_Framework_TestCase 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