Add 'src/BeSimple/SoapBundle/' from commit 'e99f707b105c0a0472260d8d42a5a14a7fb7a211'

git-subtree-dir: src/BeSimple/SoapBundle
git-subtree-mainline: 9a8d23fa23
git-subtree-split: e99f707b10
This commit is contained in:
Francis Besset
2013-07-19 17:00:11 +02:00
94 changed files with 5786 additions and 0 deletions

View File

@ -0,0 +1,170 @@
<?php
/*
* This file is part of the BeSimpleSoapBundle.
*
* (c) Christian Kerl <christian-kerl@web.de>
* (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.
*/
namespace BeSimple\SoapBundle\DependencyInjection;
use BeSimple\SoapCommon\Cache;
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\Config\FileLocator;
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>
* @author Francis Besset <francis.besset@gmail.com>
*/
class BeSimpleSoapExtension extends Extension
{
// maps config options to service suffix
private $bindingConfigToServiceSuffixMap = array(
'rpc-literal' => 'rpcliteral',
'document-wrapped' => 'documentwrapped',
);
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');
$processor = new Processor();
$configuration = new Configuration();
$config = $processor->process($configuration->getConfigTree(), $configs);
$this->registerCacheConfiguration($config['cache'], $container, $loader);
if (!empty($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);
}
}
private function registerCacheConfiguration(array $config, ContainerBuilder $container, XmlFileLoader $loader)
{
$loader->load('soap.xml');
$config['type'] = $this->getCacheType($config['type']);
foreach (array('type', 'lifetime', 'limit') as $key) {
$container->setParameter('besimple.soap.cache.'.$key, $config[$key]);
}
}
private function registerClientConfiguration(array $config, ContainerBuilder $container, XmlFileLoader $loader)
{
$loader->load('client.xml');
foreach ($config as $client => $options) {
$definition = new DefinitionDecorator('besimple.soap.client.builder');
$context = $container->setDefinition(sprintf('besimple.soap.client.builder.%s', $client), $definition);
$definition->replaceArgument(0, $options['wsdl']);
$defOptions = $container
->getDefinition('besimple.soap.client.builder')
->getArgument(1);
foreach (array('cache_type', 'user_agent') as $key) {
if (isset($options[$key])) {
$defOptions[$key] = $options[$key];
}
}
if (isset($defOptions['cache_type'])) {
$defOptions['cache_type'] = $this->getCacheType($defOptions['cache_type']);
}
$definition->replaceArgument(1, $defOptions);
if (!empty($options['classmap'])) {
$classmap = $this->createClientClassmap($client, $options['classmap'], $container);
$definition->replaceArgument(2, new Reference($classmap));
} else {
$definition->replaceArgument(2, null);
}
$this->createClient($client, $container);
}
}
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);
}
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));
}
private function createWebServiceContext(array $config, ContainerBuilder $container)
{
$bindingSuffix = $this->bindingConfigToServiceSuffixMap[$config['binding']];
unset($config['binding']);
$contextId = 'besimple.soap.context.'.$config['name'];
$definition = new DefinitionDecorator('besimple.soap.context.'.$bindingSuffix);
$context = $container->setDefinition($contextId, $definition);
if (isset($config['cache_type'])) {
$config['cache_type'] = $this->getCacheType($config['cache_type']);
}
$options = $container
->getDefinition('besimple.soap.context.'.$bindingSuffix)
->getArgument(5);
$definition->replaceArgument(5, array_merge($options, $config));
}
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;
}
}
}

View File

@ -0,0 +1,36 @@
<?php
/*
* This file is part of the BeSimpleSoapBundle.
*
* (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.
*/
namespace BeSimple\SoapBundle\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
/**
* Adds tagged besimple.soap.converter services to besimple.soap.converter.repository service
*
* @author Christian Kerl <christian-kerl@web.de>
*/
class TypeConverterPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
if (false === $container->hasDefinition('besimple.soap.converter.collection')) {
return;
}
$definition = $container->getDefinition('besimple.soap.converter.collection');
foreach ($container->findTaggedServiceIds('besimple.soap.converter') as $id => $attributes) {
$definition->addMethodCall('add', array(new Reference($id)));
}
}
}

View File

@ -0,0 +1,36 @@
<?php
/*
* This file is part of the BeSimpleSoapBundle.
*
* (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.
*/
namespace BeSimple\SoapBundle\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
/**
* Adds tagged besimple.soap.definition.loader services to ebservice.definition.resolver service
*
* @author Francis Besset <francis.besset@gmail.com>
*/
class WebServiceResolverPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
if (false === $container->hasDefinition('besimple.soap.definition.loader.resolver')) {
return;
}
$definition = $container->getDefinition('besimple.soap.definition.loader.resolver');
foreach ($container->findTaggedServiceIds('besimple.soap.definition.loader') as $id => $attributes) {
$definition->addMethodCall('addLoader', array(new Reference($id)));
}
}
}

View File

@ -0,0 +1,143 @@
<?php
/*
* This file is part of the BeSimpleSoapBundle.
*
* (c) Christian Kerl <christian-kerl@web.de>
* (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.
*/
namespace BeSimple\SoapBundle\DependencyInjection;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
/**
* WebServiceExtension configuration structure.
*
* @author Christian Kerl <christian-kerl@web.de>
* @author Francis Besset <francis.besset@gmail.com>
*/
class Configuration
{
private $cacheTypes = array('none', 'disk', 'memory', 'disk_memory');
/**
* Generates the configuration tree.
*
* @return \Symfony\Component\Config\Definition\ArrayNode The config tree
*/
public function getConfigTree()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('be_simple_soap');
$this->addCacheSection($rootNode);
$this->addClientSection($rootNode);
$this->addServicesSection($rootNode);
$this->addWsdlDumperSection($rootNode);
return $treeBuilder->buildTree();
}
private function addCacheSection(ArrayNodeDefinition $rootNode)
{
$rootNode
->children()
->arrayNode('cache')
->addDefaultsIfNotSet()
->children()
->scalarNode('type')
->defaultValue('disk')
->validate()
->ifNotInArray($this->cacheTypes)
->thenInvalid(sprintf('The cache type has to be either %s', implode(', ', $this->cacheTypes)))
->end()
->end()
->scalarNode('lifetime')->defaultNull()->end()
->scalarNode('limit')->defaultNull()->end()
->end()
->end()
->end()
;
}
private function addClientSection(ArrayNodeDefinition $rootNode)
{
$rootNode
->children()
->arrayNode('clients')
->useAttributeAsKey('name')
->prototype('array')
->children()
->scalarNode('wsdl')->isRequired()->end()
->scalarNode('user_agent')->end()
->scalarNode('cache_type')
->validate()
->ifNotInArray($this->cacheTypes)
->thenInvalid(sprintf('The cache type has to be either %s', implode(', ', $this->cacheTypes)))
->end()
->end()
->arrayNode('classmap')
->useAttributeAsKey('name')
->prototype('scalar')
->end()
->end()
->end()
->end()
;
}
private function addServicesSection(ArrayNodeDefinition $rootNode)
{
$rootNode
->children()
->arrayNode('services')
->useAttributeAsKey('name')
->prototype('array')
->children()
->scalarNode('namespace')
->isRequired()
->end()
->scalarNode('resource')
->defaultValue('*')
->end()
->scalarNode('resource_type')
->defaultValue('annotation')
->end()
->scalarNode('binding')
->defaultValue('document-wrapped')
->validate()
->ifNotInArray(array('rpc-literal', 'document-wrapped'))
->thenInvalid("Service binding style has to be either 'rpc-literal' or 'document-wrapped'")
->end()
->end()
->scalarNode('cache_type')
->validate()
->ifNotInArray($this->cacheTypes)
->thenInvalid(sprintf('The cache type has to be either %s', implode(', ', $this->cacheTypes)))
->end()
->end()
->end()
->end()
->end()
;
}
private function addWsdlDumperSection(ArrayNodeDefinition $rootNode)
{
$rootNode
->children()
->arrayNode('wsdl_dumper')
->addDefaultsIfNotSet()
->children()
->scalarNode('stylesheet')->defaultNull()
->end()
->end()
->end()
;
}
}