aahmed/google-api-client-php-bundle
Installation:
composer require samiaraboglu/google-api-client-php-bundle
(Note: The README incorrectly references samiaraboglu; the actual package is aahmed/google-api-client-php-bundle per the prompt.)
Bundle Registration:
In config/bundles.php (Symfony 5+):
return [
// ...
Aahmed\GoogleApiBundle\GoogleApiBundle::class => ['all' => true],
];
Configuration:
Add to config/packages/aahmed_google_api.yaml:
aahmed_google_api:
credential_file: "%kernel.project_dir%/config/google-api-credentials.json"
application_name: "MyLaravelApp"
First Use Case: Inject the client into a service/controller:
use Aahmed\GoogleApiBundle\Service\GoogleApiClient;
class AnalyticsController extends Controller
{
public function __construct(private GoogleApiClient $client) {}
public function getAnalytics()
{
$analytics = $this->client->getService('analytics');
// Use $analytics service
}
}
Service Initialization: The bundle abstracts the Google API client initialization. Use dependency injection to access pre-configured services:
// In a controller/service
public function __construct(
private GoogleApiClient $googleClient,
private GoogleAuthService $authService
) {}
Service Access: Fetch Google API services dynamically:
$analytics = $this->googleClient->getService('analytics');
$drive = $this->googleClient->getService('drive');
OAuth2 Workflow: Handle OAuth2 scopes and tokens via the auth service:
$this->authService->setAccessToken($token);
$this->authService->authorize(); // Refreshes token if expired
Configuration Overrides:
Override default config per environment (e.g., config/packages/dev/aahmed_google_api.yaml):
aahmed_google_api:
credential_file: "%kernel.project_dir%/config/dev-credentials.json"
Laravel-Specific: Use Laravel’s service container to bind the bundle’s services:
// In AppServiceProvider::boot()
$this->app->bind(
Aahmed\GoogleApiBundle\Service\GoogleApiClient::class,
fn($app) => new GoogleApiClient($app['config']['aahmed_google_api'])
);
Caching Tokens:
Extend GoogleAuthService to persist tokens in Laravel’s cache:
$this->authService->setCache($app['cache.store']);
Batch Operations: Use the client’s batch request feature for efficiency:
$batch = $this->googleClient->createBatch();
$batch->add($analytics->users()->get(...));
$batch->add($drive->files()->list(...));
$results = $batch->execute();
Deprecated Bundle Registration:
The README shows AppKernel.php registration, which is outdated for Symfony 5+. Use config/bundles.php instead.
Credential File Paths:
Hardcoded paths in credential_file may break in tests or deployments. Use Laravel’s environment variables:
aahmed_google_api:
credential_file: "%env(GOOGLE_CREDENTIALS_PATH)%"
Service Naming:
The bundle uses Google’s service names (e.g., 'analytics'), but these must match the Google API PHP Client’s service names. Example:
'analytics' → Google_Service_Analytics'drive' → Google_Service_DriveToken Expiry: The auth service silently refreshes tokens, but ensure your app handles rate limits during refreshes. Add retry logic:
try {
$this->authService->authorize();
} catch (Exception $e) {
if ($e->getCode() === 401) {
sleep(1); // Avoid rate-limiting
$this->authService->authorize();
}
}
Enable Debugging: Configure the Google client to log HTTP requests:
$this->googleClient->setDebug(true);
$this->googleClient->setDeveloperKey('YOUR_KEY');
Common Errors:
Invalid Credentials: Verify client_credentials.json is valid and accessible.Quota Exceeded: Check Google Cloud Console for limits.Service Not Enabled: Ensure the API is enabled in Google Cloud Console.Custom Services: Extend the bundle to add domain-specific services:
// src/Service/CustomGoogleService.php
class CustomGoogleService extends GoogleApiClient
{
public function customAnalytics()
{
return $this->getService('analytics')->data()->ga()->get(...);
}
}
Middleware for Auth: Create Laravel middleware to handle OAuth2 flows:
// app/Http/Middleware/GoogleAuth.php
public function handle($request, Closure $next)
{
$this->authService->authorize();
return $next($request);
}
Event Listeners: Listen for token refresh events:
// In EventServiceProvider
protected $listen = [
\Aahmed\GoogleApiBundle\Event\TokenRefreshed::class => [
\App\Listeners\LogTokenRefresh::class,
],
];
Lazy-Load Services: Avoid instantiating all services upfront. Fetch them on-demand:
// Bad: Initializes all services
$this->googleClient->getAllServices();
// Good: Fetch only when needed
$this->googleClient->getService('analytics');
Batch Requests: For bulk operations (e.g., fetching 100+ records), use batch requests to reduce API calls:
$batch = $this->googleClient->createBatch();
foreach ($ids as $id) {
$batch->add($drive->files()->get($id));
}
$results = $batch->execute();
How can I help you explore Laravel packages today?