Skip to content

Commit

Permalink
Add support for setting additional options when creating the Firestor…
Browse files Browse the repository at this point in the history
…e component
  • Loading branch information
jeromegamez committed Nov 25, 2023
1 parent de493e2 commit 81eb1d7
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 2 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ Please read about the future of the Firebase Admin PHP SDK on the

## [Unreleased]

### Added

* Added `Kreait\Firebase\Factory::withFirestoreClientConfig()` to support setting additional options when
creating the Firestore component.
* Added `Kreait\Firebase\Factory::withFirestoreDatabase()` to specify the database used when creating the Firestore
component.

## [7.7.0] - 2023-11-25

### Changed
Expand All @@ -26,7 +33,7 @@ Please read about the future of the Firebase Admin PHP SDK on the
}
}
```


## [7.6.0] - 2023-09-07

Expand Down
60 changes: 60 additions & 0 deletions docs/cloud-firestore.rst
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,63 @@ Getting started
``$database`` is an instance of ``Google\Cloud\Firestore\FirestoreClient``. Please refer to the links above for
guidance on how to proceed from here.

******************************
Use another Firestore Database
******************************

If you don't specify a database, the Firestore Client will connect to the ``(default)`` database.

If you want to connect to another database, you can specify its name with the factory. You can work with multiple
Firestore Databases simultaneously.

.. code-block:: php
use Kreait\Firebase\Factory;
$factory = new Factory();
$defaultDatabase = $factory
->createFirestore()
->database();
$otherDatabase = $factory
->withFirestoreDatabase('another-database')
->createFirestore()
->database();
$thirdDatabase = $factory
->withFirestoreDatabase('third-database')
->createFirestore()
->database();
***********************************
Add Firestore configuration options
***********************************

You can add additional configuration options for the Firestore Client used by the Firestore component:

.. code-block:: php
use Kreait\Firebase\Factory;
$factory = new Factory();
$firestore = $factory
->withFirestoreClientConfig([...])
->createFirestore();
You can find all configuration options in the source code of the ``FirestoreClient`` class of the
`official Google Firestore PHP library <https://github.com/googleapis/google-cloud-php-firestore/blob/4186f2a2f2a8bdaedf19376a35ccb0ffad17f4e1/src/FirestoreClient.php#L138>`_.

In fact, the ``withFirestoreDatabase()`` method is a shortcut for the ``withFirestoreClientConfig()`` method:

.. code-block:: php
use Kreait\Firebase\Factory;
$factory = new Factory();
$firestore = $factory->->withFirestoreDatabase('another-database');
// is a shortcut for
$firestore = $factory->withFirestoreClientConfig(['database' => 'another-database]);
28 changes: 27 additions & 1 deletion src/Firebase/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ final class Factory
private HttpFactory $httpFactory;
private HttpClientOptions $httpClientOptions;

/**
* @var array<non-empty-string, mixed>
*/
private array $firestoreClientConfig = [];

public function __construct()
{
$this->clock = SystemClock::create();
Expand Down Expand Up @@ -228,6 +233,25 @@ public function withDatabaseAuthVariableOverride(?array $override): self
return $factory;
}

/**
* @param non-empty-string $database
*/
public function withFirestoreDatabase(string $database): self
{
return $this->withFirestoreClientConfig(['database' => $database]);
}

/**
* @param array<non-empty-string, mixed> $config
*/
public function withFirestoreClientConfig(array $config): self
{
$factory = clone $this;
$factory->firestoreClientConfig = array_merge($this->firestoreClientConfig, $config);

return $factory;
}

/**
* @param non-empty-string $name
*/
Expand Down Expand Up @@ -438,8 +462,10 @@ public function createDynamicLinksService($defaultDynamicLinksDomain = null): Co

public function createFirestore(): Contract\Firestore
{
$config = $this->googleCloudClientConfig() + $this->firestoreClientConfig;

try {
$firestoreClient = new FirestoreClient($this->googleCloudClientConfig());
$firestoreClient = new FirestoreClient($config);
} catch (Throwable $e) {
throw new RuntimeException('Unable to create a FirestoreClient: '.$e->getMessage(), $e->getCode(), $e);
}
Expand Down
26 changes: 26 additions & 0 deletions tests/Integration/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,30 @@ public function itSupportsAddingAdditionalHttpClientMiddlewares(): void

$this->assertTrue($check);
}

#[Test]
public function itSupportsOverridingTheDefaultFirestoreDatabase(): void
{
$firestore = self::$factory
->withFirestoreDatabase(__FUNCTION__)
->createFirestore();

$db = $firestore->database();
$name = $db->collection('irrelevant')->name();

$this->assertStringContainsString(__FUNCTION__, $name);
}

#[Test]
public function itSupportsAdditionalFirestoreConfig(): void
{
$firestore = self::$factory
->withFirestoreClientConfig(['database' => __FUNCTION__])
->createFirestore();

$db = $firestore->database();
$name = $db->collection('irrelevant')->name();

$this->assertStringContainsString(__FUNCTION__, $name);
}
}

0 comments on commit 81eb1d7

Please sign in to comment.