Skip to content

Commit 2d3784a

Browse files
alcaeusOskarStark
andauthored
Remove parameter name usage for AutowireDatabase (#13)
* Remove parameter name usage for AutowireDatabase * Fix documentation * Update readme --------- Co-authored-by: Oskar Stark <oskarstark@googlemail.com>
1 parent 050062e commit 2d3784a

File tree

4 files changed

+53
-31
lines changed

4 files changed

+53
-31
lines changed

README.md

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ mongodb:
5555
clients:
5656
default:
5757
uri: '%env(MONGODB_URI)%'
58+
default_database: #...
5859
uri_options: #...
5960
driver_options: #...
6061
```
@@ -96,7 +97,8 @@ class MyService
9697
}
9798
```
9899

99-
If you register multiple clients, you can autowire them by name + `Client` suffix:
100+
If you register multiple clients, you can autowire them by using the client name with a `Client` suffix as parameter
101+
name:
100102

101103
```php
102104
use MongoDB\Bundle\Attribute\AutowireClient;
@@ -105,6 +107,7 @@ use MongoDB\Client;
105107
class MyService
106108
{
107109
public function __construct(
110+
// Will autowire the client with the id "second"
108111
private Client $secondClient,
109112
) {}
110113
}
@@ -119,16 +122,16 @@ use MongoDB\Client;
119122
class MyService
120123
{
121124
public function __construct(
122-
#[AutowireClient('second')]
125+
#[AutowireClient('second')]
123126
private Client $client,
124127
) {}
125128
}
126129
```
127130

128131
## Database and Collection Usage
129132

130-
The client service provides access to databases and collections. You can access a database by calling the `selectDatabase`
131-
method, passing the database name and potential options:
133+
The client service provides access to databases and collections. You can access a database by calling the
134+
`selectDatabase` method, passing the database name and potential options:
132135

133136
```php
134137
use MongoDB\Client;
@@ -146,7 +149,7 @@ class MyService
146149
}
147150
```
148151

149-
An alternative to this is using the `AutowireDatabase` attribute, referencing the database name:
152+
An alternative to this is using the `#[AutowireDatabase]` attribute, referencing the database name:
150153

151154
```php
152155
use MongoDB\Bundle\Attribute\AutowireDatabase;
@@ -161,21 +164,9 @@ class MyService
161164
}
162165
```
163166

164-
You can also omit the `database` option if the property name matches the database name.
165-
In the following example the database name is `myDatabase`, inferred from the property name:
166-
167-
```php
168-
use MongoDB\Bundle\Attribute\AutowireCollection;
169-
use MongoDB\Collection;
170-
171-
class MyService
172-
{
173-
public function __construct(
174-
#[AutowireCollection()]
175-
private Collection $myDatabase,
176-
) {}
177-
}
178-
```
167+
If you don't specify a database name in the attribute, the default database name (specified in the `default_database`
168+
configuration option) will be used. If you did not define a default database, the database name has to be specified in
169+
the attribute.
179170

180171
If you have more than one client defined, you can also reference the client:
181172

@@ -193,7 +184,7 @@ class MyService
193184
```
194185

195186
To inject a collection, you can either call the `selectCollection` method on a `Client` or `Database` instance.
196-
For convenience, the `AutowireCollection` attribute provides a quicker alternative:
187+
For convenience, the `#[AutowireCollection]` attribute provides a quicker alternative:
197188

198189
```php
199190
use MongoDB\Bundle\Attribute\AutowireCollection;
@@ -230,6 +221,7 @@ class MyService
230221
```
231222

232223
If you have more than one client defined, you can also reference the client:
224+
233225
```php
234226
use MongoDB\Bundle\Attribute\AutowireCollection;
235227
use MongoDB\Collection;
@@ -245,3 +237,27 @@ class MyService
245237
) {}
246238
}
247239
```
240+
241+
By specifiying the `default_database` option in the configuration, you can omit the `database` option in the
242+
`AutowireCollection` attribute:
243+
244+
```diff
245+
mongodb:
246+
clients:
247+
default:
248+
uri: '%env(MONGODB_URI)%'
249+
+ default_database: 'myDatabase'
250+
```
251+
252+
```php
253+
use MongoDB\Bundle\Attribute\AutowireCollection;
254+
use MongoDB\Collection;
255+
256+
class MyService
257+
{
258+
public function __construct(
259+
#[AutowireCollection]
260+
private Collection $myCollection,
261+
) {}
262+
}
263+
```

src/Attribute/AutowireDatabase.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,28 @@
3030
use Symfony\Component\DependencyInjection\Reference;
3131

3232
use function is_string;
33+
use function sprintf;
3334

3435
/**
3536
* Autowires a MongoDB database.
3637
*/
3738
#[Attribute(Attribute::TARGET_PARAMETER)]
3839
final class AutowireDatabase extends AutowireCallable
3940
{
41+
private readonly string $serviceId;
42+
4043
public function __construct(
4144
private readonly ?string $database = null,
4245
?string $client = null,
4346
private readonly array $options = [],
4447
bool|string $lazy = false,
4548
) {
46-
$callable = $client === null
47-
? [new Reference(Client::class), 'selectDatabase']
48-
: [new Reference(MongoDBExtension::createClientServiceId($client)), 'selectDatabase'];
49+
$this->serviceId = $client === null
50+
? Client::class
51+
: MongoDBExtension::createClientServiceId($client);
4952

5053
parent::__construct(
51-
callable: $callable,
54+
callable: [new Reference($this->serviceId), 'selectDatabase'],
5255
lazy: $lazy,
5356
);
5457
}
@@ -57,7 +60,10 @@ public function buildDefinition(mixed $value, ?string $type, ReflectionParameter
5760
{
5861
return (new Definition(is_string($this->lazy) ? $this->lazy : ($type ?: Database::class)))
5962
->setFactory($value)
60-
->setArguments([$this->database ?? $parameter->getName(), $this->options])
63+
->setArguments([
64+
$this->database ?? sprintf('%%%s.default_database%%', $this->serviceId),
65+
$this->options,
66+
])
6167
->setLazy($this->lazy);
6268
}
6369
}

tests/TestApplication/src/Controller/AutowireDatabaseController.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ public function withoutArguments(
4646
/** @see AutowireClientTest::testWithCustomClientSetViaOptions() */
4747
#[Route('/with-custom-client')]
4848
public function withCustomClient(
49-
#[AutowireDatabase(client: FunctionalTestCase::CLIENT_ID_SECONDARY)]
50-
Database $google,
49+
#[AutowireDatabase(database: 'google', client: FunctionalTestCase::CLIENT_ID_SECONDARY)]
50+
Database $database,
5151
): JsonResponse {
52-
$this->insertDocumentForDatabase($google, FunctionalTestCase::COLLECTION_USERS);
52+
$this->insertDocumentForDatabase($database, FunctionalTestCase::COLLECTION_USERS);
5353

5454
return new JsonResponse();
5555
}

tests/Unit/Attribute/AutowireDatabaseTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ static function (Database $mydb): void {
4848

4949
$this->assertSame(Database::class, $definition->getClass());
5050
$this->assertEquals($autowire->value, $definition->getFactory());
51-
$this->assertSame('mydb', $definition->getArgument(0));
51+
$this->assertSame('%MongoDB\Client.default_database%', $definition->getArgument(0));
5252
}
5353

5454
public function testDatabase(): void
@@ -98,7 +98,7 @@ static function (Database $mydb): void {
9898

9999
$this->assertSame(Database::class, $definition->getClass());
100100
$this->assertEquals($autowire->value, $definition->getFactory());
101-
$this->assertSame('mydb', $definition->getArgument(0));
101+
$this->assertSame('%mongodb.client.default.default_database%', $definition->getArgument(0));
102102
$this->assertSame(['foo' => 'bar'], $definition->getArgument(1));
103103
}
104104
}

0 commit comments

Comments
 (0)