BeSimpleSoap/DependencyInjection/BeSimpleSoapExtension.php

139 lines
4.6 KiB
PHP
Raw Normal View History

2010-10-04 20:27:00 +02:00
<?php
2011-09-04 01:59:32 +02:00
/*
* This file is part of the BeSimpleSoapBundle.
*
* (c) Christian Kerl <christian-kerl@web.de>
2011-09-04 01:59:32 +02:00
* (c) Francis Besset <francis.besset@gmail.com>
*
* 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 BeSimple\SoapBundle\DependencyInjection;
2010-10-04 20:27:00 +02:00
2011-09-04 01:59:32 +02:00
use BeSimple\SoapCommon\Cache;
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\Config\FileLocator;
2010-10-04 20:27:00 +02:00
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\DefinitionDecorator;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
/**
* BeSimpleSoapExtension.
*
* @author Christian Kerl <christian-kerl@web.de>
2011-09-04 01:59:32 +02:00
* @author Francis Besset <francis.besset@gmail.com>
*/
class BeSimpleSoapExtension extends Extension
2010-10-04 20:27:00 +02:00
{
// maps config options to service suffix
private $bindingConfigToServiceSuffixMap = array(
'rpc-literal' => 'rpcliteral',
'document-wrapped' => 'documentwrapped',
);
2011-07-14 17:45:03 +02:00
public function load(array $configs, ContainerBuilder $container)
{
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('loaders.xml');
$loader->load('converters.xml');
$loader->load('webservice.xml');
2011-07-14 17:45:03 +02:00
$processor = new Processor();
$configuration = new Configuration();
$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-09-03 21:17:57 +02:00
if (isset($config['clients'])) {
$this->registerClientConfiguration($config['clients'], $container, $loader);
}
$container->setParameter('besimple.soap.definition.dumper.options.stylesheet', $config['wsdl_dumper']['stylesheet']);
foreach($config['services'] as $name => $serviceConfig) {
$serviceConfig['name'] = $name;
$this->createWebServiceContext($serviceConfig, $container);
}
}
2011-09-04 01:59:32 +02:00
private function registerCacheConfiguration(array $config, ContainerBuilder $container, XmlFileLoader $loader)
{
$loader->load('soap.xml');
$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) {
$definition = new DefinitionDecorator('besimple.soap.client');
$context = $container->setDefinition(sprintf('besimple.soap.client.%s', $client), $definition);
$definition->replaceArgument(0, $options['wsdl']);
if (isset($options['cache_type'])) {
$options['cache_type'] = $this->getCacheType($options['cache_type']);
$defOptions = $container
->getDefinition('besimple.soap.client')
->getArgument(1);
$defOptions['cache_type'] = $options['cache_type'];
$defOptions['namespace'] = $options['namespace'];
$definition->replaceArgument(1, $defOptions);
}
2011-09-03 21:17:57 +02:00
}
}
private function createWebServiceContext(array $config, ContainerBuilder $container)
2010-10-04 20:27:00 +02:00
{
$bindingSuffix = $this->bindingConfigToServiceSuffixMap[$config['binding']];
unset($config['binding']);
2011-07-14 17:45:03 +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
if (isset($config['cache_type'])) {
$config['cache_type'] = $this->getCacheType($config['cache_type']);
}
$options = $container
->getDefinition('besimple.soap.context.'.$bindingSuffix)
->getArgument(4);
$definition->replaceArgument(4, array_merge($options, $config));
2010-10-04 20:27:00 +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;
}
}
}