4
4
5
5
namespace Kreait \Laravel \Firebase ;
6
6
7
- use Illuminate \Contracts \Container \ Container ;
7
+ use Illuminate \Contracts \Config \ Repository as ConfigRepository ;
8
8
use Illuminate \Contracts \Foundation \Application ;
9
9
use Kreait \Firebase \Exception \InvalidArgumentException ;
10
10
use Kreait \Firebase \Factory ;
11
11
use Kreait \Firebase \Http \HttpClientOptions ;
12
12
use Psr \SimpleCache \CacheInterface ;
13
13
use Symfony \Component \Cache \Adapter \Psr16Adapter ;
14
14
15
+ use function str_contains ;
16
+ use function str_starts_with ;
17
+
15
18
class FirebaseProjectManager
16
19
{
17
- /** @var Application */
18
- protected $ app ;
19
-
20
20
/** @var FirebaseProject[] */
21
21
protected array $ projects = [];
22
22
23
- public function __construct (Container $ app )
24
- {
25
- $ this -> app = $ app ;
26
- }
23
+ public function __construct (
24
+ protected Application $ app ,
25
+ protected ConfigRepository $ config ,
26
+ ) { }
27
27
28
28
public function project (?string $ name = null ): FirebaseProject
29
29
{
30
30
$ name = $ name ?? $ this ->getDefaultProject ();
31
31
32
- if (! isset ($ this ->projects [$ name ])) {
33
- $ this ->projects [$ name ] = $ this ->configure ($ name );
34
- }
35
-
36
- return $ this ->projects [$ name ];
32
+ return $ this ->projects [$ name ] ??= $ this ->configure ($ name );
37
33
}
38
34
39
35
protected function configuration (string $ name ): array
40
36
{
41
- $ config = $ this ->app -> config ->get ('firebase.projects. ' .$ name );
37
+ $ config = $ this ->config ->get ('firebase.projects. ' .$ name );
42
38
43
39
if (! $ config ) {
44
40
throw new InvalidArgumentException ("Firebase project [ {$ name }] not configured. " );
@@ -49,9 +45,9 @@ protected function configuration(string $name): array
49
45
50
46
protected function resolveJsonCredentials (string $ credentials ): string
51
47
{
52
- $ isJsonString = \ str_starts_with ($ credentials , '{ ' );
53
- $ isAbsoluteLinuxPath = \ str_starts_with ($ credentials , '/ ' );
54
- $ isAbsoluteWindowsPath = \ str_contains ($ credentials , ': \\' );
48
+ $ isJsonString = str_starts_with ($ credentials , '{ ' );
49
+ $ isAbsoluteLinuxPath = str_starts_with ($ credentials , '/ ' );
50
+ $ isAbsoluteWindowsPath = str_contains ($ credentials , ': \\' );
55
51
56
52
$ isRelativePath = ! $ isJsonString && ! $ isAbsoluteLinuxPath && ! $ isAbsoluteWindowsPath ;
57
53
@@ -60,7 +56,7 @@ protected function resolveJsonCredentials(string $credentials): string
60
56
61
57
protected function configure (string $ name ): FirebaseProject
62
58
{
63
- $ factory = new Factory ;
59
+ $ factory = $ this -> app -> make ( Factory::class) ;
64
60
65
61
$ config = $ this ->configuration ($ name );
66
62
@@ -84,10 +80,6 @@ protected function configure(string $name): FirebaseProject
84
80
$ factory = $ factory ->withDatabaseAuthVariableOverride ($ authVariableOverride );
85
81
}
86
82
87
- if ($ firestoreDatabase = $ config ['firestore ' ]['database ' ] ?? null ) {
88
- $ factory = $ factory ->withFirestoreDatabase ($ firestoreDatabase );
89
- }
90
-
91
83
if ($ defaultStorageBucket = $ config ['storage ' ]['default_bucket ' ] ?? null ) {
92
84
$ factory = $ factory ->withDefaultStorageBucket ($ defaultStorageBucket );
93
85
}
@@ -134,17 +126,21 @@ protected function configure(string $name): FirebaseProject
134
126
135
127
$ factory = $ factory ->withHttpClientOptions ($ options );
136
128
137
- return new FirebaseProject ($ factory , $ config );
129
+ return new FirebaseProject (
130
+ $ factory ,
131
+ $ config ['dynamic_links ' ]['default_domain ' ] ?? null ,
132
+ $ config ['firestore ' ]['database ' ] ?? null ,
133
+ );
138
134
}
139
135
140
136
public function getDefaultProject (): string
141
137
{
142
- return $ this ->app -> config ->get ('firebase.default ' );
138
+ return $ this ->config ->get ('firebase.default ' );
143
139
}
144
140
145
141
public function setDefaultProject (string $ name ): void
146
142
{
147
- $ this ->app -> config ->set ('firebase.default ' , $ name );
143
+ $ this ->config ->set ('firebase.default ' , $ name );
148
144
}
149
145
150
146
public function __call ($ method , $ parameters )
0 commit comments