Installation Add the bundle via Composer (if available in Packagist) or manually clone from the GitHub repo:
composer require 24hoursmedia/tesla-ws-bundle
Register the bundle in config/bundles.php (Symfony 4+):
return [
// ...
TeslaWsBundle\TeslaWsBundle::class => ['all' => true],
];
Configuration
Locate the bundle’s config file at Resources/config/services.yaml (or config/packages/tesla_ws.yaml if auto-discovered). Override defaults in config/packages/tesla_ws.yaml:
tesla_ws:
base_uri: 'https://api.tesla.example.com'
api_key: '%env(TESLA_API_KEY)%'
First Use Case: API Client Inject the client service into a controller or command:
use TeslaWsBundle\Service\TeslaClient;
class MyController extends AbstractController
{
public function __construct(private TeslaClient $teslaClient) {}
public function fetchData()
{
$response = $this->teslaClient->get('/vehicles');
return $this->json($response);
}
}
Request Handling Use the client’s fluent methods for common HTTP verbs:
$this->teslaClient
->setEndpoint('/vehicles')
->withHeader('Accept', 'application/json')
->withQuery(['limit' => 10])
->get(); // or post(), put(), delete()
Authentication The bundle likely handles OAuth2/JWT via config. Extend with custom auth logic:
tesla_ws:
auth:
strategy: 'oauth2'
token_url: '/oauth/token'
Response Processing Decorate the client to transform responses (e.g., JSON → DTOs):
$this->teslaClient->get('/vehicles')->then(function ($response) {
return json_decode($response, true);
});
Event-Driven Extensions
Listen to tesla_ws.request and tesla_ws.response events (if supported) to log or modify requests/responses:
// config/services.yaml
services:
App\EventListener\TeslaLogger:
tags:
- { name: 'kernel.event_listener', event: 'tesla_ws.request', method: 'onRequest' }
/v1/vehicles) and map them to config keys.TeslaClient interface in PHPUnit:
$this->teslaClient = $this->createMock(TeslaClient::class);
$this->teslaClient->method('get')->willReturn(['data' => 'mock']);
Deprecated/Archived Status
Configuration Overrides
# config/packages/tesla_ws.yaml
imports:
- { resource: '@TeslaWsBundle/Resources/config/services.yaml' }
tesla_ws:
custom_key: 'value' # Override specific keys
Error Handling
try {
$this->teslaClient->get('/vehicles');
} catch (\TeslaWsBundle\Exception\ApiException $e) {
$this->addFlash('error', $e->getMessage());
}
Endpoint Hardcoding
TeslaEndpoint service or DTO:
class VehicleEndpoint {
public const LIST = '/vehicles';
}
TESLA_WS_DEBUG=true in .env to log raw requests/responses.LogMiddleware for inspection:
$this->teslaClient->getClient()->getEmitter()->attach(
new \GuzzleHttp\Middleware::tap(function ($request) {
error_log($request->getUri());
})
);
Custom Clients Extend the base client to add domain-specific logic:
class CustomTeslaClient extends TeslaClient
{
public function fetchVehicleData($vehicleId)
{
return $this->get("/vehicles/{$vehicleId}")->then(function ($response) {
return $this->mapVehicleResponse($response);
});
}
}
Response Transformers Register a service to transform responses globally:
services:
App\Transformer\TeslaResponseTransformer:
tags:
- { name: 'tesla_ws.response_transformer' }
Middleware Add request/response middleware via the bundle’s config or DI:
tesla_ws:
middleware:
- 'App\Middleware\AddCustomHeader'
How can I help you explore Laravel packages today?