Installation:
composer require beyerz/api-adapter-bundle
Add the bundle to AppKernel.php last in the registerBundles() array:
$bundles[] = new Beyerz\ApiAdapterBundle\BeyerzApiAdapterBundle();
Configure a Client (config.yml):
beyerz_api_adapter:
json:
stripe: # Client name (injected via `beyerz_api_adapter.client.stripe`)
base_url: "https://api.stripe.com/v1"
options:
- "auth_key=sk_test_..."
First Use Case:
Create a Manager class (e.g., StripeManager) to encapsulate API logic:
use Beyerz\ApiAdapterBundle\Manager\ManagerInterface;
use Beyerz\ApiAdapterBundle\Gateway\GatewayInterface;
class StripeManager implements ManagerInterface
{
private $gateway;
public function __construct(GatewayInterface $gateway)
{
$this->gateway = $gateway;
}
public function createCustomer(array $data)
{
return $this->gateway->post('/customers', $data);
}
}
Register the manager as a service in services.yml:
services:
stripe.manager:
class: AppBundle\Manager\StripeManager
arguments: ["@beyerz_api_adapter.client.stripe"]
Define a Manager:
ManagerInterface or implement it directly.GatewayInterface (auto-wired via beyerz_api_adapter.client.{name}).get(), post(), put()).Data Handling:
StripeCustomer) with @Serializer\SerializedName annotations for API response mapping:
use JMS\Serializer\Annotation as Serializer;
class StripeCustomer
{
/** @Serializer\SerializedName("id") */
public $id;
/** @Serializer\SerializedName("email") */
public $email;
}
$customer = $this->gateway->get('/customers/1', StripeCustomer::class);
Error Handling:
try {
$response = $this->gateway->post('/charges', $data);
} catch (\RuntimeException $e) {
// Log or rethrow
}
Reusable Logic:
withRetry() helper to the manager:
public function withRetry(callable $callback, int $retries = 3)
{
$attempts = 0;
while ($attempts < $retries) {
try {
return $callback();
} catch (\Exception $e) {
$attempts++;
if ($attempts >= $retries) throw $e;
sleep(2 ** $attempts); // Exponential backoff
}
}
}
Integration with Symfony:
class StripeController extends Controller
{
public function createCustomerAction(Request $request, StripeManager $manager)
{
$data = json_decode($request->getContent(), true);
return new JsonResponse($manager->createCustomer($data));
}
}
Bundle Ordering:
AppKernel to avoid autoloading conflicts with other bundles that might depend on it.JMSSerializer Dependencies:
jms/serializer-bundle is installed (composer require jms/serializer-bundle).config/bundles.php:
return [
// ...
JMS\SerializerBundle\JMSSerializerBundle::class => ['all' => true],
];
Gateway Initialization:
@beyerz_api_adapter.client.{name}).Response Validation:
$response = $this->gateway->post('/charges', $data);
if ($response->getStatusCode() !== 200) {
throw new \RuntimeException($response->getContent());
}
Deprecated Symfony2:
nelmio/api-client-bundlephp-http/guzzle7-adapter with custom logic.Enable Debug Mode:
beyerz_api_adapter.debug: true in config to log raw API responses:
beyerz_api_adapter:
debug: true
json:
stripe:
base_url: "https://api.stripe.com/v1"
Gateway Logs:
/_profiler) for HTTP request/response details if debug mode is enabled.Serializer Issues:
@Serializer\SerializedName annotations.var_dump($response->getContent()) to inspect).Caching Responses:
CacheInterface):
class CachedGateway implements GatewayInterface
{
private $gateway;
private $cache;
public function __construct(GatewayInterface $gateway, CacheInterface $cache)
{
$this->gateway = $gateway;
$this->cache = $cache;
}
public function get($uri, $class = null)
{
$cacheKey = md5($uri);
$response = $this->cache->get($cacheKey);
if ($response) return $response;
$response = $this->gateway->get($uri, $class);
$this->cache->set($cacheKey, $response, 300); // Cache for 5 mins
return $response;
}
}
Custom Gateways:
Beyerz\ApiAdapterBundle\Gateway\Gateway to add custom logic (e.g., signing requests):
class CustomGateway extends Gateway
{
protected function prepareRequest(Request $request)
{
$request->headers->set('X-Custom-Header', 'value');
return parent::prepareRequest($request);
}
}
services:
beyerz_api_adapter.client.stripe:
class: AppBundle\Gateway\CustomGateway
arguments: ["@beyerz_api_adapter.client.stripe.inner"]
Event Dispatching:
EventDispatcherInterface:
class StripeManager
{
private $gateway;
private $dispatcher;
public function __construct(GatewayInterface $gateway, EventDispatcherInterface $dispatcher)
{
$this->gateway = $gateway;
$this->dispatcher = $dispatcher;
}
public function createCustomer(array $data)
{
$this->dispatcher->dispatch(new PreApiEvent('stripe.customer.create', $data));
$response = $this->gateway->post('/customers', $data);
$this->dispatcher->dispatch(new PostApiEvent('stripe.customer.create', $response));
return $response;
}
}
Middleware Support:
sendRequest() method:
class MiddlewareGateway extends Gateway
{
public function sendRequest(Request $request)
{
// Add middleware logic here
return parent::sendRequest($request);
}
}
How can I help you explore Laravel packages today?