started refactoring SoapKernel to SoapWebServiceController, so Symfony2 kernel is no longer overridden and no special front controller scripts or environments are required

This commit is contained in:
Christian Kerl 2011-03-25 20:09:23 +01:00 committed by Christian Kerl
parent 047db378c7
commit 52642f3ec6
2 changed files with 55 additions and 74 deletions

View File

@ -29,14 +29,11 @@ use Bundle\WebServiceBundle\Converter\ConverterRepository;
use Bundle\WebServiceBundle\Util\String;
/**
* SoapKernel converts a SoapRequest to a SoapResponse. It uses PHP's SoapServer for SOAP message
* handling. The logic for every service method is implemented in a Symfony controller. The controller
* to use for a specific service method is defined in the ServiceDefinition. The controller is invoked
* by Symfony's HttpKernel implementation.
*
*
* @author Christian Kerl <christian-kerl@web.de>
*/
class SoapKernel implements HttpKernelInterface
class SoapWebServiceController extends ContainerAware
{
/**
* @var \SoapServer
@ -53,6 +50,11 @@ class SoapKernel implements HttpKernelInterface
*/
protected $soapResponse;
/**
* @var \Bundle\WebServiceBundle\ServiceConfigurationFactory
*/
protected $serviceConfigurationFactory;
/**
* @var \Bundle\WebServiceBundle\ServiceBinding\ServiceBinder
*/
@ -63,13 +65,10 @@ class SoapKernel implements HttpKernelInterface
*/
protected $kernel;
public function __construct(ServiceBinder $serviceBinder, SoapServerFactory $soapServerFactory, HttpKernelInterface $kernel)
public function __construct(ServiceConfigurationFactory $serviceConfigurationFactory, HttpKernelInterface $kernel)
{
$this->serviceBinder = $serviceBinder;
$this->soapServer = $soapServerFactory->create($this->soapRequest, $this->soapResponse);
$this->soapServer->setObject($this);
$this->serviceConfigurationFactory = $serviceConfigurationFactory;
$this->kernel = $kernel;
}
public function getRequest()
@ -82,19 +81,45 @@ class SoapKernel implements HttpKernelInterface
return $this->soapResponse;
}
public function handle(Request $request = null, $type = self::MASTER_REQUEST, $catch = true)
public function handle($webservice)
{
$this->soapRequest = $this->checkRequest($request);
$serviceConfiguration = $this->serviceConfigurationFactory->create($webservice);
$this->soapRequest = SoapRequest::createFromHttpRequest($this->container->get('request'));
$this->serviceBinder = $serviceConfiguration->createServiceBinder();
$this->soapServer = $serviceConfiguration->createServer($this->soapRequest, $this->soapResponse);
$this->soapServer->setObject($this);
ob_start();
{
$this->soapServer->handle($this->soapRequest->getSoapMessage());
}
$soapResponseContent = ob_get_clean();
$this->soapResponse->setContent($soapResponseContent);
return $this->soapResponse;
}
public function definition($webservice)
{
$serviceConfiguration = $this->serviceConfigurationFactory->create($webservice);
$request = $this->container->get('request');
if($request->query->has('WSDL'))
{
// dump wsdl file
// return
}
else
{
// dump pretty definition
// return $this->container->get('templating')->renderView('');
}
}
/**
* This method gets called once for every SOAP header the \SoapServer received
* and afterwards once for the called SOAP operation.
@ -122,8 +147,8 @@ class SoapKernel implements HttpKernelInterface
$this->serviceBinder->processServiceMethodArguments($method, $arguments)
);
// delegate to standard http kernel
$response = $this->kernel->handle($this->soapRequest, self::MASTER_REQUEST, true);
// forward to controller
$response = $this->kernel->handle($this->soapRequest, self::SUB_REQUEST, false);
$this->soapResponse = $this->checkResponse($response);
@ -142,32 +167,7 @@ class SoapKernel implements HttpKernelInterface
}
/**
* Checks the given Request, that it is a SoapRequest. If the request is null a new
* SoapRequest is created.
*
* @param Request $request A request to check
*
* @return SoapRequest A valid SoapRequest
*
* @throws InvalidArgumentException if the given Request is not a SoapRequest
*/
protected function checkRequest(Request $request)
{
if($request == null)
{
$request = new SoapRequest();
}
if(!is_a($request, __NAMESPACE__ . '\\Soap\\SoapRequest'))
{
throw new \InvalidArgumentException();
}
return $request;
}
/**
* Checks the given Response, that it is a SoapResponse.
* Checks that the given Response is a SoapResponse.
*
* @param Response $response A response to check
*
@ -177,7 +177,7 @@ class SoapKernel implements HttpKernelInterface
*/
protected function checkResponse(Response $response)
{
if($response == null || !is_a($response, __NAMESPACE__ . '\\Soap\\SoapResponse'))
if($response == null || $response instanceof SoapResponse)
{
throw new \InvalidArgumentException();
}

View File

@ -24,10 +24,10 @@ use Zend\Mime\Message;
*/
class SoapRequest extends Request
{
/**
* @var string
*/
protected $rawContent;
public static function createFromHttpRequest(Request $request)
{
return new static($request->query->all(), $request->request->all(), $request->attributes->all(), $request->cookies->all(), $request->files->all(), $request->server->all(), $request->content);
}
/**
* @var string
@ -49,24 +49,15 @@ class SoapRequest extends Request
*/
protected $soapAttachments;
public function __construct($rawContent = null, array $query = null, array $attributes = null, array $cookies = null, array $server = null)
public function initialize(array $query = array(), array $request = array(), array $attributes = array(), array $cookies = array(), array $files = array(), array $server = array(), $content = null)
{
parent::__construct($query, null, $attributes, $cookies, null, $server);
parent::initialize($query, $request, $attributes, $cookies, $files, $server, $content);
$this->rawContent = $rawContent != null ? $rawContent : $this->loadRawContent();
$this->soapMessage = null;
$this->soapHeaders = new Collection('getName');
$this->soapAttachments = new Collection('getId');
}
/**
* Gets raw data send to the server.
*
* @return string
*/
public function getRawContent()
{
return $this->rawContent;
$this->setRequestFormat('soap');
}
/**
@ -94,16 +85,6 @@ class SoapRequest extends Request
return $this->soapAttachments;
}
/**
* Loads the plain HTTP POST data.
*
* @return string
*/
protected function loadRawContent()
{
return isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : file_get_contents('php://input');
}
protected function initializeSoapMessage()
{
if($this->server->has('CONTENT_TYPE'))
@ -115,7 +96,7 @@ class SoapRequest extends Request
case 'multipart/related':
if($type['type'] == 'application/xop+xml')
{
return $this->initializeMtomSoapMessage($type, $this->rawContent);
return $this->initializeMtomSoapMessage($type, $this->getContent());
}
else
{
@ -132,7 +113,7 @@ class SoapRequest extends Request
}
// fallback
return $this->rawContent;
return $this->getContent();
}
protected function initializeMtomSoapMessage(array $contentTypeHeader, $content)