Core/src/ClientTrait.php
private function detectProjectId(array $config)
    {
        $config += [
            'httpHandler' => null,
            'projectId' => null,
            'projectIdRequired' => false,
            'hasEmulator' => false,
            'preferNumericProjectId' => false,
            'suppressKeyFileNotice' => false
        ];

        if ($config['projectId']) {
            return $config['projectId'];
        }

        if ($config['hasEmulator']) {
            return 'emulator-project';
        }

        if (isset($config['keyFile'])) {
            if (isset($config['keyFile']['project_id'])) {
                return $config['keyFile']['project_id'];
            }

            if ($config['suppressKeyFileNotice'] !== true) {
                $serviceAccountUri = 'https://cloud.google.com/iam/docs/' .
                    'creating-managing-service-account-keys#creating_service_account_keys';

                trigger_error(
                    sprintf(
                        'A keyfile was given, but it does not contain a project ' .
                        'ID. This can indicate an old and obsolete keyfile, ' .
                        'in which case you should create a new one. To suppress ' .
                        'this message, set `suppressKeyFileNotice` to `true` in your client configuration. ' .
                        'To learn more about generating new keys, see this URL: %s',
                        $serviceAccountUri
                    ),
                    E_USER_NOTICE
                );
            }
        }

        if (getenv('GOOGLE_CLOUD_PROJECT')) {
            return getenv('GOOGLE_CLOUD_PROJECT');
        }

        if (getenv('GCLOUD_PROJECT')) {
            return getenv('GCLOUD_PROJECT');
        }

        if ($this->onGce($config['httpHandler'])) {
            $metadata = $this->getMetaData();
            $projectId = $config['preferNumericProjectId']
                ? $metadata->getNumericProjectId()
                : $metadata->getProjectId();
            if ($projectId) {
                return $projectId;
            }
        }

        if ($config['projectIdRequired']) {
            throw new GoogleException(
                'No project ID was provided, ' .
                'and we were unable to detect a default project ID.'
            );
        }
    }
private function detectProjectId(array $config)
    {
        $config += [
            'httpHandler' => null,
            'projectId' => null,
            'projectIdRequired' => false,
            'hasEmulator' => false,
            'preferNumericProjectId' => false,
            'suppressKeyFileNotice' => false
        ];

        if ($config['projectId']) {
            return $config['projectId'];
        }

        if ($config['hasEmulator']) {
            return 'emulator-project';
        }

        if (isset($config['keyFile'])) {
            if (isset($config['keyFile']['project_id'])) {
                return $config['keyFile']['project_id'];
            }

            if ($config['suppressKeyFileNotice'] !== true) {
                $serviceAccountUri = 'https://cloud.google.com/iam/docs/' .
                    'creating-managing-service-account-keys#creating_service_account_keys';

                trigger_error(
                    sprintf(
                        'A keyfile was given, but it does not contain a project ' .
                        'ID. This can indicate an old and obsolete keyfile, ' .
                        'in which case you should create a new one. To suppress ' .
                        'this message, set `suppressKeyFileNotice` to `true` in your client configuration. ' .
                        'To learn more about generating new keys, see this URL: %s',
                        $serviceAccountUri
                    ),
                    E_USER_NOTICE
                );
            }
        }

        if (getenv('GOOGLE_CLOUD_PROJECT')) {
            return getenv('GOOGLE_CLOUD_PROJECT');
        }

        if (getenv('GCLOUD_PROJECT')) {
            return getenv('GCLOUD_PROJECT');
        }

        if ($this->onGce($config['httpHandler'])) {
            $metadata = $this->getMetaData();
            $projectId = $config['preferNumericProjectId']
                ? $metadata->getNumericProjectId()
                : $metadata->getProjectId();
            if ($projectId) {
                return $projectId;
            }
        }

        if ($config['projectIdRequired']) {
            throw new GoogleException(
                'No project ID was provided, ' .
                'and we were unable to detect a default project ID.'
            );
        }
    }
private function detectProjectId ( array $ config ) { $ config += [ 'httpHandler' => null , 'projectId' => null , 'projectIdRequired' => false , 'hasEmulator' => false , 'preferNumericProjectId' => false , 'suppressKeyFileNotice' => false ] ; if ( $ config [ 'projectId' ] ) { return $ config [ 'projectId' ] ; } if ( $ config [ 'hasEmulator' ] ) { return 'emulator-project' ; } if ( isset ( $ config [ 'keyFile' ] ) ) { if ( isset ( $ config [ 'keyFile' ] [ 'project_id' ] ) ) { return $ config [ 'keyFile' ] [ 'project_id' ] ; } if ( $ config [ 'suppressKeyFileNotice' ] !== true ) { $ serviceAccountUri = 'https://cloud.google.com/iam/docs/' . 'creating-managing-service-account-keys#creating_service_account_keys' ; trigger_error ( sprintf ( 'A keyfile was given, but it does not contain a project ' . 'ID. This can indicate an old and obsolete keyfile, ' . 'in which case you should create a new one. To suppress ' . 'this message, set `suppressKeyFileNotice` to `true` in your client configuration. ' . 'To learn more about generating new keys, see this URL: %s' , $ serviceAccountUri ) , E_USER_NOTICE ) ; } } if ( getenv ( 'GOOGLE_CLOUD_PROJECT' ) ) { return getenv ( 'GOOGLE_CLOUD_PROJECT' ) ; } if ( getenv ( 'GCLOUD_PROJECT' ) ) { return getenv ( 'GCLOUD_PROJECT' ) ; } if ( $ this -> onGce ( $ config [ 'httpHandler' ] ) ) { $ metadata = $ this -> getMetaData ( ) ; $ projectId = $ config [ 'preferNumericProjectId' ] ? $ metadata -> getNumericProjectId ( ) : $ metadata -> getProjectId ( ) ; if ( $ projectId ) { return $ projectId ; } } if ( $ config [ 'projectIdRequired' ] ) { throw new GoogleException ( 'No project ID was provided, ' . 'and we were unable to detect a default project ID.' ) ; } }
Detect and return a project ID.

Process:
1. If $config['projectId'] is set, use that.
2. If an emulator is enabled, return a dummy value.
3. If $config['keyFile'] is set, attempt to retrieve a project ID from
that.
4. Check `GOOGLE_CLOUD_PROJECT` environment variable.
5. Check `GCLOUD_PROJECT` environment variable.
6. If code is running on compute engine, try to get the project ID from
the metadata store.
7. Throw exception.

@param  array $config
@return string
@throws GoogleException
Detect and return a project ID .