google/cloud-core
Shared core infrastructure for Google Cloud PHP libraries, including common transport, auth, retry, and configuration utilities. Not intended for direct use—required by other Google Cloud components. Stable GA with backwards-compatible minor and patch releases.
Installation:
composer require google/cloud-core
Note: This package is not used directly in application code but is a dependency for other Google Cloud PHP packages (e.g., google/cloud-storage, google/cloud-bigquery).
First Use Case:
When integrating a Google Cloud service (e.g., Storage, BigQuery), ensure your Laravel app includes google/cloud-core as a dependency. The package handles:
Where to Look First:
google/cloud-storage) for implementation examples.Authentication and Configuration:
use Google\Cloud\Core\Grpc\CredentialsWrapper;
$credentials = new CredentialsWrapper([
'keyFile' => '/path/to/service-account.json',
]);
GOOGLE_APPLICATION_CREDENTIALS or GOOGLE_CLOUD_PROJECT environment variables for Laravel’s .env:
GOOGLE_APPLICATION_CREDENTIALS=/path/to/key.json
GOOGLE_CLOUD_PROJECT=your-project-id
Retry and Error Handling:
use Google\Cloud\Core\Retry\RetrySettings;
$retrySettings = new RetrySettings([
'maxAttempts' => 3,
'initialBackoff' => 100, // ms
]);
App\Exceptions\Handler) to log structured errors.Debugging and Emulators:
gcloud beta emulators datastore start --host-port=localhost:8080
Configure the emulator host in your Laravel service client:
$client = new Google\Cloud\Datastore\DatastoreClient([
'host' => 'localhost:8080',
]);
use Google\Cloud\Core\Trace\Trace;
Trace::enable();
Streaming and Pagination:
ObjectIterator for paginated responses (e.g., BigQuery queries):
foreach ($queryResults as $row) {
// Process row
}
Laravel Service Providers:
Bind Google Cloud clients in AppServiceProvider:
public function register()
{
$this->app->singleton(Google\Cloud\Storage\StorageClient::class, function ($app) {
return new Google\Cloud\Storage\StorageClient([
'keyFile' => config('services.google.key_file'),
]);
});
}
Artisan Commands: Use the package for CLI tasks (e.g., syncing files to GCS):
use Google\Cloud\Storage\StorageClient;
use Illuminate\Console\Command;
class SyncFilesCommand extends Command
{
protected $signature = 'gcs:sync';
public function handle(StorageClient $storage)
{
// Sync logic
}
}
Testing:
$this->app->instance(
Google\Cloud\Core\Grpc\CredentialsWrapper::class,
MockCredentials::class
);
GOOGLE_APPLICATION_CREDENTIALS as a Kubernetes secret for pod authentication.Direct Usage:
google/cloud-core directly. It’s a dependency for other packages (e.g., google/cloud-storage).google/cloud-bigquery) that rely on google/cloud-core.Deprecated Options:
keyFile and keyFilePath are deprecated. Use credentials or environment variables instead:
// Deprecated (avoid)
$client = new StorageClient(['keyFile' => 'path/to/key.json']);
// Recommended
$client = new StorageClient(['credentials' => $credentialsWrapper]);
PHP Version Compatibility:
Emulator Quirks:
$client = new StorageClient(['host' => 'localhost:8080', 'useGrpc' => false]);
Retry Logic:
Logging:
putenv('GOOGLE_CLOUD_DEBUG=1');
try {
$client->someOperation();
} catch (\Google\Cloud\Core\Exception\GoogleException $e) {
\Log::error('Google Cloud Error', ['error' => $e->getMessage()]);
}
Trace and Profiler:
use Google\Cloud\Core\Trace\Trace;
Trace::enable();
// Your code...
$traceData = Trace::getCurrentTrace();
Validation Errors:
OptionsValidator to catch misconfigured client options early:
use Google\Cloud\Core\ApiHelperTrait;
$this->validateOptions($options);
Configuration:
config/services.php:
'google' => [
'key_file' => env('GOOGLE_APPLICATION_CREDENTIALS'),
'project_id' => env('GOOGLE_CLOUD_PROJECT'),
'retry' => [
'max_attempts' => 3,
'initial_backoff' => 100,
],
],
Performance:
StorageClient) across requests to avoid reconnecting:
$storage = app(Google\Cloud\Storage\StorageClient::class);
Extension Points:
RetrySettings for service-specific backoff strategies.public function handle($request, Closure $next)
{
Trace::enable();
return $next($request);
}
Laravel-Specific:
google/cloud-core dependencies as singletons for consistency:
$this->app->singleton(CredentialsWrapper::class, function ($app) {
return new CredentialsWrapper(['keyFile' => config('services.google.key_file')]);
});
How can I help you explore Laravel packages today?