|
4 | 4 |
|
5 | 5 | namespace Kreait\Laravel\Firebase;
|
6 | 6 |
|
7 |
| -use Illuminate\Contracts\Foundation\Application; |
| 7 | +use Illuminate\Contracts\Container\Container; |
| 8 | +use Laravel\Lumen\Application as Lumen; |
8 | 9 | use Kreait\Firebase;
|
9 | 10 |
|
10 | 11 | final class ServiceProvider extends \Illuminate\Support\ServiceProvider
|
11 | 12 | {
|
12 | 13 | public function boot()
|
13 | 14 | {
|
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; |
18 | 17 | }
|
| 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'); |
19 | 26 | }
|
20 | 27 |
|
21 | 28 | public function register()
|
22 | 29 | {
|
| 30 | + if ($this->app instanceof Lumen) { |
| 31 | + $this->app->configure('firebase'); |
| 32 | + } |
| 33 | + |
23 | 34 | $this->mergeConfigFrom(__DIR__.'/../config/firebase.php', 'firebase');
|
24 | 35 |
|
25 | 36 | $this->registerComponents();
|
26 | 37 | }
|
27 | 38 |
|
28 | 39 | private function registerComponents()
|
29 | 40 | {
|
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(); |
60 | 42 |
|
61 |
| - $this->app->singleton(Firebase\Auth::class, static function (Application $app) { |
| 43 | + $this->app->singleton(Firebase\Auth::class, static function (Container $app) { |
62 | 44 | return $app->make(Firebase\Factory::class)->createAuth();
|
63 | 45 | });
|
64 | 46 | $this->app->alias(Firebase\Auth::class, 'firebase.auth');
|
65 | 47 |
|
66 |
| - $this->app->singleton(Firebase\Database::class, static function (Application $app) { |
| 48 | + $this->app->singleton(Firebase\Database::class, static function (Container $app) { |
67 | 49 | return $app->make(Firebase\Factory::class)->createDatabase();
|
68 | 50 | });
|
69 | 51 | $this->app->alias(Firebase\Database::class, 'firebase.database');
|
70 | 52 |
|
71 |
| - $this->app->singleton(Firebase\DynamicLinks::class, static function (Application $app) { |
| 53 | + $this->app->singleton(Firebase\DynamicLinks::class, static function (Container $app) { |
72 | 54 | $defaultDynamicLinksDomain = $app->make('config')['firebase']['dynamic_links']['default_domain'] ?? null;
|
73 | 55 |
|
74 | 56 | return $app->make(Firebase\Factory::class)->createDynamicLinksService($defaultDynamicLinksDomain);
|
75 | 57 | });
|
76 | 58 | $this->app->alias(Firebase\DynamicLinks::class, 'firebase.dynamic_links');
|
77 | 59 |
|
78 |
| - $this->app->singleton(Firebase\Firestore::class, static function (Application $app) { |
| 60 | + $this->app->singleton(Firebase\Firestore::class, static function (Container $app) { |
79 | 61 | return $app->make(Firebase\Factory::class)->createFirestore();
|
80 | 62 | });
|
81 | 63 | $this->app->alias(Firebase\Firestore::class, 'firebase.firestore');
|
82 | 64 |
|
83 |
| - $this->app->singleton(Firebase\Messaging::class, static function (Application $app) { |
| 65 | + $this->app->singleton(Firebase\Messaging::class, static function (Container $app) { |
84 | 66 | return $app->make(Firebase\Factory::class)->createMessaging();
|
85 | 67 | });
|
86 | 68 | $this->app->alias(Firebase\Messaging::class, 'firebase.messaging');
|
87 | 69 |
|
88 |
| - $this->app->singleton(Firebase\RemoteConfig::class, static function (Application $app) { |
| 70 | + $this->app->singleton(Firebase\RemoteConfig::class, static function (Container $app) { |
89 | 71 | return $app->make(Firebase\Factory::class)->createRemoteConfig();
|
90 | 72 | });
|
91 | 73 | $this->app->alias(Firebase\RemoteConfig::class, 'firebase.remote_config');
|
92 | 74 |
|
93 |
| - $this->app->singleton(Firebase\Storage::class, static function (Application $app) { |
| 75 | + $this->app->singleton(Firebase\Storage::class, static function (Container $app) { |
94 | 76 | return $app->make(Firebase\Factory::class)->createStorage();
|
95 | 77 | });
|
96 | 78 | $this->app->alias(Firebase\Storage::class, 'firebase.storage');
|
97 | 79 | }
|
| 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 | + } |
98 | 114 | }
|
0 commit comments