Skip to content

Commit 1cbc4c5

Browse files
authored
Allow usage of #[Target] attribute for the MongoDB client (#15)
* Allow usage of #[Target] attribute for the MongoDB client * Add test to autowire client using #[Target] attribute
1 parent 5beff17 commit 1cbc4c5

File tree

4 files changed

+30
-0
lines changed

4 files changed

+30
-0
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,21 @@ class MyService
128128
}
129129
```
130130

131+
You can also use the `#[Target]` attribute:
132+
133+
```php
134+
use MongoDB\Client;
135+
use Symfony\Component\DependencyInjection\Attribute\Target;
136+
137+
class MyService
138+
{
139+
public function __construct(
140+
#[Target('second')]
141+
private Client $client,
142+
) {}
143+
}
144+
```
145+
131146
## Database Usage
132147

133148
The client service provides access to databases and collections. You can access a database by calling the

src/DependencyInjection/MongoDBExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ private function createClients(string $defaultClient, array $clients, ContainerB
7878

7979
// Allows to autowire the client using the name
8080
$container->registerAliasForArgument($serviceId, Client::class, sprintf('%sClient', $client));
81+
$container->registerAliasForArgument($serviceId, Client::class, $client);
8182
}
8283

8384
// Register an autowiring alias for the default client

tests/Functional/Attribute/AutowireClientTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ public static function autowireClientProvider(): iterable
5353

5454
/** @see AutowireClientController::viaNamedClient() */
5555
yield 'via-named-client' => ['/autowire-client/via-named-client', self::CLIENT_ID_SECONDARY, self::DB_CUSTOMER_GOOGLE, self::COLLECTION_USERS];
56+
57+
/** @see AutowireClientController::viaTarget() */
58+
yield 'via-target' => ['/autowire-client/via-target', self::CLIENT_ID_SECONDARY, self::DB_CUSTOMER_GOOGLE, self::COLLECTION_USERS];
5659
}
5760

5861
/**

tests/TestApplication/src/Controller/AutowireClientController.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use MongoDB\Bundle\Tests\Functional\Attribute\AutowireClientTest;
2525
use MongoDB\Bundle\Tests\Functional\FunctionalTestCase;
2626
use MongoDB\Client;
27+
use Symfony\Component\DependencyInjection\Attribute\Target;
2728
use Symfony\Component\HttpFoundation\JsonResponse;
2829
use Symfony\Component\Routing\Annotation\Route;
2930

@@ -69,6 +70,16 @@ public function viaNamedClient(
6970
return new JsonResponse();
7071
}
7172

73+
#[Route('/via-target')]
74+
public function viaTarget(
75+
#[Target('secondary')]
76+
Client $client,
77+
): JsonResponse {
78+
$this->insertDocumentForClient($client, FunctionalTestCase::DB_CUSTOMER_GOOGLE, FunctionalTestCase::COLLECTION_USERS);
79+
80+
return new JsonResponse();
81+
}
82+
7283
#[Route('/with-unknown-client')]
7384
public function withUnknownClient(
7485
#[AutowireClient(client: 'foo-bar-baz')]

0 commit comments

Comments
 (0)