2010-10-04 20:27:00 +02:00
|
|
|
<?php
|
2013-02-21 08:27:36 +01:00
|
|
|
|
2010-10-05 21:44:30 +02:00
|
|
|
/*
|
2011-07-18 22:43:12 +02:00
|
|
|
* This file is part of the BeSimpleSoapBundle.
|
2010-10-05 21:44:30 +02:00
|
|
|
*
|
|
|
|
* (c) Christian Kerl <christian-kerl@web.de>
|
2013-02-21 08:27:36 +01:00
|
|
|
* (c) Francis Besset <francis.besset@gmail.com>
|
2010-10-05 21:44:30 +02:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
2011-07-18 22:43:12 +02:00
|
|
|
namespace BeSimple\SoapBundle\Controller;
|
2011-02-03 01:07:08 +01:00
|
|
|
|
2011-07-18 22:43:12 +02:00
|
|
|
use BeSimple\SoapBundle\Soap\SoapRequest;
|
|
|
|
use BeSimple\SoapBundle\Soap\SoapResponse;
|
2010-10-07 15:16:56 +02:00
|
|
|
|
2011-07-17 10:46:54 +02:00
|
|
|
use Symfony\Component\DependencyInjection\ContainerAware;
|
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
2011-07-17 15:01:42 +02:00
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
2011-07-17 10:46:54 +02:00
|
|
|
use Symfony\Component\HttpKernel\HttpKernelInterface;
|
2010-10-04 21:13:45 +02:00
|
|
|
|
2010-10-05 21:44:30 +02:00
|
|
|
/**
|
|
|
|
* @author Christian Kerl <christian-kerl@web.de>
|
2013-02-21 08:27:36 +01:00
|
|
|
* @author Francis Besset <francis.besset@gmail.com>
|
2010-10-05 21:44:30 +02:00
|
|
|
*/
|
2011-03-25 20:09:23 +01:00
|
|
|
class SoapWebServiceController extends ContainerAware
|
2010-10-04 20:27:00 +02:00
|
|
|
{
|
2010-10-05 21:44:30 +02:00
|
|
|
/**
|
|
|
|
* @var \SoapServer
|
|
|
|
*/
|
|
|
|
protected $soapServer;
|
|
|
|
|
|
|
|
/**
|
2011-07-18 22:43:12 +02:00
|
|
|
* @var \BeSimple\SoapBundle\Soap\SoapRequest
|
2010-10-05 21:44:30 +02:00
|
|
|
*/
|
|
|
|
protected $soapRequest;
|
|
|
|
|
|
|
|
/**
|
2011-07-18 22:43:12 +02:00
|
|
|
* @var \BeSimple\SoapBundle\Soap\SoapResponse
|
2010-10-05 21:44:30 +02:00
|
|
|
*/
|
|
|
|
protected $soapResponse;
|
|
|
|
|
2010-10-07 15:16:56 +02:00
|
|
|
/**
|
2011-07-18 22:43:12 +02:00
|
|
|
* @var \BeSimple\SoapBundle\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
|
|
|
|
2011-08-14 18:00:28 +02:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $headers = array();
|
|
|
|
|
2010-10-05 21:44:30 +02:00
|
|
|
/**
|
2011-07-18 22:43:12 +02:00
|
|
|
* @return \BeSimple\SoapBundle\Soap\SoapResponse
|
2010-10-05 21:44:30 +02:00
|
|
|
*/
|
2011-07-17 10:46:54 +02:00
|
|
|
public function callAction($webservice)
|
2010-12-30 02:18:10 +01:00
|
|
|
{
|
2011-08-24 23:36:49 +02:00
|
|
|
$webServiceContext = $this->getWebServiceContext($webservice);
|
|
|
|
|
2011-04-07 21:54:53 +02:00
|
|
|
$this->serviceBinder = $webServiceContext->getServiceBinder();
|
2011-07-14 17:45:03 +02:00
|
|
|
|
2011-07-17 10:46:54 +02:00
|
|
|
$this->soapRequest = SoapRequest::createFromHttpRequest($this->container->get('request'));
|
2011-10-12 22:01:50 +02:00
|
|
|
$this->soapServer = $webServiceContext
|
|
|
|
->getServerBuilder()
|
|
|
|
->withHandler($this)
|
|
|
|
->build()
|
|
|
|
;
|
2010-10-05 22:42:47 +02:00
|
|
|
|
2011-03-25 20:09:23 +01:00
|
|
|
ob_start();
|
2011-07-17 10:46:54 +02:00
|
|
|
$this->soapServer->handle($this->soapRequest->getSoapMessage());
|
2010-10-05 21:44:30 +02:00
|
|
|
|
2013-02-21 08:27:36 +01:00
|
|
|
return $this->getResponse()->setContent(ob_get_clean());
|
2010-10-05 21:44:30 +02:00
|
|
|
}
|
|
|
|
|
2011-07-17 10:46:54 +02:00
|
|
|
/**
|
|
|
|
* @return Symfony\Component\HttpFoundation\Response
|
|
|
|
*/
|
|
|
|
public function definitionAction($webservice)
|
2011-07-14 17:45:03 +02:00
|
|
|
{
|
2012-08-27 08:59:29 +02:00
|
|
|
$response = new Response($this->getWebServiceContext($webservice)->getWsdlFileContent(
|
|
|
|
$this->container->get('router')->generate(
|
|
|
|
'_webservice_call',
|
|
|
|
array('webservice' => $webservice),
|
|
|
|
true
|
|
|
|
)
|
|
|
|
));
|
|
|
|
|
2013-02-21 08:27:36 +01:00
|
|
|
$query = $this->container->get('request')->query;
|
|
|
|
if ($query->has('wsdl') || $query->has('WSDL')) {
|
2011-04-09 00:43:47 +02:00
|
|
|
$response->headers->set('Content-Type', 'application/wsdl+xml');
|
2011-07-14 17:45:03 +02:00
|
|
|
} else {
|
2011-04-09 00:43:47 +02:00
|
|
|
$response->headers->set('Content-Type', 'text/xml');
|
2011-03-25 20:09:23 +01:00
|
|
|
}
|
2011-07-14 17:45:03 +02:00
|
|
|
|
2011-04-08 00:46:58 +02:00
|
|
|
return $response;
|
2011-03-25 20:09:23 +01:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2011-08-14 18:00:28 +02:00
|
|
|
if ($this->serviceBinder->isServiceMethod($method)) {
|
2011-08-17 08:35:49 +02:00
|
|
|
// @TODO Add all SoapHeaders in SoapRequest
|
2011-08-14 18:00:28 +02:00
|
|
|
foreach ($this->headers as $name => $value) {
|
|
|
|
if ($this->serviceBinder->isServiceHeader($method, $name)) {
|
|
|
|
$this->soapRequest->getSoapHeaders()->add($this->serviceBinder->processServiceHeader($method, $name, $value));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->headers = null;
|
|
|
|
|
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
|
|
|
|
2011-03-25 20:09:23 +01:00
|
|
|
// forward to controller
|
2011-08-11 00:42:14 +02:00
|
|
|
try {
|
|
|
|
$response = $this->container->get('http_kernel')->handle($this->soapRequest, HttpKernelInterface::SUB_REQUEST, false);
|
|
|
|
} catch (\SoapFault $e) {
|
|
|
|
$this->soapResponse = new Response(null, 500);
|
|
|
|
|
|
|
|
throw $e;
|
|
|
|
}
|
2010-10-05 21:44:30 +02:00
|
|
|
|
2013-02-21 08:27:36 +01:00
|
|
|
$this->setResponse($response);
|
2010-10-05 21:44:30 +02:00
|
|
|
|
2010-10-07 15:16:56 +02:00
|
|
|
// add response soap headers to soap server
|
2013-02-21 08:27:36 +01:00
|
|
|
foreach ($response->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,
|
2013-02-21 08:27:36 +01:00
|
|
|
$response->getReturnValue()
|
2010-10-08 14:24:42 +02:00
|
|
|
);
|
2011-08-14 18:00:28 +02:00
|
|
|
} else {
|
|
|
|
// collect request soap headers
|
|
|
|
$this->headers[$method] = $arguments[0];
|
2010-10-05 21:44:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-14 18:00:28 +02:00
|
|
|
/**
|
|
|
|
* @return \BeSimple\SoapBundle\Soap\SoapRequest
|
|
|
|
*/
|
2013-02-21 08:27:36 +01:00
|
|
|
protected function getRequest()
|
2011-08-14 18:00:28 +02:00
|
|
|
{
|
|
|
|
return $this->soapRequest;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return \BeSimple\SoapBundle\Soap\SoapResponse
|
|
|
|
*/
|
2013-02-21 08:27:36 +01:00
|
|
|
protected function getResponse()
|
2011-08-14 18:00:28 +02:00
|
|
|
{
|
2011-08-27 22:47:58 +02:00
|
|
|
return $this->soapResponse ?: $this->soapResponse = $this->container->get('besimple.soap.response');
|
2011-08-14 18:00:28 +02:00
|
|
|
}
|
|
|
|
|
2010-10-05 21:44:30 +02:00
|
|
|
/**
|
2013-02-21 08:27:36 +01:00
|
|
|
* Set the SoapResponse
|
2010-10-05 21:44:30 +02:00
|
|
|
*
|
2013-02-21 08:27:36 +01:00
|
|
|
* @param Response $response A response to check and set
|
2010-10-05 21:44:30 +02:00
|
|
|
*
|
2013-02-21 08:27:36 +01:00
|
|
|
* @return \BeSimple\SoapBundle\Soap\SoapResponse A valid SoapResponse
|
2010-10-05 21:44:30 +02:00
|
|
|
*
|
2013-02-21 08:27:36 +01:00
|
|
|
* @throws InvalidArgumentException If the given Response is not an instance of SoapResponse
|
2010-10-05 21:44:30 +02:00
|
|
|
*/
|
2013-02-21 08:27:36 +01:00
|
|
|
protected function setResponse(Response $response)
|
2010-10-05 21:44:30 +02:00
|
|
|
{
|
2011-08-11 00:42:14 +02:00
|
|
|
if (!$response instanceof SoapResponse) {
|
2011-08-27 22:47:58 +02:00
|
|
|
throw new \InvalidArgumentException('You must return an instance of BeSimple\SoapBundle\Soap\SoapResponse');
|
2010-10-05 21:44:30 +02:00
|
|
|
}
|
|
|
|
|
2013-02-21 08:27:36 +01:00
|
|
|
return $this->soapResponse = $response;
|
2010-10-05 21:44:30 +02:00
|
|
|
}
|
2011-07-17 10:46:54 +02:00
|
|
|
|
2011-07-17 15:01:42 +02:00
|
|
|
private function getWebServiceContext($webservice)
|
|
|
|
{
|
2013-02-21 08:27:36 +01:00
|
|
|
$context = sprintf('besimple.soap.context.%s', $webservice);
|
|
|
|
|
|
|
|
if (!$this->container->has($context)) {
|
|
|
|
throw new NotFoundHttpException(sprintf('No WebService with name "%s" found.', $webservice));
|
2011-07-17 15:01:42 +02:00
|
|
|
}
|
2011-08-14 18:00:28 +02:00
|
|
|
|
2013-02-21 08:27:36 +01:00
|
|
|
return $this->container->get($context);
|
2011-07-17 15:01:42 +02:00
|
|
|
}
|
|
|
|
}
|