Skip to content

Commit

Permalink
Add support for Lumen
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromegamez committed Jan 15, 2020
1 parent bd90a20 commit 7fdf330
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 49 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

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

## 1.2.0 - 2019-10-26

Expand Down
29 changes: 23 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
A Laravel package for the [Firebase PHP Admin SDK](https://github.com/kreait/firebase-php).

[![Current version](https://img.shields.io/packagist/v/kreait/laravel-firebase.svg?logo=composer)](https://packagist.org/packages/kreait/laravel-firebase)
[![Firebase Admin SDK version](https://img.shields.io/badge/Firebase%20Admin%20SDK-%5E4.32.0-blue)](https://packagist.org/packages/kreait/firebase-php)
[![Firebase Admin SDK version](https://img.shields.io/badge/Firebase%20Admin%20SDK-%5E4.35.0-blue)](https://packagist.org/packages/kreait/firebase-php)
![Supported Laravel versions](https://img.shields.io/badge/Laravel-%3E%3D5.8-blue)
![Supported Lumen versions](https://img.shields.io/badge/Lumen-%3E%3D5.8-blue)
[![Discord](https://img.shields.io/discord/523866370778333184.svg?color=7289da&logo=discord)](https://discord.gg/nbgVfty)

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

## Installation

This package requires Laravel 5.8 and higher.
This package requires Laravel 5.8 and higher or Lumen 5.8 and higher.

```bash
composer require kreait/laravel-firebase
```

If you don't use package auto-discovery, add the following service provider in `config/app.php`
If you use Lumen or don't use Laravel's package auto-discovery, add the following service provider in
`config/app.php` (Laravel) or `bootstrap/app.php` (Lumen):

### Laravel

```php
// config/app.php
<?php

// config/app.php
return [
// ...
'providers' => [
Expand All @@ -36,6 +39,15 @@ return [
];
```

### Lumen

```php
<?php
// bootstrap/app.php

$app->register(Kreait\Laravel\Firebase\ServiceProvider::class);
```

## Configuration

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

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

```bash
# Laravel
php artisan vendor:publish --provider="Kreait\Laravel\Firebase\ServiceProvider" --tag=config

# Lumen
mkdir -p config
cp vendor/kreait/firebase-laravel/config/firebase.php config/firebase.php
```

## Usage
Expand Down
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@
}
],
"require": {
"kreait/firebase-php": "^4.35.0",
"kreait/firebase-php": "^4.38.1",
"illuminate/contracts": "^5.8|^6.0",
"illuminate/support": "^5.8|^6.0"
},
"require-dev": {
"laravel/lumen-framework": "^5.8|^6.0"
},
"autoload": {
"psr-4": {
"Kreait\\Laravel\\Firebase\\": "src"
Expand Down
100 changes: 58 additions & 42 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,95 +4,111 @@

namespace Kreait\Laravel\Firebase;

use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Container\Container;
use Laravel\Lumen\Application as Lumen;
use Kreait\Firebase;

final class ServiceProvider extends \Illuminate\Support\ServiceProvider
{
public function boot()
{
if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__ . '/../config/firebase.php' => $this->app->configPath('firebase.php'),
], 'config');
if (!$this->app->runningInConsole()) {
return;
}

if ($this->app instanceof Lumen) {
return;
}

$this->publishes([
__DIR__ . '/../config/firebase.php' => $this->app->configPath('firebase.php'),
], 'config');
}

public function register()
{
if ($this->app instanceof Lumen) {
$this->app->configure('firebase');
}

$this->mergeConfigFrom(__DIR__.'/../config/firebase.php', 'firebase');

$this->registerComponents();
}

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

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

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

$enableAutoDiscovery = $config['credentials']['auto_discovery'] ?? true;
if (!$enableAutoDiscovery) {
$factory = $factory->withDisabledAutoDiscovery();
}

if ($databaseUrl = $config['database']['url'] ?? null) {
$factory = $factory->withDatabaseUri($databaseUrl);
}

if ($defaultStorageBucket = $config['storage']['default_bucket'] ?? null) {
$factory = $factory->withDefaultStorageBucket($defaultStorageBucket);
}

if ($cacheStore = $config['cache_store'] ?? null) {
$factory = $factory->withVerifierCache(
$app->make('cache')->store($cacheStore)
);
}

return $factory;
});
$this->registerFactory();

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

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

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

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

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

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

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

$this->app->singleton(Firebase\Storage::class, static function (Application $app) {
$this->app->singleton(Firebase\Storage::class, static function (Container $app) {
return $app->make(Firebase\Factory::class)->createStorage();
});
$this->app->alias(Firebase\Storage::class, 'firebase.storage');
}

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

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

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

$enableAutoDiscovery = $config['credentials']['auto_discovery'] ?? true;
if (!$enableAutoDiscovery) {
$factory = $factory->withDisabledAutoDiscovery();
}

if ($databaseUrl = $config['database']['url'] ?? null) {
$factory = $factory->withDatabaseUri($databaseUrl);
}

if ($defaultStorageBucket = $config['storage']['default_bucket'] ?? null) {
$factory = $factory->withDefaultStorageBucket($defaultStorageBucket);
}

if ($cacheStore = $config['cache_store'] ?? null) {
$factory = $factory->withVerifierCache(
$app->make('cache')->store($cacheStore)
);
}

return $factory;
});
}
}

0 comments on commit 7fdf330

Please sign in to comment.