2010-10-04 20:27:00 +02:00
|
|
|
<?php
|
2010-10-05 21:44:30 +02:00
|
|
|
/*
|
|
|
|
* This file is part of the WebServiceBundle.
|
|
|
|
*
|
|
|
|
* (c) Christian Kerl <christian-kerl@web.de>
|
|
|
|
*
|
|
|
|
* This source file is subject to the MIT license that is bundled
|
|
|
|
* with this source code in the file LICENSE.
|
|
|
|
*/
|
2010-10-04 20:27:00 +02:00
|
|
|
|
|
|
|
namespace Bundle\WebServiceBundle;
|
|
|
|
|
2010-12-30 02:18:10 +01:00
|
|
|
|
2010-10-04 21:13:45 +02:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2010-10-05 21:44:30 +02:00
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
2010-10-04 21:13:45 +02:00
|
|
|
|
|
|
|
use Symfony\Component\HttpKernel\HttpKernelInterface;
|
|
|
|
|
2010-10-05 21:44:30 +02:00
|
|
|
use Bundle\WebServiceBundle\Soap\SoapRequest;
|
|
|
|
use Bundle\WebServiceBundle\Soap\SoapResponse;
|
2010-10-08 14:24:42 +02:00
|
|
|
use Bundle\WebServiceBundle\Soap\SoapHeader;
|
2010-10-05 21:44:30 +02:00
|
|
|
|
2010-10-08 14:24:42 +02:00
|
|
|
use Bundle\WebServiceBundle\ServiceBinding\ServiceBinder;
|
2010-10-07 15:16:56 +02:00
|
|
|
|
2010-12-30 02:18:10 +01:00
|
|
|
use Bundle\WebServiceBundle\Converter\ConverterRepository;
|
|
|
|
|
2010-10-05 21:44:30 +02:00
|
|
|
use Bundle\WebServiceBundle\Util\String;
|
2010-10-04 21:13:45 +02:00
|
|
|
|
2010-10-05 21:44:30 +02:00
|
|
|
/**
|
|
|
|
* 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
|
2010-10-04 20:27:00 +02:00
|
|
|
{
|
2010-10-05 21:44:30 +02:00
|
|
|
/**
|
|
|
|
* @var \SoapServer
|
|
|
|
*/
|
|
|
|
protected $soapServer;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Bundle\WebServiceBundle\Soap\SoapRequest
|
|
|
|
*/
|
|
|
|
protected $soapRequest;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Bundle\WebServiceBundle\Soap\SoapResponse
|
|
|
|
*/
|
|
|
|
protected $soapResponse;
|
|
|
|
|
2010-10-07 15:16:56 +02:00
|
|
|
/**
|
2010-10-08 14:24:42 +02:00
|
|
|
* @var \Bundle\WebServiceBundle\ServiceBinding\ServiceBinder
|
2010-10-07 15:16:56 +02:00
|
|
|
*/
|
2010-10-08 14:24:42 +02:00
|
|
|
protected $serviceBinder;
|
2010-10-07 15:16:56 +02:00
|
|
|
|
2010-10-05 21:44:30 +02:00
|
|
|
/**
|
|
|
|
* @var \Symfony\Component\HttpKernel\HttpKernelInterface
|
|
|
|
*/
|
|
|
|
protected $kernel;
|
|
|
|
|
2010-12-30 02:18:10 +01:00
|
|
|
public function __construct(ServiceBinder $serviceBinder, ConverterRepository $converterRepository, HttpKernelInterface $kernel)
|
2010-10-05 21:44:30 +02:00
|
|
|
{
|
2010-10-08 14:24:42 +02:00
|
|
|
$this->serviceBinder = $serviceBinder;
|
2010-10-07 15:16:56 +02:00
|
|
|
|
2010-12-30 02:18:10 +01:00
|
|
|
$this->soapServer = new \SoapServer(
|
|
|
|
$this->serviceBinder->getSerializedServiceDefinition(),
|
|
|
|
array(
|
|
|
|
'classmap' => $this->serviceBinder->getSoapServerClassmap(),
|
|
|
|
'typemap' => $converterRepository->toSoapServerTypemap($this),
|
|
|
|
'features' => SOAP_SINGLE_ELEMENT_ARRAYS,
|
|
|
|
)
|
|
|
|
);
|
2010-10-05 21:44:30 +02:00
|
|
|
$this->soapServer->setObject($this);
|
|
|
|
|
|
|
|
$this->kernel = $kernel;
|
|
|
|
}
|
|
|
|
|
2010-10-04 21:13:45 +02:00
|
|
|
public function getRequest()
|
|
|
|
{
|
2010-10-05 21:44:30 +02:00
|
|
|
return $this->soapRequest;
|
2010-10-04 21:13:45 +02:00
|
|
|
}
|
2010-10-04 20:27:00 +02:00
|
|
|
|
2010-12-30 02:18:10 +01:00
|
|
|
public function getResponse()
|
|
|
|
{
|
|
|
|
return $this->soapResponse;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function handle(Request $request = null, $type = self::MASTER_REQUEST, $catch = true)
|
2010-10-04 21:13:45 +02:00
|
|
|
{
|
2010-10-05 21:44:30 +02:00
|
|
|
$this->soapRequest = $this->checkRequest($request);
|
|
|
|
|
2010-10-05 22:42:47 +02:00
|
|
|
ob_start();
|
2010-10-08 16:58:14 +02:00
|
|
|
$this->soapServer->handle($this->soapRequest->getSoapMessage());
|
2010-10-05 22:42:47 +02:00
|
|
|
|
|
|
|
$soapResponseContent = ob_get_clean();
|
|
|
|
$this->soapResponse->setContent($soapResponseContent);
|
2010-10-05 21:44:30 +02:00
|
|
|
|
|
|
|
return $this->soapResponse;
|
|
|
|
}
|
|
|
|
|
2010-10-07 15:16:56 +02:00
|
|
|
/**
|
|
|
|
* This method gets called once for every SOAP header the \SoapServer received
|
|
|
|
* and afterwards once for the called SOAP operation.
|
|
|
|
*
|
|
|
|
* @param string $method The SOAP header or SOAP operation name
|
|
|
|
* @param array $arguments
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2010-10-05 21:44:30 +02:00
|
|
|
public function __call($method, $arguments)
|
|
|
|
{
|
2010-10-08 14:24:42 +02:00
|
|
|
if($this->serviceBinder->isServiceHeader($method))
|
2010-10-05 21:44:30 +02:00
|
|
|
{
|
2010-10-07 15:16:56 +02:00
|
|
|
// collect request soap headers
|
2010-10-08 14:24:42 +02:00
|
|
|
$this->soapRequest->getSoapHeaders()->add(
|
|
|
|
$this->serviceBinder->processServiceHeader($method, $arguments[0])
|
|
|
|
);
|
2010-10-07 15:16:56 +02:00
|
|
|
|
|
|
|
return;
|
2010-10-05 21:44:30 +02:00
|
|
|
}
|
2010-10-07 15:16:56 +02:00
|
|
|
|
2010-10-08 14:24:42 +02:00
|
|
|
if($this->serviceBinder->isServiceMethod($method))
|
2010-10-05 21:44:30 +02:00
|
|
|
{
|
2010-10-08 14:24:42 +02:00
|
|
|
$this->soapRequest->attributes->add(
|
|
|
|
$this->serviceBinder->processServiceMethodArguments($method, $arguments)
|
|
|
|
);
|
2010-10-05 21:44:30 +02:00
|
|
|
|
2010-10-07 15:16:56 +02:00
|
|
|
// delegate to standard http kernel
|
2010-10-05 21:44:30 +02:00
|
|
|
$response = $this->kernel->handle($this->soapRequest, self::MASTER_REQUEST, true);
|
|
|
|
|
|
|
|
$this->soapResponse = $this->checkResponse($response);
|
|
|
|
|
2010-10-07 15:16:56 +02:00
|
|
|
// add response soap headers to soap server
|
2010-10-05 21:44:30 +02:00
|
|
|
foreach($this->soapResponse->getSoapHeaders() as $header)
|
|
|
|
{
|
2010-10-07 15:16:56 +02:00
|
|
|
$this->soapServer->addSoapHeader($header->toNativeSoapHeader());
|
2010-10-05 21:44:30 +02:00
|
|
|
}
|
|
|
|
|
2010-10-07 15:16:56 +02:00
|
|
|
// return operation return value to soap server
|
2010-10-08 14:24:42 +02:00
|
|
|
return $this->serviceBinder->processServiceMethodReturnValue(
|
|
|
|
$method,
|
|
|
|
$this->soapResponse->getReturnValue()
|
|
|
|
);
|
2010-10-05 21:44:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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'))
|
|
|
|
{
|
2010-10-05 22:42:47 +02:00
|
|
|
throw new \InvalidArgumentException();
|
2010-10-05 21:44:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $request;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks the given Response, that it is a SoapResponse.
|
|
|
|
*
|
|
|
|
* @param Response $response A response to check
|
|
|
|
*
|
|
|
|
* @return SoapResponse A valid SoapResponse
|
|
|
|
*
|
|
|
|
* @throws InvalidArgumentException if the given Response is null or not a SoapResponse
|
|
|
|
*/
|
|
|
|
protected function checkResponse(Response $response)
|
|
|
|
{
|
2010-10-05 22:42:47 +02:00
|
|
|
if($response == null || !is_a($response, __NAMESPACE__ . '\\Soap\\SoapResponse'))
|
2010-10-05 21:44:30 +02:00
|
|
|
{
|
2010-10-05 22:42:47 +02:00
|
|
|
throw new \InvalidArgumentException();
|
2010-10-05 21:44:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
2010-10-04 20:27:00 +02:00
|
|
|
}
|