Skip to content

Commit 7fdf330

Browse files
committed
Add support for Lumen
1 parent bd90a20 commit 7fdf330

File tree

4 files changed

+87
-49
lines changed

4 files changed

+87
-49
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
* Added a notice about not using the factory pattern described in the SDK documentation when using this package.
66
(Although not a code change, adding it in the changelog to enhance visibility)
7+
* Added support for [Lumen](https://lumen.laravel.com/)
8+
* Updated `kreait/firebase-php` to `^4.38.1`
79

810
## 1.2.0 - 2019-10-26
911

README.md

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
A Laravel package for the [Firebase PHP Admin SDK](https://github.com/kreait/firebase-php).
44

55
[![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.32.0-blue)](https://packagist.org/packages/kreait/firebase-php)
6+
[![Firebase Admin SDK version](https://img.shields.io/badge/Firebase%20Admin%20SDK-%5E4.35.0-blue)](https://packagist.org/packages/kreait/firebase-php)
77
![Supported Laravel versions](https://img.shields.io/badge/Laravel-%3E%3D5.8-blue)
8+
![Supported Lumen versions](https://img.shields.io/badge/Lumen-%3E%3D5.8-blue)
89
[![Discord](https://img.shields.io/discord/523866370778333184.svg?color=7289da&logo=discord)](https://discord.gg/nbgVfty)
910

1011
* [Installation](#installation)
@@ -14,18 +15,20 @@ A Laravel package for the [Firebase PHP Admin SDK](https://github.com/kreait/fir
1415

1516
## Installation
1617

17-
This package requires Laravel 5.8 and higher.
18+
This package requires Laravel 5.8 and higher or Lumen 5.8 and higher.
1819

1920
```bash
2021
composer require kreait/laravel-firebase
2122
```
2223

23-
If you don't use package auto-discovery, add the following service provider in `config/app.php`
24+
If you use Lumen or don't use Laravel's package auto-discovery, add the following service provider in
25+
`config/app.php` (Laravel) or `bootstrap/app.php` (Lumen):
26+
27+
### Laravel
2428

2529
```php
26-
// config/app.php
2730
<?php
28-
31+
// config/app.php
2932
return [
3033
// ...
3134
'providers' => [
@@ -36,6 +39,15 @@ return [
3639
];
3740
```
3841

42+
### Lumen
43+
44+
```php
45+
<?php
46+
// bootstrap/app.php
47+
48+
$app->register(Kreait\Laravel\Firebase\ServiceProvider::class);
49+
```
50+
3951
## Configuration
4052

4153
In order to access a Firebase project and its related services using a server SDK, requests must be authenticated.
@@ -57,10 +69,15 @@ FIREBASE_CREDENTIALS=relative/path/to/firebase_credentials.json
5769
```
5870

5971
For further configuration, please see [config/firebase.php](config/firebase.php). You can modify the configuration
60-
by copying it to your local `config` directory with the publish command:
72+
by copying it to your local `config` directory:
6173

6274
```bash
75+
# Laravel
6376
php artisan vendor:publish --provider="Kreait\Laravel\Firebase\ServiceProvider" --tag=config
77+
78+
# Lumen
79+
mkdir -p config
80+
cp vendor/kreait/firebase-laravel/config/firebase.php config/firebase.php
6481
```
6582

6683
## Usage

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@
1111
}
1212
],
1313
"require": {
14-
"kreait/firebase-php": "^4.35.0",
14+
"kreait/firebase-php": "^4.38.1",
1515
"illuminate/contracts": "^5.8|^6.0",
1616
"illuminate/support": "^5.8|^6.0"
1717
},
18+
"require-dev": {
19+
"laravel/lumen-framework": "^5.8|^6.0"
20+
},
1821
"autoload": {
1922
"psr-4": {
2023
"Kreait\\Laravel\\Firebase\\": "src"

src/ServiceProvider.php

Lines changed: 58 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,95 +4,111 @@
44

55
namespace Kreait\Laravel\Firebase;
66

7-
use Illuminate\Contracts\Foundation\Application;
7+
use Illuminate\Contracts\Container\Container;
8+
use Laravel\Lumen\Application as Lumen;
89
use Kreait\Firebase;
910

1011
final class ServiceProvider extends \Illuminate\Support\ServiceProvider
1112
{
1213
public function boot()
1314
{
14-
if ($this->app->runningInConsole()) {
15-
$this->publishes([
16-
__DIR__ . '/../config/firebase.php' => $this->app->configPath('firebase.php'),
17-
], 'config');
15+
if (!$this->app->runningInConsole()) {
16+
return;
1817
}
18+
19+
if ($this->app instanceof Lumen) {
20+
return;
21+
}
22+
23+
$this->publishes([
24+
__DIR__ . '/../config/firebase.php' => $this->app->configPath('firebase.php'),
25+
], 'config');
1926
}
2027

2128
public function register()
2229
{
30+
if ($this->app instanceof Lumen) {
31+
$this->app->configure('firebase');
32+
}
33+
2334
$this->mergeConfigFrom(__DIR__.'/../config/firebase.php', 'firebase');
2435

2536
$this->registerComponents();
2637
}
2738

2839
private function registerComponents()
2940
{
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-
$factory = $factory->withServiceAccount((string) $credentialsFile);
37-
}
38-
39-
$enableAutoDiscovery = $config['credentials']['auto_discovery'] ?? true;
40-
if (!$enableAutoDiscovery) {
41-
$factory = $factory->withDisabledAutoDiscovery();
42-
}
43-
44-
if ($databaseUrl = $config['database']['url'] ?? null) {
45-
$factory = $factory->withDatabaseUri($databaseUrl);
46-
}
47-
48-
if ($defaultStorageBucket = $config['storage']['default_bucket'] ?? null) {
49-
$factory = $factory->withDefaultStorageBucket($defaultStorageBucket);
50-
}
51-
52-
if ($cacheStore = $config['cache_store'] ?? null) {
53-
$factory = $factory->withVerifierCache(
54-
$app->make('cache')->store($cacheStore)
55-
);
56-
}
57-
58-
return $factory;
59-
});
41+
$this->registerFactory();
6042

61-
$this->app->singleton(Firebase\Auth::class, static function (Application $app) {
43+
$this->app->singleton(Firebase\Auth::class, static function (Container $app) {
6244
return $app->make(Firebase\Factory::class)->createAuth();
6345
});
6446
$this->app->alias(Firebase\Auth::class, 'firebase.auth');
6547

66-
$this->app->singleton(Firebase\Database::class, static function (Application $app) {
48+
$this->app->singleton(Firebase\Database::class, static function (Container $app) {
6749
return $app->make(Firebase\Factory::class)->createDatabase();
6850
});
6951
$this->app->alias(Firebase\Database::class, 'firebase.database');
7052

71-
$this->app->singleton(Firebase\DynamicLinks::class, static function (Application $app) {
53+
$this->app->singleton(Firebase\DynamicLinks::class, static function (Container $app) {
7254
$defaultDynamicLinksDomain = $app->make('config')['firebase']['dynamic_links']['default_domain'] ?? null;
7355

7456
return $app->make(Firebase\Factory::class)->createDynamicLinksService($defaultDynamicLinksDomain);
7557
});
7658
$this->app->alias(Firebase\DynamicLinks::class, 'firebase.dynamic_links');
7759

78-
$this->app->singleton(Firebase\Firestore::class, static function (Application $app) {
60+
$this->app->singleton(Firebase\Firestore::class, static function (Container $app) {
7961
return $app->make(Firebase\Factory::class)->createFirestore();
8062
});
8163
$this->app->alias(Firebase\Firestore::class, 'firebase.firestore');
8264

83-
$this->app->singleton(Firebase\Messaging::class, static function (Application $app) {
65+
$this->app->singleton(Firebase\Messaging::class, static function (Container $app) {
8466
return $app->make(Firebase\Factory::class)->createMessaging();
8567
});
8668
$this->app->alias(Firebase\Messaging::class, 'firebase.messaging');
8769

88-
$this->app->singleton(Firebase\RemoteConfig::class, static function (Application $app) {
70+
$this->app->singleton(Firebase\RemoteConfig::class, static function (Container $app) {
8971
return $app->make(Firebase\Factory::class)->createRemoteConfig();
9072
});
9173
$this->app->alias(Firebase\RemoteConfig::class, 'firebase.remote_config');
9274

93-
$this->app->singleton(Firebase\Storage::class, static function (Application $app) {
75+
$this->app->singleton(Firebase\Storage::class, static function (Container $app) {
9476
return $app->make(Firebase\Factory::class)->createStorage();
9577
});
9678
$this->app->alias(Firebase\Storage::class, 'firebase.storage');
9779
}
80+
81+
private function registerFactory()
82+
{
83+
$this->app->singleton(Firebase\Factory::class, static function (Container $app) {
84+
$factory = new Firebase\Factory();
85+
86+
$config = $app->make('config')['firebase'];
87+
88+
if ($credentialsFile = $config['credentials']['file'] ?? null) {
89+
$factory = $factory->withServiceAccount((string) $credentialsFile);
90+
}
91+
92+
$enableAutoDiscovery = $config['credentials']['auto_discovery'] ?? true;
93+
if (!$enableAutoDiscovery) {
94+
$factory = $factory->withDisabledAutoDiscovery();
95+
}
96+
97+
if ($databaseUrl = $config['database']['url'] ?? null) {
98+
$factory = $factory->withDatabaseUri($databaseUrl);
99+
}
100+
101+
if ($defaultStorageBucket = $config['storage']['default_bucket'] ?? null) {
102+
$factory = $factory->withDefaultStorageBucket($defaultStorageBucket);
103+
}
104+
105+
if ($cacheStore = $config['cache_store'] ?? null) {
106+
$factory = $factory->withVerifierCache(
107+
$app->make('cache')->store($cacheStore)
108+
);
109+
}
110+
111+
return $factory;
112+
});
113+
}
98114
}

0 commit comments

Comments
 (0)