Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating object of class PubSubClient when PUBSUB_EMULATOR_HOST is set produces Class "Grpc\ChannelCredentials" not found when grcp extension is not installed #8052

Open
botzkobg opened this issue Jan 28, 2025 · 0 comments

Comments

@botzkobg
Copy link

Environment details

  • OS: All supported
  • PHP version: All supported
  • Package name and version: google/cloud-pubsub 2.9.1

Steps to reproduce

  1. Configure php without installing grcp extension
  2. Try to create an object of class PubSubClient when env var PUBSUB_EMULATOR_HOST is set

Code example

$pubsubClient = new PubSubClient([
            'projectId'         => 'emulator-project',
            'credentialsConfig' => [
                'keyFile' => [
                    "client_id"     => "fake-fake-fake.apps.googleusercontent.com",
                    "client_secret" => "fake-fake-fake",
                    "refresh_token" => "fake-fake-fake",
                    "type"          => "authorized_user",
                ],
            ],
            'transportConfig'   => [
                'rest' => [
                    'httpHandler' => [HttpHandlerFactory::build($client), 'async'],
                ],
            ],
        ]);

Other affected classes

  • PublisherClient
  • SchemaServiceClient
  • SubscriberClient

This was introduced by PR https://github.com/googleapis/google-cloud-php/pull/8043/files#diff-7531da9182e8b27adc9c92152bb021494ed2dde7cfb50433a16d8adbcce60518 and more specifically commit googleapis/google-cloud-php-pubsub@7b2d692

This commit adds a method called setDefaultEmulatorConfig() to all affected classes to set $options['transportConfig']['grpc']['stubOpts']['credentials'] but the code doesn't check if Grpc\ChannelCredentials exits.

Workaround

Source: #7187 (comment)

To resolve the issue before a new version with a fix is released you can create a stub (grpc.php.stub) and load it before creating any of the affected objects:

<?php

/**
 * PubSubClient assumes that if the PUBSUB_EMULATOR_HOST env var is set that the grpc extension is installed :(
 */

declare(strict_types=1);

namespace Grpc;

if (! class_exists('Grpc\ChannelCredentials')) {
    class ChannelCredentials {
        public static function createInsecure(): null
        {
            return null;
        }
    }
}
<?php
require_once 'grpc.php.stub';

$pubsubClient = new PubSubClient([
            'projectId'         => 'emulator-project',
            'credentialsConfig' => [
                'keyFile' => [
                    "client_id"     => "fake-fake-fake.apps.googleusercontent.com",
                    "client_secret" => "fake-fake-fake",
                    "refresh_token" => "fake-fake-fake",
                    "type"          => "authorized_user",
                ],
            ],
            'transportConfig'   => [
                'rest' => [
                    'httpHandler' => [HttpHandlerFactory::build($client), 'async'],
                ],
            ],
        ]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant