Skip to content

Commit

Permalink
Apply base_path() to relative file paths
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromegamez committed Feb 22, 2020
1 parent 3206b13 commit 3213acc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## Unreleased

* A relative path to a credentials file is now resolved with `base_path()` to address issues on Windows systems [#7](https://github.com/kreait/laravel-firebase/issues/7)

## 1.3.0 - 2020-01-15

* Added a notice about not using the factory pattern described in the SDK documentation when using this package.
Expand Down
19 changes: 16 additions & 3 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,15 @@ private function registerComponents()

private function registerFactory()
{
$this->app->singleton(Firebase\Factory::class, static function (Container $app) {
$this->app->singleton(Firebase\Factory::class, function (Container $app) {
$factory = new Firebase\Factory();

$config = $app->make('config')['firebase'];

if ($credentialsFile = $config['credentials']['file'] ?? null) {
$factory = $factory->withServiceAccount((string) $credentialsFile);
if ($credentials = $config['credentials']['file'] ?? null) {
$resolvedCredentials = $this->resolveCredentials((string) $credentials);

$factory = $factory->withServiceAccount($resolvedCredentials);
}

$enableAutoDiscovery = $config['credentials']['auto_discovery'] ?? true;
Expand All @@ -111,4 +113,15 @@ private function registerFactory()
return $factory;
});
}

private function resolveCredentials(string $credentials): string
{
$isJsonString = strpos($credentials, '{') === 0;
$isAbsoluteLinuxPath = strpos($credentials, '/') === 0;
$isAbsoluteWindowsPath = strpos($credentials, ':\\') !== false;

$isRelativePath = !$isJsonString && !$isAbsoluteLinuxPath && !$isAbsoluteWindowsPath;

return $isRelativePath ? $this->app->basePath($credentials) : $credentials;
}
}

0 comments on commit 3213acc

Please sign in to comment.