kreait / laravel-firebase

A Laravel package for the Firebase PHP Admin SDK
https://github.com/kreait/firebase-php
MIT License
1.08k stars 173 forks source link

Using the array of `credentials` in the config file doesn't work #228

Closed chadanuk closed 2 months ago

chadanuk commented 2 months ago

Describe the bug

When deploying to a server where we cache the config, we cannot use the environment variables directly.

So we want to use the array of credentials in the config file firebase.php

The only way I can see to do this at the moment is to replace the FirebaseProjectManager with my own that extends it and adds the following to the configure method before the final $factory->withHttpClientOptions call:

        if ($projectId = $config['credentials']['project_id'] ?? null) {
            $factory = $factory->withProjectId($projectId)->withServiceAccount($config['credentials']);

            return new FirebaseProject($factory, $config);
        }

Installed packages

{
    "name": "laravel/laravel",
    "description": "The Laravel Framework.",
    "keywords": [
        "framework",
        "laravel"
    ],
    "license": "MIT",
    "type": "project",
    "require": {
        "php": "^8.3",
        "benwilkins/laravel-fcm-notification": "dev-master",
        "cache/simple-cache-bridge": "^1.2",
        "doctrine/dbal": "^3.8.6",
        "dyrynda/laravel-efficient-uuid": "^4.5.3",
        "dyrynda/laravel-model-uuid": "^6.6.3",
        "fakerphp/faker": "^1.23.1",
        "florianv/laravel-swap": "^2.3",
        "guzzlehttp/guzzle": "^7.8.1",
        "laravel-notification-channels/fcm": "^2.7",
        "laravel/framework": "^9.52.16",
        "laravel/horizon": "^5.25",
        "laravel/slack-notification-channel": "^2.5",
        "laravel/tinker": "^2.9",
        "lcobucci/jwt": "^4.3",
        "league/flysystem-aws-s3-v3": "^3.28",
        "livewire/livewire": "^2.12.8",
        "maatwebsite/excel": "^3.1.55",
        "nyholm/psr7": "^1.8.1",
        "php-http/curl-client": "^2.3.2",
        "php-http/message": "^1.16.1",
        "php-open-source-saver/jwt-auth": "^1.4.2",
        "predis/predis": "^2.2.2",
        "rollbar/rollbar-laravel": "^7.2",
        "spatie/laravel-fractal": "^6.2.1",
        "spatie/laravel-newsletter": "^4.11",
        "spatie/laravel-permission": "^5.11.1",
        "symfony/http-client": "^7.1.2"
    },
    "require-dev": {
        "beyondcode/laravel-dump-server": "^1.9",
        "infection/infection": "^0.26.21",
        "jasonmccreary/laravel-test-assertions": "^2.3",
        "mockery/mockery": "^1.6.12",
        "nunomaduro/collision": "^6.4",
        "nunomaduro/larastan": "^1.0.4",
        "nunomaduro/phpinsights": "^2.11",
        "osteel/openapi-httpfoundation-testing": "^0.7.2",
        "php-parallel-lint/php-var-dump-check": "^0.5.0",
        "phpmd/phpmd": "^2.15",
        "phpunit/phpunit": "^9.6.20",
        "spatie/laravel-ignition": "^1.7",
        "squizlabs/php_codesniffer": "^3.10.1",
        "tomb1n0/guzzle-mock-handler": "^1.6"
    },
    "autoload": {
        "classmap": [
            "database/factory-builders"
        ],
        "psr-4": {
            "App\\": "app/",
            "Generators\\": "database/generators/",
            "Database\\Factories\\": "database/factories/",
            "Database\\Seeders\\": "database/seeders/",
            "Database\\Seeders\\Permissions\\": "database/seeders/permissions/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/",
            "BadgeSeeders\\": "database/seeders/Badges/"
        }
    },
    "extra": {
        "laravel": {
            "dont-discover": []
        }
    },
    "scripts": {
        "post-root-package-install": [
            "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "@php artisan key:generate"
        ],
        "post-autoload-dump": [
            "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
            "@php artisan package:discover"
        ],
        "lint:php:version": [
            "./vendor/squizlabs/php_codesniffer/bin/phpcs --version"
        ],
        "lint:php": [
            "./vendor/squizlabs/php_codesniffer/bin/phpcs ./app"
        ],
        "fix:php": [
            "./vendor/squizlabs/php_codesniffer/bin/phpcbf ./app"
        ],
        "test": "./vendor/bin/phpunit --testdox -v --colors=always",
        "infection": "./vendor/bin/infection --ansi",
        "analyse": "php artisan code:analyse",
        "post-update-cmd": [
            "@php artisan vendor:publish --tag=laravel-assets --ansi --force"
        ]
    },
    "config": {
        "preferred-install": "dist",
        "sort-packages": true,
        "optimize-autoloader": true,
        "platform": {
            "php": "8.3"
        },
        "allow-plugins": {
            "ocramius/package-versions": true,
            "infection/extension-installer": true,
            "dealerdirect/phpcodesniffer-composer-installer": true,
            "php-http/discovery": true
        }
    },
    "minimum-stability": "dev",
    "prefer-stable": true
}

PHP version and extensions

composer                2.7.7     Composer package
composer-plugin-api     2.6.0     The Composer Plugin API
composer-runtime-api    2.2.2     The Composer Runtime API
ext-ctype               8.3.8     The ctype PHP extension
ext-curl                8.3.8     The curl PHP extension
ext-date                8.3.8     The date PHP extension
ext-dom                 20031129  The dom PHP extension
ext-exif                8.3.8     The exif PHP extension
ext-fileinfo            8.3.8     The fileinfo PHP extension
ext-filter              8.3.8     The filter PHP extension
ext-gd                  8.3.8     The gd PHP extension
ext-hash                8.3.8     The hash PHP extension
ext-iconv               8.3.8     The iconv PHP extension
ext-imagick             3.7.0     The imagick PHP extension
ext-intl                8.3.8     The intl PHP extension
ext-json                8.3.8     The json PHP extension
ext-libxml              8.3.8     The libxml PHP extension
ext-mbstring            8.3.8     The mbstring PHP extension
ext-mysqli              8.3.8     The mysqli PHP extension
ext-mysqlnd             0         The mysqlnd PHP extension (actual version: mysqlnd 8.3.8)
ext-openssl             8.3.8     The openssl PHP extension
ext-pcntl               8.3.8     The pcntl PHP extension
ext-pcre                8.3.8     The pcre PHP extension
ext-pdo                 8.3.8     The PDO PHP extension
ext-pdo_mysql           8.3.8     The pdo_mysql PHP extension
ext-pdo_sqlite          8.3.8     The pdo_sqlite PHP extension
ext-phar                8.3.8     The Phar PHP extension
ext-posix               8.3.8     The posix PHP extension
ext-random              8.3.8     The random PHP extension
ext-readline            8.3.8     The readline PHP extension
ext-redis               6.0.2     The redis PHP extension
ext-reflection          8.3.8     The Reflection PHP extension
ext-session             8.3.8     The session PHP extension
ext-simplexml           8.3.8     The SimpleXML PHP extension
ext-soap                8.3.8     The soap PHP extension
ext-sockets             8.3.8     The sockets PHP extension
ext-sodium              8.3.8     The sodium PHP extension
ext-spl                 8.3.8     The SPL PHP extension
ext-sqlite3             8.3.8     The sqlite3 PHP extension
ext-tokenizer           8.3.8     The tokenizer PHP extension
ext-xml                 8.3.8     The xml PHP extension
ext-xmlreader           8.3.8     The xmlreader PHP extension
ext-xmlwriter           8.3.8     The xmlwriter PHP extension
ext-yaml                2.2.3     The yaml PHP extension
ext-zend-opcache        8.3.8     The Zend OPcache PHP extension
ext-zip                 1.22.3    The zip PHP extension
ext-zlib                8.3.8     The zlib PHP extension
lib-curl                7.88.1    The curl library
lib-curl-libssh2        1.10.0    curl libssh2 version
lib-curl-openssl        3.0.11    curl OpenSSL version (3.0.11)
lib-curl-zlib           1.2.13    curl zlib version
lib-date-timelib        2022.10   date timelib version
lib-date-zoneinfo       2024.1    zoneinfo ("Olson") database for date
lib-fileinfo-libmagic   543       fileinfo libmagic version
lib-gd                  2.0.35    The gd library
lib-gd-freetype         2.12.1    freetype version for gd
lib-gd-libjpeg          6.2       libjpeg version for gd
lib-gd-libpng           1.6.39    libpng version for gd
lib-gd-libxpm           3.4.11    libxpm version for gd
lib-iconv               2.36      The iconv library
lib-icu                 72.1      The ICU unicode and globalization support library
lib-icu-cldr            42        ICU CLDR project version
lib-icu-unicode         15.0.0    ICU unicode version
lib-icu-zoneinfo        2022.5    zoneinfo ("Olson") database for icu
lib-imagick-imagemagick 6.9.11.60 The imagick-imagemagick library
lib-libsodium           1.0.18    The libsodium library
lib-libxml              2.9.14    libxml library version
lib-mbstring-libmbfl    1.3.2     mbstring libmbfl version
lib-mbstring-oniguruma  6.9.8     mbstring oniguruma version
lib-openssl             3.0.11    OpenSSL 3.0.11 19 Sep 2023
lib-pcre                10.42     The pcre library
lib-pcre-unicode        14.0.0    PCRE Unicode version support
lib-pdo_sqlite-sqlite   3.40.1    The pdo_sqlite-sqlite library
lib-sqlite3-sqlite      3.40.1    The sqlite3-sqlite library
lib-yaml-libyaml        0.2.5     libyaml version of yaml
lib-zip-libzip          1.7.3     The zip-libzip library
lib-zlib                1.2.13    The zlib library
php                     8.3       Package overridden via config.platform, actual: 8.3.8
php-64bit               8.3       Package overridden via config.platform, actual: 8.3.8
php-ipv6                8.3       Package overridden via config.platform, actual: 8.3.8

Steps to reproduce the issue.

# Insert the commands issued in the terminal if they are needed
# to reproduce the issue. Otherwise, delete this code block.
// Insert the PHP code to reproduce the issue. Please ensure that it is code that
// can be copy pasted to reproduce it.

Error message/Stack trace

[#638 New error: Kreait\Firebase\Exception\RuntimeException: Unable to determine the Firebase Project ID]

Additional information

I couldn't see in the docs how to use the credentials array, other than it says they are supported so I think this is an oversight rather than intentional, the code provided should resolve the issue.

chadanuk commented 2 months ago

Ah, we were importing an older version of the code. Closing this now. Thanks

jeromegamez commented 2 months ago

These are my favorite issues 😂, glad you found the solution! 🙌🏻