2011-04-07 21:54:53 +02:00
|
|
|
<?php
|
|
|
|
/*
|
2011-07-18 22:43:12 +02:00
|
|
|
* This file is part of the BeSimpleSoapBundle.
|
2011-04-07 21:54:53 +02:00
|
|
|
*
|
|
|
|
* (c) Christian Kerl <christian-kerl@web.de>
|
2013-10-15 11:46:12 +02:00
|
|
|
* (c) Francis Besset <francis.besset@gmail.com>
|
2011-04-07 21:54:53 +02:00
|
|
|
*
|
|
|
|
* This source file is subject to the MIT license that is bundled
|
|
|
|
* with this source code in the file LICENSE.
|
|
|
|
*/
|
|
|
|
|
2011-07-18 22:43:12 +02:00
|
|
|
namespace BeSimple\SoapBundle;
|
2011-04-07 21:54:53 +02:00
|
|
|
|
2011-07-18 22:43:12 +02:00
|
|
|
use BeSimple\SoapBundle\ServiceBinding\ServiceBinder;
|
2011-09-13 20:47:35 +02:00
|
|
|
use BeSimple\SoapCommon\Converter\TypeConverterCollection;
|
2013-10-15 11:46:12 +02:00
|
|
|
use BeSimple\SoapWsdl\Dumper\Dumper;
|
2011-10-12 22:01:50 +02:00
|
|
|
use BeSimple\SoapServer\SoapServerBuilder;
|
2011-07-17 10:46:54 +02:00
|
|
|
use Symfony\Component\Config\ConfigCache;
|
|
|
|
use Symfony\Component\Config\Loader\LoaderInterface;
|
|
|
|
|
2011-04-08 00:46:58 +02:00
|
|
|
/**
|
|
|
|
* WebServiceContext.
|
|
|
|
*
|
|
|
|
* @author Christian Kerl <christian-kerl@web.de>
|
2013-10-15 11:46:12 +02:00
|
|
|
* @author Francis Besset <francis.besset@gmail.com>
|
2011-04-08 00:46:58 +02:00
|
|
|
*/
|
2011-04-07 21:54:53 +02:00
|
|
|
class WebServiceContext
|
|
|
|
{
|
|
|
|
private $options;
|
2011-07-14 17:45:03 +02:00
|
|
|
|
2011-04-07 21:54:53 +02:00
|
|
|
private $serviceDefinition;
|
|
|
|
private $serviceBinder;
|
2011-10-12 22:01:50 +02:00
|
|
|
private $serverBuilder;
|
2011-07-14 17:45:03 +02:00
|
|
|
|
2013-10-15 11:46:12 +02:00
|
|
|
public function __construct(LoaderInterface $loader, TypeConverterCollection $converters, array $options)
|
2013-02-20 15:26:26 +01:00
|
|
|
{
|
2013-10-15 11:46:12 +02:00
|
|
|
$this->loader = $loader;
|
|
|
|
$this->converters = $converters;
|
|
|
|
$this->options = $options;
|
2011-04-07 21:54:53 +02:00
|
|
|
}
|
2011-07-14 17:45:03 +02:00
|
|
|
|
|
|
|
public function getServiceDefinition()
|
2011-04-07 21:54:53 +02:00
|
|
|
{
|
2011-07-17 10:46:54 +02:00
|
|
|
if (null === $this->serviceDefinition) {
|
2013-10-15 11:46:12 +02:00
|
|
|
$cache = new ConfigCache(sprintf('%s/%s.definition.php', $this->options['cache_dir'], $this->options['name']), $this->options['debug']);
|
|
|
|
if ($cache->isFresh()) {
|
|
|
|
$this->serviceDefinition = include (string) $cache;
|
2011-11-22 23:00:59 +01:00
|
|
|
} else {
|
|
|
|
if (!$this->loader->supports($this->options['resource'], $this->options['resource_type'])) {
|
|
|
|
throw new \LogicException(sprintf('Cannot load "%s" (%s)', $this->options['resource'], $this->options['resource_type']));
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->serviceDefinition = $this->loader->load($this->options['resource'], $this->options['resource_type']);
|
|
|
|
$this->serviceDefinition->setName($this->options['name']);
|
|
|
|
$this->serviceDefinition->setNamespace($this->options['namespace']);
|
|
|
|
|
2013-10-15 11:46:12 +02:00
|
|
|
$cache->write('<?php return unserialize('.var_export(serialize($this->serviceDefinition), true).');');
|
2011-04-08 00:46:58 +02:00
|
|
|
}
|
|
|
|
}
|
2011-07-14 17:45:03 +02:00
|
|
|
|
2011-04-08 00:46:58 +02:00
|
|
|
return $this->serviceDefinition;
|
|
|
|
}
|
2011-07-14 17:45:03 +02:00
|
|
|
|
2011-07-23 20:06:42 +02:00
|
|
|
public function getWsdlFileContent($endpoint = null)
|
|
|
|
{
|
|
|
|
return file_get_contents($this->getWsdlFile($endpoint));
|
|
|
|
}
|
|
|
|
|
2011-04-08 00:46:58 +02:00
|
|
|
public function getWsdlFile($endpoint = null)
|
|
|
|
{
|
2013-10-15 11:46:12 +02:00
|
|
|
$file = sprintf ('%s/%s.%s.wsdl', $this->options['cache_dir'], $this->options['name'], md5($endpoint));
|
|
|
|
$cache = new ConfigCache($file, $this->options['debug']);
|
2011-11-22 23:00:59 +01:00
|
|
|
|
2013-10-15 11:46:12 +02:00
|
|
|
if(!$cache->isFresh()) {
|
|
|
|
$definition = $this->getServiceDefinition();
|
2011-11-22 23:00:59 +01:00
|
|
|
|
2013-10-15 11:46:12 +02:00
|
|
|
if ($endpoint) {
|
|
|
|
$definition->setOption('location', $endpoint);
|
|
|
|
}
|
2011-07-14 17:45:03 +02:00
|
|
|
|
2013-11-26 08:39:07 +01:00
|
|
|
$dumper = new Dumper($definition, array('stylesheet' => $this->options['wsdl_stylesheet']));
|
2013-10-15 11:46:12 +02:00
|
|
|
$cache->write($dumper->dump());
|
2011-04-07 21:54:53 +02:00
|
|
|
}
|
2011-07-14 17:45:03 +02:00
|
|
|
|
2013-10-15 11:46:12 +02:00
|
|
|
return (string) $cache;
|
2011-04-07 21:54:53 +02:00
|
|
|
}
|
2011-07-14 17:45:03 +02:00
|
|
|
|
|
|
|
public function getServiceBinder()
|
2011-04-07 21:54:53 +02:00
|
|
|
{
|
2011-07-17 10:46:54 +02:00
|
|
|
if (null === $this->serviceBinder) {
|
2011-08-14 21:06:04 +02:00
|
|
|
$this->serviceBinder = new ServiceBinder(
|
|
|
|
$this->getServiceDefinition(),
|
2011-08-14 21:16:11 +02:00
|
|
|
new $this->options['binder_request_header_class'](),
|
2011-08-14 21:06:04 +02:00
|
|
|
new $this->options['binder_request_class'](),
|
|
|
|
new $this->options['binder_response_class']()
|
|
|
|
);
|
2011-04-07 21:54:53 +02:00
|
|
|
}
|
2011-07-14 17:45:03 +02:00
|
|
|
|
2011-04-07 21:54:53 +02:00
|
|
|
return $this->serviceBinder;
|
|
|
|
}
|
2011-07-14 17:45:03 +02:00
|
|
|
|
2011-10-12 22:01:50 +02:00
|
|
|
public function getServerBuilder()
|
2011-04-07 21:54:53 +02:00
|
|
|
{
|
2011-10-12 22:01:50 +02:00
|
|
|
if (null === $this->serverBuilder) {
|
|
|
|
$this->serverBuilder = SoapServerBuilder::createWithDefaults()
|
|
|
|
->withWsdl($this->getWsdlFile())
|
2013-10-15 11:46:12 +02:00
|
|
|
->withClassmap($this->getServiceDefinition()->getTypeRepository()->getClassmap())
|
2011-10-12 22:01:50 +02:00
|
|
|
->withTypeConverters($this->converters)
|
|
|
|
;
|
|
|
|
|
2011-11-14 20:23:40 +01:00
|
|
|
if (null !== $this->options['cache_type']) {
|
2011-10-12 22:01:50 +02:00
|
|
|
$this->serverBuilder->withWsdlCache($this->options['cache_type']);
|
|
|
|
}
|
2011-04-07 21:54:53 +02:00
|
|
|
}
|
2011-07-14 17:45:03 +02:00
|
|
|
|
2011-10-12 22:01:50 +02:00
|
|
|
return $this->serverBuilder;
|
2011-04-07 21:54:53 +02:00
|
|
|
}
|
2011-11-22 23:00:59 +01:00
|
|
|
}
|