Skip to content

Commit c6468ad

Browse files
committed
Initial commit
0 parents  commit c6468ad

File tree

8 files changed

+342
-0
lines changed

8 files changed

+342
-0
lines changed

.editorconfig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
insert_final_newline = true
7+
indent_style = space
8+
indent_size = 4
9+
trim_trailing_whitespace = true
10+
11+
[*.md]
12+
trim_trailing_whitespace = false
13+

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/build
2+
/vendor
3+
4+
/composer.lock
5+
/google-service-account.json

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# CHANGELOG
2+
3+
## Unreleased
4+
5+
* Initial release

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) Jérôme Gamez, https://github.com/jeromegamez <[email protected]>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# Firebase for Laravel
2+
3+
A Laravel package for the [Firebase PHP Admin SDK](https://github.com/kreait/firebase-php).
4+
5+
[![Current version](https://img.shields.io/packagist/v/kreait/laravel-firebase.svg?logo=composer)](https://packagist.org/packages/kreait/laravel-firebase)
6+
[![Firebase Admin SDK version](https://img.shields.io/badge/Firebase%20Admin%20SDK-%5E4.30.0-blue)](https://packagist.org/packages/kreait/firebase-php)
7+
[![Discord](https://img.shields.io/discord/523866370778333184.svg?color=7289da&logo=discord)](https://discord.gg/nbgVfty)
8+
9+
* [Installation](#installation)
10+
* [Configuration](#configuration)
11+
* [Usage](#usage)
12+
* [Documentation](https://firebase-php.readthedocs.io/)
13+
14+
## Installation
15+
16+
```bash
17+
composer require kreait/laravel-firebase ^1.0@dev
18+
```
19+
20+
If you don't use package auto-discovery, add the following service provider in `config/app.php`
21+
22+
```php
23+
// config/app.php
24+
<?php
25+
26+
return [
27+
// ...
28+
'providers' => [
29+
// ...
30+
Kreait\Laravel\Firebase\ServiceProvider::class
31+
]
32+
// ...
33+
];
34+
```
35+
36+
## Configuration
37+
38+
In order to access a Firebase project and its related services using a server SDK, requests must be authenticated.
39+
For server-to-server communication this is done with a Service Account.
40+
41+
The package uses auto discovery to find the credentials needed for authenticating requests to the Firebase APIs
42+
by inspecting certain environment variables and looking into Google's well known path(s).
43+
44+
If you don't already have generated a Service Account, you can do so by following the instructions from the
45+
official documentation pages at https://firebase.google.com/docs/admin/setup#initialize_the_sdk.
46+
47+
Once you have downloaded the Service Account JSON file, you can use it to configure the package by specifying
48+
the environment variable `FIREBASE_CREDENTIALS` in your `.env` file:
49+
50+
```
51+
FIREBASE_CREDENTIALS=/full/path/to/firebase_credentials.json
52+
# or
53+
FIREBASE_CREDENTIALS=relative/path/to/firebase_credentials.json
54+
```
55+
56+
For further configuration, please see [config/firebase.php](config/firebase.php). You can modify the configuration
57+
by copying it to your local `config` directory with the publish command:
58+
59+
```bash
60+
php artisan vendor:publish --provider="Kreait\Laravel\Firebase\ServiceProvider" --tag=config
61+
```
62+
63+
## Usage
64+
65+
| Component (links to docs) | [Automatic Injection](https://laravel.com/docs/5.8/container#automatic-injection) | [`app()`](https://laravel.com/docs/helpers#method-app) |
66+
| --- | --- | --- |
67+
| [Authentication](https://firebase-php.readthedocs.io/en/latest/authentication.html) | `\Kreait\Firebase\Auth` | `app('firebase.auth')` |
68+
| [Cloud Messaging (FCM)](https://firebase-php.readthedocs.io/en/latest/cloud-messaging.html) | `\Kreait\Firebase\Messaging` | `app('firebase.messaging')` |
69+
| [Realtime Database](https://firebase-php.readthedocs.io/en/latest/realtime-database.html) | `\Kreait\Firebase\Database` | `app('firebase.database')` |
70+
| [Remote Config](https://firebase-php.readthedocs.io/en/latest/remote-config.html) | `\Kreait\Firebase\RemoteConfig` | `app('firebase.remote_config')` |
71+
| [Storage](https://firebase-php.readthedocs.io/en/latest/storage.html) | `\Kreait\Firebase\Storage` | `app('firebase.storage')` |
72+
73+
Once you have retrieved a component, please refer to the [documentation of the Firebase PHP Admin SDK](https://firebase-php.readthedocs.io)
74+
for further information on how to use it.
75+
76+
## License
77+
78+
The MIT License (MIT). Please see [License File](LICENSE) for more information.

composer.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "kreait/laravel-firebase",
3+
"description": "A Laravel package for the Firebase PHP Admin SDK",
4+
"keywords": ["laravel", "firebase", "fcm", "gcm"],
5+
"type": "library",
6+
"license": "MIT",
7+
"authors": [
8+
{
9+
"name": "Jérôme Gamez",
10+
"email": "[email protected]"
11+
}
12+
],
13+
"require": {
14+
"kreait/firebase-php": "^4.30"
15+
},
16+
"autoload": {
17+
"psr-4": {
18+
"Kreait\\Laravel\\Firebase\\": "src"
19+
}
20+
},
21+
"require-dev": {
22+
"laravel/framework": "^5.8"
23+
},
24+
"extra": {
25+
"branch-alias": {
26+
"dev-master": "1.0-dev"
27+
},
28+
"laravel": {
29+
"providers": [
30+
"Kreait\\Laravel\\Firebase\\ServiceProvider"
31+
]
32+
}
33+
}
34+
}

config/firebase.php

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
return [
4+
/**
5+
* ------------------------------------------------------------------------
6+
* Credentials / Service Account
7+
* ------------------------------------------------------------------------
8+
*
9+
* In order to access a Firebase project and its related services using a
10+
* server SDK, requests must be authenticated. For server-to-server
11+
* communication this is done with a Service Account.
12+
*
13+
* If you don't already have generated a Service Account, you can do so by
14+
* following the instructions from the official documentation pages at
15+
*
16+
* https://firebase.google.com/docs/admin/setup#initialize_the_sdk
17+
*
18+
* Once you have downloaded the Service Account JSON file, you can use it
19+
* to configure the package.
20+
*
21+
* If you don't provide credentials, the Firebase Admin SDK will try to
22+
* autodiscover them
23+
*
24+
* - by checking the environment variable FIREBASE_CREDENTIALS
25+
* - by checking the environment variable GOOGLE_APPLICATION_CREDENTIALS
26+
* - by trying to find Google's well known file
27+
* - by checking if the application is running on GCE/GCP
28+
*
29+
* If no credentials file can be found, an exception will be thrown the
30+
* first time you try to access a component of the Firebase Admin SDK.
31+
*
32+
*/
33+
'credentials' => [
34+
'file' => env('FIREBASE_CREDENTIALS'),
35+
36+
/**
37+
* If you want to prevent the auto discovery of credentials, set the
38+
* following parameter to false. If you disable it, you must
39+
* provide a credentials file.
40+
*/
41+
'auto_discovery' => true,
42+
],
43+
44+
/**
45+
* ------------------------------------------------------------------------
46+
* Firebase Realtime Database
47+
* ------------------------------------------------------------------------
48+
*/
49+
50+
'database' => [
51+
52+
/**
53+
* In most of the cases the project ID defined in the credentials file
54+
* determines the URL of your project's Realtime Database. If the
55+
* connection to the Realtime Database fails, you can override
56+
* its URL with the value you see at
57+
*
58+
* https://console.firebase.google.com/u/1/project/_/database
59+
*
60+
* Please make sure that you use a full URL like, for example,
61+
* https://my-project-id.firebaseio.com
62+
*/
63+
64+
'url' => env('FIREBASE_DATABASE_URL'),
65+
66+
],
67+
68+
/**
69+
* ------------------------------------------------------------------------
70+
* Firebase Cloud Storage
71+
* ------------------------------------------------------------------------
72+
*/
73+
74+
'storage' => [
75+
76+
/**
77+
* Your project's default storage bucket usually uses the project ID
78+
* as its name. If you have multiple storage buckets and want to
79+
* use another one as the default for your application, you can
80+
* override it here.
81+
*/
82+
83+
'default_bucket' => env('FIREBASE_STORAGE_DEFAULT_BUCKET'),
84+
85+
],
86+
87+
/**
88+
* ------------------------------------------------------------------------
89+
* Caching
90+
* ------------------------------------------------------------------------
91+
*
92+
* The Firebase Admin SDK can cache some data returned from the Firebase
93+
* API, for example Google's public keys used to verify ID tokens.
94+
*
95+
*/
96+
97+
'cache_store' => env('FIREBASE_CACHE_STORE', 'file'),
98+
99+
];

src/ServiceProvider.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Kreait\Laravel\Firebase;
6+
7+
use Illuminate\Contracts\Foundation\Application;
8+
use Kreait\Firebase;
9+
10+
final class ServiceProvider extends \Illuminate\Support\ServiceProvider
11+
{
12+
public function boot()
13+
{
14+
if ($this->app->runningInConsole()) {
15+
$this->publishes([
16+
__DIR__ . '/../config/firebase.php' => $this->app->configPath('firebase.php'),
17+
], 'config');
18+
}
19+
}
20+
21+
public function register()
22+
{
23+
$this->mergeConfigFrom(__DIR__.'/../config/firebase.php', 'firebase');
24+
25+
$this->registerComponents();
26+
}
27+
28+
private function registerComponents()
29+
{
30+
$this->app->singleton(Firebase\Factory::class, static function (Application $app) {
31+
$factory = new Firebase\Factory();
32+
33+
$config = $app->make('config')['firebase'];
34+
35+
if ($credentialsFile = $config['credentials']['file'] ?? null) {
36+
$serviceAccount = Firebase\ServiceAccount::fromJsonFile((string) $credentialsFile);
37+
$factory = $factory->withServiceAccount($serviceAccount);
38+
}
39+
40+
$enableAutoDiscovery = $config['credentials']['auto_discovery'] ?? true;
41+
if (!$enableAutoDiscovery) {
42+
$factory = $factory->withDisabledAutoDiscovery();
43+
}
44+
45+
if ($databaseUrl = $config['database']['url'] ?? null) {
46+
$factory = $factory->withDatabaseUri($databaseUrl);
47+
}
48+
49+
if ($defaultStorageBucket = $config['storage']['default_bucket'] ?? null) {
50+
$factory = $factory->withDefaultStorageBucket($defaultStorageBucket);
51+
}
52+
53+
if ($cacheStore = $config['cache_store'] ?? null) {
54+
$factory = $factory->withVerifierCache(
55+
$app->make('cache')->store($cacheStore)
56+
);
57+
}
58+
59+
return $factory;
60+
});
61+
62+
$this->app->singleton(Firebase\Auth::class, static function (Application $app) {
63+
return $app->make(Firebase\Factory::class)->createAuth();
64+
});
65+
$this->app->alias(Firebase\Auth::class, 'firebase.auth');
66+
67+
$this->app->singleton(Firebase\Database::class, static function (Application $app) {
68+
return $app->make(Firebase\Factory::class)->createDatabase();
69+
});
70+
$this->app->alias(Firebase\Database::class, 'firebase.database');
71+
72+
$this->app->singleton(Firebase\Messaging::class, static function (Application $app) {
73+
return $app->make(Firebase\Factory::class)->createMessaging();
74+
});
75+
$this->app->alias(Firebase\Messaging::class, 'firebase.messaging');
76+
77+
$this->app->singleton(Firebase\RemoteConfig::class, static function (Application $app) {
78+
return $app->make(Firebase\Factory::class)->createRemoteConfig();
79+
});
80+
$this->app->alias(Firebase\RemoteConfig::class, 'firebase.remote_config');
81+
82+
$this->app->singleton(Firebase\Storage::class, static function (Application $app) {
83+
return $app->make(Firebase\Factory::class)->createStorage();
84+
});
85+
$this->app->alias(Firebase\Storage::class, 'firebase.storage');
86+
}
87+
}

0 commit comments

Comments
 (0)