2010-10-04 20:27:00 +02:00
|
|
|
<?php
|
2011-09-04 01:59:32 +02: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>
|
2011-09-04 01:59:32 +02: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\DependencyInjection;
|
2010-10-04 20:27:00 +02:00
|
|
|
|
2011-09-04 01:59:32 +02:00
|
|
|
use BeSimple\SoapCommon\Cache;
|
|
|
|
|
2011-04-07 21:49:01 +02:00
|
|
|
use Symfony\Component\Config\Definition\Processor;
|
2011-07-17 10:46:54 +02:00
|
|
|
use Symfony\Component\Config\FileLocator;
|
2010-10-04 20:27:00 +02:00
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
2011-04-08 00:45:52 +02:00
|
|
|
use Symfony\Component\DependencyInjection\DefinitionDecorator;
|
|
|
|
use Symfony\Component\DependencyInjection\Reference;
|
2010-10-05 21:44:30 +02:00
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
|
2011-04-07 21:49:01 +02:00
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
|
|
|
|
|
2010-10-05 21:44:30 +02:00
|
|
|
/**
|
2011-07-18 22:43:12 +02:00
|
|
|
* BeSimpleSoapExtension.
|
2010-10-05 21:44:30 +02:00
|
|
|
*
|
|
|
|
* @author Christian Kerl <christian-kerl@web.de>
|
2011-09-04 01:59:32 +02:00
|
|
|
* @author Francis Besset <francis.besset@gmail.com>
|
2010-10-05 21:44:30 +02:00
|
|
|
*/
|
2011-07-18 22:43:12 +02:00
|
|
|
class BeSimpleSoapExtension extends Extension
|
2010-10-04 20:27:00 +02:00
|
|
|
{
|
2011-07-18 22:59:20 +02:00
|
|
|
// maps config options to service suffix
|
2011-07-21 22:55:59 +02:00
|
|
|
private $bindingConfigToServiceSuffixMap = array(
|
|
|
|
'rpc-literal' => 'rpcliteral',
|
2011-08-14 18:00:28 +02:00
|
|
|
'document-wrapped' => 'documentwrapped',
|
2011-07-21 22:55:59 +02:00
|
|
|
);
|
2011-07-14 17:45:03 +02:00
|
|
|
|
2011-04-07 21:49:01 +02:00
|
|
|
public function load(array $configs, ContainerBuilder $container)
|
2010-10-07 15:16:56 +02:00
|
|
|
{
|
2011-04-07 21:49:01 +02:00
|
|
|
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
|
2010-10-08 14:24:42 +02:00
|
|
|
|
2011-07-17 10:46:54 +02:00
|
|
|
$loader->load('loaders.xml');
|
2011-08-24 18:12:32 +02:00
|
|
|
$loader->load('converters.xml');
|
2011-04-07 21:49:01 +02:00
|
|
|
$loader->load('webservice.xml');
|
2011-07-14 17:45:03 +02:00
|
|
|
|
2011-07-17 10:46:54 +02:00
|
|
|
$processor = new Processor();
|
2011-04-07 21:49:01 +02:00
|
|
|
$configuration = new Configuration();
|
2010-10-08 14:24:42 +02:00
|
|
|
|
2011-04-07 21:49:01 +02:00
|
|
|
$config = $processor->process($configuration->getConfigTree(), $configs);
|
2011-07-14 17:45:03 +02:00
|
|
|
|
2011-09-04 01:59:32 +02:00
|
|
|
$this->registerCacheConfiguration($config['cache'], $container, $loader);
|
|
|
|
|
2011-10-12 21:08:59 +02:00
|
|
|
if (!empty($config['clients'])) {
|
2011-09-03 21:17:57 +02:00
|
|
|
$this->registerClientConfiguration($config['clients'], $container, $loader);
|
|
|
|
}
|
|
|
|
|
2011-08-14 21:59:03 +02:00
|
|
|
$container->setParameter('besimple.soap.definition.dumper.options.stylesheet', $config['wsdl_dumper']['stylesheet']);
|
|
|
|
|
2011-07-14 18:39:47 +02:00
|
|
|
foreach($config['services'] as $name => $serviceConfig) {
|
2011-07-17 10:46:54 +02:00
|
|
|
$serviceConfig['name'] = $name;
|
|
|
|
$this->createWebServiceContext($serviceConfig, $container);
|
2010-10-08 14:24:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-04 01:59:32 +02:00
|
|
|
private function registerCacheConfiguration(array $config, ContainerBuilder $container, XmlFileLoader $loader)
|
|
|
|
{
|
|
|
|
$loader->load('soap.xml');
|
|
|
|
|
2011-09-04 02:42:53 +02:00
|
|
|
$config['type'] = $this->getCacheType($config['type']);
|
2011-09-04 01:59:32 +02:00
|
|
|
|
|
|
|
foreach (array('type', 'lifetime', 'limit') as $key) {
|
|
|
|
$container->setParameter('besimple.soap.cache.'.$key, $config[$key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-03 21:17:57 +02:00
|
|
|
private function registerClientConfiguration(array $config, ContainerBuilder $container, XmlFileLoader $loader)
|
|
|
|
{
|
|
|
|
$loader->load('client.xml');
|
|
|
|
|
|
|
|
foreach ($config as $client => $options) {
|
2011-10-10 22:19:43 +02:00
|
|
|
$definition = new DefinitionDecorator('besimple.soap.client.builder');
|
|
|
|
$context = $container->setDefinition(sprintf('besimple.soap.client.builder.%s', $client), $definition);
|
2011-09-03 21:17:57 +02:00
|
|
|
|
|
|
|
$definition->replaceArgument(0, $options['wsdl']);
|
2011-09-04 02:42:53 +02:00
|
|
|
|
2011-10-08 22:04:18 +02:00
|
|
|
$defOptions = $container
|
2011-10-10 22:19:43 +02:00
|
|
|
->getDefinition('besimple.soap.client.builder')
|
2011-10-08 12:41:47 +02:00
|
|
|
->getArgument(1);
|
2011-09-04 02:42:53 +02:00
|
|
|
|
2011-10-10 22:19:43 +02:00
|
|
|
foreach (array('cache_type', 'user_agent') as $key) {
|
2011-10-08 22:04:18 +02:00
|
|
|
if (isset($options[$key])) {
|
|
|
|
$defOptions[$key] = $options[$key];
|
|
|
|
}
|
|
|
|
}
|
2011-09-04 23:43:20 +02:00
|
|
|
|
2011-10-08 22:04:18 +02:00
|
|
|
if (isset($defOptions['cache_type'])) {
|
2011-10-10 22:19:43 +02:00
|
|
|
$defOptions['cache_type'] = $this->getCacheType($defOptions['cache_type']);
|
2011-09-04 02:42:53 +02:00
|
|
|
}
|
2011-10-08 18:03:27 +02:00
|
|
|
|
2011-10-08 22:04:18 +02:00
|
|
|
$definition->replaceArgument(1, $defOptions);
|
|
|
|
|
2011-10-08 18:03:27 +02:00
|
|
|
if (!empty($options['classmap'])) {
|
|
|
|
$classmap = $this->createClientClassmap($client, $options['classmap'], $container);
|
|
|
|
$definition->replaceArgument(2, new Reference($classmap));
|
|
|
|
} else {
|
|
|
|
$definition->replaceArgument(2, null);
|
|
|
|
}
|
2011-10-10 22:19:43 +02:00
|
|
|
|
|
|
|
$this->createClient($client, $container);
|
2011-09-03 21:17:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-08 18:03:27 +02:00
|
|
|
private function createClientClassmap($client, array $classmap, ContainerBuilder $container)
|
|
|
|
{
|
|
|
|
$definition = new DefinitionDecorator('besimple.soap.classmap');
|
|
|
|
$context = $container->setDefinition(sprintf('besimple.soap.classmap.%s', $client), $definition);
|
|
|
|
|
|
|
|
$definition->setMethodCalls(array(
|
|
|
|
array('set', array($classmap)),
|
|
|
|
));
|
|
|
|
|
|
|
|
return sprintf('besimple.soap.classmap.%s', $client);
|
|
|
|
}
|
|
|
|
|
2011-10-10 22:19:43 +02:00
|
|
|
private function createClient($client, ContainerBuilder $container)
|
|
|
|
{
|
|
|
|
$definition = new DefinitionDecorator('besimple.soap.client');
|
|
|
|
$context = $container->setDefinition(sprintf('besimple.soap.client.%s', $client), $definition);
|
|
|
|
|
|
|
|
$definition->setFactoryService(sprintf('besimple.soap.client.builder.%s', $client));
|
|
|
|
}
|
|
|
|
|
2011-07-17 10:46:54 +02:00
|
|
|
private function createWebServiceContext(array $config, ContainerBuilder $container)
|
2010-10-04 20:27:00 +02:00
|
|
|
{
|
2011-04-08 00:45:52 +02:00
|
|
|
$bindingSuffix = $this->bindingConfigToServiceSuffixMap[$config['binding']];
|
|
|
|
unset($config['binding']);
|
2011-07-14 17:45:03 +02:00
|
|
|
|
2011-07-21 22:55:59 +02:00
|
|
|
$contextId = 'besimple.soap.context.'.$config['name'];
|
|
|
|
$definition = new DefinitionDecorator('besimple.soap.context.'.$bindingSuffix);
|
|
|
|
$context = $container->setDefinition($contextId, $definition);
|
2011-07-14 17:45:03 +02:00
|
|
|
|
2011-09-04 12:59:19 +02:00
|
|
|
if (isset($config['cache_type'])) {
|
|
|
|
$config['cache_type'] = $this->getCacheType($config['cache_type']);
|
|
|
|
}
|
|
|
|
|
2011-07-21 22:55:59 +02:00
|
|
|
$options = $container
|
|
|
|
->getDefinition('besimple.soap.context.'.$bindingSuffix)
|
2011-10-12 21:08:59 +02:00
|
|
|
->getArgument(5);
|
2011-07-17 10:46:54 +02:00
|
|
|
|
2011-10-12 21:08:59 +02:00
|
|
|
$definition->replaceArgument(5, array_merge($options, $config));
|
2010-10-04 20:27:00 +02:00
|
|
|
}
|
2011-09-04 02:42:53 +02:00
|
|
|
|
|
|
|
private function getCacheType($type)
|
|
|
|
{
|
|
|
|
switch ($type) {
|
|
|
|
case 'none':
|
|
|
|
return Cache::TYPE_NONE;
|
|
|
|
|
|
|
|
case 'disk':
|
|
|
|
return Cache::TYPE_DISK;
|
|
|
|
|
|
|
|
case 'memory':
|
|
|
|
return Cache::TYPE_MEMORY;
|
|
|
|
|
|
|
|
case 'disk_memory':
|
|
|
|
return Cache::TYPE_DISK_MEMORY;
|
|
|
|
}
|
|
|
|
}
|
2011-07-18 22:43:12 +02:00
|
|
|
}
|