Installation:
composer require barm/soap-bundle
Add to config/bundles.php (Symfony 4+):
return [
// ...
Barm\Bundle\SoapBundle\BarmSoapBundle::class => ['all' => true],
];
First Use Case:
Replace direct SoapClient instantiation with the bundle’s factory:
use Symfony\Component\DependencyInjection\ContainerInterface;
class MyService {
public function __construct(private ContainerInterface $container) {}
public function callSoapService() {
$wsdl = 'https://example.com/service.wsdl';
$soapClient = $this->container->get('barm_soap.factory')->create($wsdl);
$result = $soapClient->__soapCall('MethodName', [$arg1, $arg2]);
return $result;
}
}
Verify Profiler Integration: Check the WebProfiler toolbar under the "SOAP" tab to see logged requests.
Service Configuration (Recommended):
Define SOAP clients as services in config/services.yaml:
services:
App\Service\MySoapService:
arguments:
$soapClient: '@barm_soap.factory'
calls:
- [setWsdl, ['%env(WSDL_URL)%']]
Factory Method Pattern:
Use the factory to create SoapClient instances dynamically:
$client = $this->container->get('barm_soap.factory')->create($wsdl, [
'trace' => 1,
'exceptions' => true,
]);
Event Dispatching:
Listen for soap.request events (triggered after each request) to log or transform responses:
// src/EventListener/SoapListener.php
class SoapListener {
public function onSoapRequest(SoapRequestEvent $event) {
$response = $event->getResponse();
// Modify or log $response
}
}
Register in services.yaml:
services:
App\EventListener\SoapListener:
tags: ['kernel.event_listener', { event: 'soap.request' }]
Configuration Overrides:
Customize default options via config/packages/barm_soap.yaml:
barm_soap:
default_options:
trace: 1
cache_wsdl: WSDL_CACHE_BOTH
Profiler Dependency:
APP_DEBUG=false).webprofiler bundle is not installed (Symfony Flex includes it by default).APP_DEBUG=1 in .env during development.Event Dispatching Timing:
soap.request event fires after the SOAP request completes, so it’s unsuitable for request-level modifications (e.g., headers).kernel.request event to pre-process SOAP calls if needed.WSDL Caching:
cache_wsdl (e.g., WSDL_CACHE_NONE) may cause performance issues on repeated calls.WSDL_CACHE_DISK for development and WSDL_CACHE_BOTH for production.Factory vs. Direct Instantiation:
new SoapClient() calls—this breaks profiler logging.Enable SOAP Traces:
Pass trace: 1 to the factory to log raw request/response data:
$client = $factory->create($wsdl, ['trace' => 1]);
$client->__soapCall(...);
// Check $client->__getLastRequest() and $client->__getLastResponse()
Profiler Data:
?_profiler=soap to your request URL.Common SOAP Errors:
faultstring and faultcode in the profiler.connection_timeout or receive_timeout in the factory options.Custom Event Subscribers:
Extend the SoapRequestEvent to add metadata or validate responses:
class CustomSoapSubscriber implements EventSubscriberInterface {
public static function getSubscribedEvents() {
return ['soap.request' => 'onSoapRequest'];
}
public function onSoapRequest(SoapRequestEvent $event) {
$event->setMetadata(['custom_key' => 'value']);
}
}
Middleware for SOAP:
Use Symfony’s HttpClient middleware to pre-process SOAP requests (e.g., add auth headers):
$client = $factory->create($wsdl);
$client->__setLocation('https://custom-endpoint.com');
Testing: Mock the factory in PHPUnit:
$this->container->get('barm_soap.factory')->method('create')->willReturn($mockSoapClient);
How can I help you explore Laravel packages today?