Installation:
composer require avtonom/web-gate-bundle
Add to config/bundles.php (Symfony 4+):
return [
// ...
Avtonom\WebGateBundle\AvtonomWebGateBundle::class => ['all' => true],
Sensio\Bundle\BuzzBundle\SensioBuzzBundle::class => ['all' => true],
];
Configure (config/packages/web_gate.yaml):
web_gate:
soap:
environment: '%env(APP_ENV)%'
connection_timeout: 15
logger:
logging_max_files: 0
logging_level: 100
rest:
host: '%env(REST_HOST)%'
env: '%env(REST_ENV)%'
login: '%env(REST_LOGIN)%'
password: '%env(REST_PASSWORD)%'
First Use Case:
Define a REST client service (config/services.yaml):
services:
app.rest.user_client:
class: Avtonom\WebGateBundle\Service\RestService
arguments:
- '@web_gate.logger'
- '@buzz.client'
- 'GET'
- '%web_gate.rest.host%'
- '%web_gate.rest.env%/api/v1/user/'
- '%env(REST_LOGIN)%'
- '%env(REST_PASSWORD)%'
Use in a controller:
use Symfony\Component\HttpFoundation\Response;
class UserController extends AbstractController
{
public function getUser(Request $request): Response
{
$client = $this->get('app.rest.user_client');
$response = $client->send(['id' => 123], '/details');
return $this->json($response);
}
}
REST API Calls:
app.rest.order_client).
app.rest.order_client:
class: Avtonom\WebGateBundle\Service\RestService
arguments:
- '@web_gate.logger'
- '@buzz.client'
- 'POST'
- '%web_gate.rest.host%'
- '%web_gate.rest.env%/api/v1/orders/'
send() with path overrides:
$client->send($data, '/custom-path'); // Overrides base path
SOAP Integration:
services.yaml:
app.soap.client:
class: Avtonom\WebGateBundle\Service\SoapService
arguments:
- '@web_gate.logger'
- '%web_gate.soap.wsdl_url%'
- '%web_gate.soap.environment%'
$response = $this->get('app.soap.client')->call('GetUserDetails', ['userId' => 1]);
Authentication:
%env(REST_LOGIN)%).RestService to add custom auth headers:
class CustomRestService extends RestService {
protected function getHeaders(): array {
return array_merge(parent::getHeaders(), [
'X-Custom-Auth' => $this->getAuthToken(),
]);
}
}
Error Handling:
web_gate.logger).try {
$response = $client->send($data);
} catch (\Avtonom\WebGateBundle\Exception\GateException $e) {
$this->addFlash('error', $e->getMessage());
}
Testing:
BuzzClient in PHPUnit:
$mockBuzz = $this->createMock(\Buzz\Client\ClientInterface::class);
$mockBuzz->method('get')->willReturn(new \Buzz\Message\Response(200, [], '{}'));
$container->set('buzz.client', $mockBuzz);
Deprecated Symfony 2.x:
BuzzBundle Dependency:
sensio/buzz-bundle (v1.0). Newer versions may break compatibility.composer.json:
"require": {
"sensio/buzz-bundle": "1.0.0"
}
SOAP Limitations:
Logging Overhead:
logging_max_files: 0 disables log rotation. Set to 30 (e.g.) for production:
web_gate.logger:
logging_max_files: 30
Hardcoded Paths:
%web_gate.rest.env%/api/v1/) are concatenated in RestService. Override the getBaseUrl() method to customize:
class CustomRestService extends RestService {
protected function getBaseUrl(): string {
return $this->host . '/custom-base/';
}
}
Enable Verbose Logging:
logging_level: 100 (DEBUG) in config/packages/web_gate.yaml to log raw requests/responses.Buzz Client Debugging:
buzz.client debug mode:
# config/packages/sensio_buzz.yaml
sensio_buzz:
client:
debug: '%kernel.debug%'
Common Exceptions:
GateException: Wraps HTTP errors (e.g., 404, 500). Check the underlying Buzz\Exception\ExceptionInterface.SoapFault: SOAP-specific errors. Use try-catch to handle gracefully.Custom Response Handling:
RestService to parse responses (e.g., JSON → DTOs):
class JsonRestService extends RestService {
protected function parseResponse(\Buzz\Message\Response $response): array {
return json_decode($response->getContent(), true);
}
}
Middleware Support:
services:
app.rest.client:
class: Avtonom\WebGateBundle\Service\RestService
arguments:
- '@web_gate.logger'
- '@buzz.client.custom' # Custom client with middleware
Define the custom client:
buzz.client.custom:
class: Buzz\Client\Client
factory: ['@buzz.client', 'withMiddleware']
arguments:
- ['@app.middleware.auth']
Dynamic Configuration:
RestService to load configs dynamically (e.g., from a database):
class DynamicRestService extends RestService {
public function __construct(
LoggerInterface $logger,
ClientInterface $client,
string $method,
string $host,
string $env,
?callable $configLoader = null
) {
$this->configLoader = $configLoader;
parent::__construct($logger, $client, $method, $host, $env);
}
protected function getBaseUrl(): string {
$config = $this->configLoader ? $this->configLoader() : [];
return $this->host . ($config['base_path'] ?? '');
}
}
How can I help you explore Laravel packages today?