Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Google Api Client Php Bundle Laravel Package

aahmed/google-api-client-php-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. 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.)

  2. Bundle Registration: In config/bundles.php (Symfony 5+):

    return [
        // ...
        Aahmed\GoogleApiBundle\GoogleApiBundle::class => ['all' => true],
    ];
    
  3. 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"
    
  4. 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
        }
    }
    

Implementation Patterns

Core Workflow

  1. 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
    ) {}
    
  2. Service Access: Fetch Google API services dynamically:

    $analytics = $this->googleClient->getService('analytics');
    $drive = $this->googleClient->getService('drive');
    
  3. OAuth2 Workflow: Handle OAuth2 scopes and tokens via the auth service:

    $this->authService->setAccessToken($token);
    $this->authService->authorize(); // Refreshes token if expired
    
  4. 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"
    

Integration Tips

  • 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();
    

Gotchas and Tips

Pitfalls

  1. Deprecated Bundle Registration: The README shows AppKernel.php registration, which is outdated for Symfony 5+. Use config/bundles.php instead.

  2. 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)%"
    
  3. 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_Drive
  4. Token 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();
        }
    }
    

Debugging

  • 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.

Extension Points

  1. 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(...);
        }
    }
    
  2. 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);
    }
    
  3. Event Listeners: Listen for token refresh events:

    // In EventServiceProvider
    protected $listen = [
        \Aahmed\GoogleApiBundle\Event\TokenRefreshed::class => [
            \App\Listeners\LogTokenRefresh::class,
        ],
    ];
    

Performance Tips

  • 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();
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
codeflextech/permission-manager
karnoweb/livewire-datepicker
sayedenam/sayed-dashboard
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver