diff --git a/README.md b/README.md index 45cfba4..47cee05 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,7 @@ mongodb: clients: default: uri: '%env(MONGODB_URI)%' + default_database: #... uri_options: #... driver_options: #... ``` @@ -96,7 +97,8 @@ class MyService } ``` -If you register multiple clients, you can autowire them by name + `Client` suffix: +If you register multiple clients, you can autowire them by using the client name with a `Client` suffix as parameter +name: ```php use MongoDB\Bundle\Attribute\AutowireClient; @@ -105,6 +107,7 @@ use MongoDB\Client; class MyService { public function __construct( + // Will autowire the client with the id "second" private Client $secondClient, ) {} } @@ -119,7 +122,7 @@ use MongoDB\Client; class MyService { public function __construct( - #[AutowireClient('second')] + #[AutowireClient('second')] private Client $client, ) {} } @@ -127,8 +130,8 @@ class MyService ## Database and Collection Usage -The client service provides access to databases and collections. You can access a database by calling the `selectDatabase` -method, passing the database name and potential options: +The client service provides access to databases and collections. You can access a database by calling the +`selectDatabase` method, passing the database name and potential options: ```php use MongoDB\Client; @@ -146,7 +149,7 @@ class MyService } ``` -An alternative to this is using the `AutowireDatabase` attribute, referencing the database name: +An alternative to this is using the `#[AutowireDatabase]` attribute, referencing the database name: ```php use MongoDB\Bundle\Attribute\AutowireDatabase; @@ -161,21 +164,9 @@ class MyService } ``` -You can also omit the `database` option if the property name matches the database name. -In the following example the database name is `myDatabase`, inferred from the property name: - -```php -use MongoDB\Bundle\Attribute\AutowireCollection; -use MongoDB\Collection; - -class MyService -{ - public function __construct( - #[AutowireCollection()] - private Collection $myDatabase, - ) {} -} -``` +If you don't specify a database name in the attribute, the default database name (specified in the `default_database` +configuration option) will be used. If you did not define a default database, the database name has to be specified in +the attribute. If you have more than one client defined, you can also reference the client: @@ -193,7 +184,7 @@ class MyService ``` To inject a collection, you can either call the `selectCollection` method on a `Client` or `Database` instance. -For convenience, the `AutowireCollection` attribute provides a quicker alternative: +For convenience, the `#[AutowireCollection]` attribute provides a quicker alternative: ```php use MongoDB\Bundle\Attribute\AutowireCollection; @@ -230,6 +221,7 @@ class MyService ``` If you have more than one client defined, you can also reference the client: + ```php use MongoDB\Bundle\Attribute\AutowireCollection; use MongoDB\Collection; @@ -245,3 +237,27 @@ class MyService ) {} } ``` + +By specifiying the `default_database` option in the configuration, you can omit the `database` option in the +`AutowireCollection` attribute: + +```diff +mongodb: + clients: + default: + uri: '%env(MONGODB_URI)%' ++ default_database: 'myDatabase' +``` + +```php +use MongoDB\Bundle\Attribute\AutowireCollection; +use MongoDB\Collection; + +class MyService +{ + public function __construct( + #[AutowireCollection] + private Collection $myCollection, + ) {} +} +``` diff --git a/src/Attribute/AutowireDatabase.php b/src/Attribute/AutowireDatabase.php index 1c94f35..c46b17a 100644 --- a/src/Attribute/AutowireDatabase.php +++ b/src/Attribute/AutowireDatabase.php @@ -30,6 +30,7 @@ use Symfony\Component\DependencyInjection\Reference; use function is_string; +use function sprintf; /** * Autowires a MongoDB database. @@ -37,18 +38,20 @@ #[Attribute(Attribute::TARGET_PARAMETER)] final class AutowireDatabase extends AutowireCallable { + private readonly string $serviceId; + public function __construct( private readonly ?string $database = null, ?string $client = null, private readonly array $options = [], bool|string $lazy = false, ) { - $callable = $client === null - ? [new Reference(Client::class), 'selectDatabase'] - : [new Reference(MongoDBExtension::createClientServiceId($client)), 'selectDatabase']; + $this->serviceId = $client === null + ? Client::class + : MongoDBExtension::createClientServiceId($client); parent::__construct( - callable: $callable, + callable: [new Reference($this->serviceId), 'selectDatabase'], lazy: $lazy, ); } @@ -57,7 +60,10 @@ public function buildDefinition(mixed $value, ?string $type, ReflectionParameter { return (new Definition(is_string($this->lazy) ? $this->lazy : ($type ?: Database::class))) ->setFactory($value) - ->setArguments([$this->database ?? $parameter->getName(), $this->options]) + ->setArguments([ + $this->database ?? sprintf('%%%s.default_database%%', $this->serviceId), + $this->options, + ]) ->setLazy($this->lazy); } } diff --git a/tests/TestApplication/src/Controller/AutowireDatabaseController.php b/tests/TestApplication/src/Controller/AutowireDatabaseController.php index 9529ec6..682e0a4 100644 --- a/tests/TestApplication/src/Controller/AutowireDatabaseController.php +++ b/tests/TestApplication/src/Controller/AutowireDatabaseController.php @@ -46,10 +46,10 @@ public function withoutArguments( /** @see AutowireClientTest::testWithCustomClientSetViaOptions() */ #[Route('/with-custom-client')] public function withCustomClient( - #[AutowireDatabase(client: FunctionalTestCase::CLIENT_ID_SECONDARY)] - Database $google, + #[AutowireDatabase(database: 'google', client: FunctionalTestCase::CLIENT_ID_SECONDARY)] + Database $database, ): JsonResponse { - $this->insertDocumentForDatabase($google, FunctionalTestCase::COLLECTION_USERS); + $this->insertDocumentForDatabase($database, FunctionalTestCase::COLLECTION_USERS); return new JsonResponse(); } diff --git a/tests/Unit/Attribute/AutowireDatabaseTest.php b/tests/Unit/Attribute/AutowireDatabaseTest.php index 4ccab63..2f097e2 100644 --- a/tests/Unit/Attribute/AutowireDatabaseTest.php +++ b/tests/Unit/Attribute/AutowireDatabaseTest.php @@ -48,7 +48,7 @@ static function (Database $mydb): void { $this->assertSame(Database::class, $definition->getClass()); $this->assertEquals($autowire->value, $definition->getFactory()); - $this->assertSame('mydb', $definition->getArgument(0)); + $this->assertSame('%MongoDB\Client.default_database%', $definition->getArgument(0)); } public function testDatabase(): void @@ -98,7 +98,7 @@ static function (Database $mydb): void { $this->assertSame(Database::class, $definition->getClass()); $this->assertEquals($autowire->value, $definition->getFactory()); - $this->assertSame('mydb', $definition->getArgument(0)); + $this->assertSame('%mongodb.client.default.default_database%', $definition->getArgument(0)); $this->assertSame(['foo' => 'bar'], $definition->getArgument(1)); } }