Replaced soap.client by soap.client.builder

This commit is contained in:
Francis Besset 2011-10-10 22:19:43 +02:00
parent 6d2e9d7634
commit d3c31116c7
4 changed files with 96 additions and 8 deletions

View File

@ -79,23 +79,23 @@ class BeSimpleSoapExtension extends Extension
$loader->load('client.xml'); $loader->load('client.xml');
foreach ($config as $client => $options) { foreach ($config as $client => $options) {
$definition = new DefinitionDecorator('besimple.soap.client'); $definition = new DefinitionDecorator('besimple.soap.client.builder');
$context = $container->setDefinition(sprintf('besimple.soap.client.%s', $client), $definition); $context = $container->setDefinition(sprintf('besimple.soap.client.builder.%s', $client), $definition);
$definition->replaceArgument(0, $options['wsdl']); $definition->replaceArgument(0, $options['wsdl']);
$defOptions = $container $defOptions = $container
->getDefinition('besimple.soap.client') ->getDefinition('besimple.soap.client.builder')
->getArgument(1); ->getArgument(1);
foreach (array('cache_type', 'namespace', 'user_agent') as $key) { foreach (array('cache_type', 'user_agent') as $key) {
if (isset($options[$key])) { if (isset($options[$key])) {
$defOptions[$key] = $options[$key]; $defOptions[$key] = $options[$key];
} }
} }
if (isset($defOptions['cache_type'])) { if (isset($defOptions['cache_type'])) {
$$defOptions['cache_type'] = $this->getCacheType($defOptions['cache_type']); $defOptions['cache_type'] = $this->getCacheType($defOptions['cache_type']);
} }
$definition->replaceArgument(1, $defOptions); $definition->replaceArgument(1, $defOptions);
@ -106,6 +106,8 @@ class BeSimpleSoapExtension extends Extension
} else { } else {
$definition->replaceArgument(2, null); $definition->replaceArgument(2, null);
} }
$this->createClient($client, $container);
} }
} }
@ -121,6 +123,14 @@ class BeSimpleSoapExtension extends Extension
return sprintf('besimple.soap.classmap.%s', $client); 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) private function createWebServiceContext(array $config, ContainerBuilder $container)
{ {
$bindingSuffix = $this->bindingConfigToServiceSuffixMap[$config['binding']]; $bindingSuffix = $this->bindingConfigToServiceSuffixMap[$config['binding']];

View File

@ -74,7 +74,6 @@ class Configuration
->prototype('array') ->prototype('array')
->children() ->children()
->scalarNode('wsdl')->isRequired()->end() ->scalarNode('wsdl')->isRequired()->end()
->scalarNode('namespace')->end()
->scalarNode('user_agent')->end() ->scalarNode('user_agent')->end()
->scalarNode('cache_type') ->scalarNode('cache_type')
->validate() ->validate()

View File

@ -4,12 +4,12 @@
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<parameters> <parameters>
<parameter key="besimple.soap.client.class">BeSimple\SoapClient\SoapClient</parameter> <parameter key="besimple.soap.client.builder.class">BeSimple\SoapBundle\Soap\SoapClientBuilder</parameter>
<parameter key="besimple.soap.classmap.class">BeSimple\SoapCommon\Classmap</parameter> <parameter key="besimple.soap.classmap.class">BeSimple\SoapCommon\Classmap</parameter>
</parameters> </parameters>
<services> <services>
<service id="besimple.soap.client" class="%besimple.soap.client.class%" abstract="true"> <service id="besimple.soap.client.builder" class="%besimple.soap.client.builder.class%" abstract="true">
<argument /> <!-- wsdl URI --> <argument /> <!-- wsdl URI -->
<argument type="collection"> <argument type="collection">
<argument key="debug">%kernel.debug%</argument> <argument key="debug">%kernel.debug%</argument>
@ -19,6 +19,8 @@
<argument type="service" id="besimple.soap.cache" /> <argument type="service" id="besimple.soap.cache" />
</service> </service>
<service id="besimple.soap.client" factory-service="besimple.soap.client.builder" factory-method="build" class="%besimple.soap.client.builder.class%" abstract="true" />
<service id="besimple.soap.classmap" class="%besimple.soap.classmap.class%" abstract="true" /> <service id="besimple.soap.classmap" class="%besimple.soap.classmap.class%" abstract="true" />
</services> </services>

View File

@ -0,0 +1,77 @@
<?php
namespace BeSimple\SoapBundle\Soap;
use BeSimple\SoapCommon\Classmap;
use BeSimple\SoapCommon\Converter\TypeConverterCollection;
use BeSimple\SoapClient\SoapClientBuilder as BaseSoapClientBuilder;
class SoapClientBuilder extends BaseSoapClientBuilder
{
protected $soapClient;
public function __construct($wsdl, array $options, Classmap $classmap = null, TypeConverterCollection $converters = null)
{
parent::__construct();
$this->checkOptions($options);
$this
->withWsdl($wsdl)
->withTrace($options['debug'])
;
if (isset($options['user_agent'])) {
$this->withUserAgent($options['user_agent']);
}
if (isset($options['cache_type'])) {
$this->withWsdlCache($options['cache_type']);
}
if ($classmap) {
$this->withClassmap($classmap);
}
if ($converters) {
$this->withTypeConverters($converters);
}
}
public function build()
{
if (!$this->soapClient) {
$this->soapClient = parent::build();
}
return $this->soapClient;
}
protected function checkOptions(array $options)
{
$checkOptions = array(
'debug' => false,
'cache_type' => null,
'exceptions' => true,
'user_agent' => 'BeSimpleSoap',
);
// check option names and live merge, if errors are encountered Exception will be thrown
$invalid = array();
$isInvalid = false;
foreach ($options as $key => $value) {
if (!array_key_exists($key, $checkOptions)) {
$isInvalid = true;
$invalid[] = $key;
}
}
if ($isInvalid) {
throw new \InvalidArgumentException(sprintf(
'The "%s" class does not support the following options: "%s".',
get_class($this),
implode('\', \'', $invalid)
));
}
}
}