diff --git a/config/firebase.php b/config/firebase.php index bf33b63..10118ee 100644 --- a/config/firebase.php +++ b/config/firebase.php @@ -16,7 +16,6 @@ * ------------------------------------------------------------------------ */ 'projects' => [ - 'app' => [ /* * ------------------------------------------------------------------------ @@ -47,7 +46,6 @@ * first time you try to access a component of the Firebase Admin SDK. * */ - 'credentials' => env('FIREBASE_CREDENTIALS', env('GOOGLE_APPLICATION_CREDENTIALS')), /* @@ -93,7 +91,6 @@ ], 'dynamic_links' => [ - /* * Dynamic links can be built with any URL prefix registered on * @@ -105,9 +102,7 @@ * The value must be a valid domain, for example, * https://example.page.link */ - 'default_domain' => env('FIREBASE_DYNAMIC_LINKS_DEFAULT_DOMAIN'), - ], /* @@ -117,7 +112,6 @@ */ 'storage' => [ - /* * Your project's default storage bucket usually uses the project ID * as its name. If you have multiple storage buckets and want to @@ -126,7 +120,6 @@ */ 'default_bucket' => env('FIREBASE_STORAGE_DEFAULT_BUCKET'), - ], /* @@ -185,7 +178,7 @@ */ 'timeout' => env('FIREBASE_HTTP_CLIENT_TIMEOUT'), - // 'guzzle_middlewares' => [], + 'guzzle_middlewares' => [], ], ], ], diff --git a/src/FirebaseProjectManager.php b/src/FirebaseProjectManager.php index 8845abd..3a0dbe8 100644 --- a/src/FirebaseProjectManager.php +++ b/src/FirebaseProjectManager.php @@ -6,7 +6,6 @@ use Illuminate\Contracts\Container\Container; use Illuminate\Contracts\Foundation\Application; -use Illuminate\Support\Arr; use Kreait\Firebase\Exception\InvalidArgumentException; use Kreait\Firebase\Factory; use Kreait\Firebase\Http\HttpClientOptions; @@ -48,7 +47,7 @@ protected function configuration(string $name): array return $config; } - protected function resolveCredentials(string $credentials): string + protected function resolveJsonCredentials(string $credentials): string { $isJsonString = \str_starts_with($credentials, '{'); $isAbsoluteLinuxPath = \str_starts_with($credentials, '/'); @@ -65,33 +64,31 @@ protected function configure(string $name): FirebaseProject $config = $this->configuration($name); - if ($tenantId = Arr::get($config, 'auth.tenant_id')) { + if ($tenantId = $config['auth']['tenant_id'] ?? null) { $factory = $factory->withTenantId($tenantId); } - if ($credentials = Arr::get($config, 'credentials.file', Arr::get($config, 'credentials'))) { + if ($credentials = $config['credentials']['file'] ?? ($config['credentials'] ?? null)) { if (is_string($credentials)) { - $factory = $factory->withServiceAccount($this->resolveCredentials($credentials)); + $credentials = $this->resolveJsonCredentials($credentials); } - if (is_array($credentials) && Arr::has($credentials, ['type', 'project_id'])) { - $factory = $factory->withServiceAccount($credentials); - } + $factory = $factory->withServiceAccount($credentials); } - if ($databaseUrl = Arr::get($config, 'database.url')) { + if ($databaseUrl = $config['database']['url'] ?? null) { $factory = $factory->withDatabaseUri($databaseUrl); } - if ($authVariableOverride = Arr::get($config, 'database.auth_variable_override')) { + if ($authVariableOverride = $config['database']['auth_variable_override'] ?? null) { $factory = $factory->withDatabaseAuthVariableOverride($authVariableOverride); } - if ($defaultStorageBucket = Arr::get($config, 'storage.default_bucket')) { + if ($defaultStorageBucket = $config['storage']['default_bucket'] ?? null) { $factory = $factory->withDefaultStorageBucket($defaultStorageBucket); } - if ($cacheStore = Arr::get($config, 'cache_store')) { + if ($cacheStore = $config['cache_store'] ?? null) { $cache = $this->app->make('cache')->store($cacheStore); if ($cache instanceof CacheInterface) { @@ -100,16 +97,18 @@ protected function configure(string $name): FirebaseProject throw new InvalidArgumentException('The cache store must be an instance of a PSR-6 or PSR-16 cache'); } - $factory = $factory->withVerifierCache($cache)->withAuthTokenCache($cache); + $factory = $factory + ->withVerifierCache($cache) + ->withAuthTokenCache($cache); } - if ($logChannel = Arr::get($config, 'logging.http_log_channel')) { + if ($logChannel = $config['logging']['http_log_channel'] ?? null) { $factory = $factory->withHttpLogger( $this->app->make('log')->channel($logChannel) ); } - if ($logChannel = Arr::get($config, 'logging.http_debug_log_channel')) { + if ($logChannel = $config['logging']['http_debug_log_channel'] ?? null) { $factory = $factory->withHttpDebugLogger( $this->app->make('log')->channel($logChannel) ); @@ -117,15 +116,15 @@ protected function configure(string $name): FirebaseProject $options = HttpClientOptions::default(); - if ($proxy = Arr::get($config, 'http_client_options.proxy')) { + if ($proxy = $config['http_client_options']['proxy'] ?? null) { $options = $options->withProxy($proxy); } - if ($timeout = Arr::get($config, 'http_client_options.timeout')) { + if ($timeout = $config['http_client_options']['timeout'] ?? null) { $options = $options->withTimeOut((float) $timeout); } - if ($middlewares = Arr::get($config, 'http_client_options.guzzle_middlewares')) { + if ($middlewares = $config['http_client_options']['guzzle_middlewares'] ?? null) { $options = $options->withGuzzleMiddlewares($middlewares); } diff --git a/tests/FirebaseProjectManagerTest.php b/tests/FirebaseProjectManagerTest.php index 54963b2..9e0d07d 100644 --- a/tests/FirebaseProjectManagerTest.php +++ b/tests/FirebaseProjectManagerTest.php @@ -84,6 +84,27 @@ public function credentials_can_be_configured_using_a_json_file(): void $this->assertSame($credentials, $serviceAccount); } + /** + * @test + */ + public function json_file_credentials_can_be_used_using_the_deprecated_configuration_entry(): void + { + // Reference credentials + $credentialsPath = \realpath(__DIR__ . '/_fixtures/service_account.json'); + $credentials = \json_decode(\file_get_contents($credentialsPath), true); + + // Set configuration and retrieve project + $projectName = 'app'; + $this->app->config->set('firebase.projects.' . $projectName . '.credentials.file', \realpath(__DIR__ . '/_fixtures/service_account.json')); + $factory = $this->factoryForProject($projectName); + + // Retrieve service account + $serviceAccount = $this->getAccessibleProperty($factory, 'serviceAccount')->getValue($factory); + + // Validate value + $this->assertSame($credentials, $serviceAccount); + } + /** * @test */