24hoursmedia/tesla-client-bundle
Installation
Add the bundle to composer.json:
"require": {
"24hoursmedia/tesla-client-bundle": "master-dev"
}
Run composer update and register the bundle in AppKernel.php:
new Tesla\Bundle\ClientBundle\TeslaClientBundle(),
First Use Case Fetch a URL using the HTTP client factory:
$factory = $this->get('tesla_client.http_client_factory');
$httpClient = $factory->get('https://api.example.com');
$request = $httpClient->createRequest('https://api.example.com/data');
$response = $httpClient->execute($request);
$content = $response->getContent(); // Process response
Where to Look First
Resources/config/services.yml for default configurations.HttpClientFactory for creating clients with custom base URLs.TeslaRequest and TeslaResponse for request/response manipulation.Client Creation Use the factory to instantiate clients with different base URLs or configurations:
$client = $factory->get('https://api.example.com', ['timeout' => 30]);
Request Customization
Extend TeslaRequest to add headers, query params, or auth:
$request = $client->createRequest('https://api.example.com/endpoint');
$request->setHeader('Authorization', 'Bearer token');
$request->setQuery(['param' => 'value']);
Response Handling Parse responses with built-in methods:
$response = $client->execute($request);
$data = json_decode($response->getContent(), true);
$status = $response->getStatusCode();
Caching Integration Leverage the bundled caching layer for repeated requests:
$cachedClient = $factory->get('https://api.example.com', ['cache' => true]);
Proxy Support Configure proxies via DI or factory options:
$proxyClient = $factory->get('https://api.example.com', [
'proxy' => ['http' => 'tcp://proxy.example.com:8080']
]);
HttpClientInterface directly into services for cleaner code.config.yml or environment variables.TeslaResponse to add custom exception handling for HTTP errors.Deprecated Symfony 2 The bundle is designed for Symfony 2, which may cause compatibility issues with newer Symfony versions (e.g., DI changes, HTTP client updates).
Limited Documentation
The README is sparse; explore the source (src/Tesla/Bundle/ClientBundle/) for undocumented features.
HttpClientFactory and TeslaRequest for hidden methods.Caching Quirks The caching layer may not support all HTTP methods or edge cases (e.g., conditional requests).
ETag/Last-Modified headers.Proxy Configuration
Proxy settings are passed as raw curl options, which may not align with modern PHP HTTP clients (e.g., Guzzle).
No Built-in Retries The bundle lacks retry logic for transient failures (e.g., 503 errors).
Enable Verbose Logging
Add CURLOPT_VERBOSE to curl options to debug requests:
$client = $factory->get('https://api.example.com', [
'curl_options' => [CURLOPT_VERBOSE => true]
]);
Log output to a file with CURLOPT_STDERR.
Check Response Headers
Use getHeaders() on TeslaResponse to inspect raw headers for clues:
$headers = $response->getHeaders();
Custom Request Classes
Extend TeslaRequest to add domain-specific logic:
class ApiRequest extends TeslaRequest {
public function setAuthToken($token) {
$this->setHeader('X-Auth-Token', $token);
}
}
Response Transformers
Override TeslaResponse to auto-transform JSON/XML:
class JsonResponse extends TeslaResponse {
public function getData() {
return json_decode($this->getContent(), true);
}
}
Middleware Support Use Symfony’s event system to intercept requests/responses:
# config.yml
tesla_client:
listeners:
request: ['App\EventListener\AddAuthHeaderListener']
Testing
Mock HttpClientInterface in tests to avoid real HTTP calls:
$mockClient = $this->createMock(HttpClientInterface::class);
$mockClient->method('execute')->willReturn($mockResponse);
$this->container->set('tesla_client.http_client', $mockClient);
How can I help you explore Laravel packages today?