Skip to content

Commit aff66e6

Browse files
committed
feat: enforce service configuration to avoid bundle coupling
1 parent 2950165 commit aff66e6

File tree

5 files changed

+48
-43
lines changed

5 files changed

+48
-43
lines changed

README.md

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,17 @@ configuration.
3030

3131
## Configuration
3232

33-
**Note: this is an optional step. By default, the bundle assumes _DynamoDBClient_ and _Marshaler_ services are already
34-
registered in the application DI container.**
35-
36-
After installation, you can configure the bundle by creating a `dynamo_php.yaml` file in your `config/packages/`
33+
After installation, you must configure the bundle by creating a `dynamo_php.yaml` file in your `config/packages/`
3734
directory:
3835

3936
```yaml
4037
# config/packages/dynamo_php.yaml
4138
dynamo_php:
42-
client:
43-
region: 'us-east-1'
44-
version: 'latest'
45-
# Add DynamoDB client configuration options here
46-
marshaler:
47-
# Add Marshaler configuration options here
39+
client: dynamodb_client # set the service ID of the AWS DynamoDB client registered in your app
40+
marshaler: marshaler # set the service ID of the AWS Marshaler registered in your app
41+
serializer: serializer # set the service ID of the Symfony serializer registered in your app
4842
```
4943
50-
Adjust the client and marshaler settings according to your AWS DynamoDB setup and application requirements.
51-
5244
## Usage
5345
5446
Once configured, you can inject DynamoPHP services into your Symfony services or controllers. For example, to use the

phpcs.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
<rule ref="SlevomatCodingStandard.Namespaces.AlphabeticallySortedUses"/>
2626
<rule ref="SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly"/>
2727
<rule ref="SlevomatCodingStandard.Namespaces.UseDoesNotStartWithBackslash"/>
28+
<rule ref="SlevomatCodingStandard.Namespaces.UnusedUses"/>
2829

2930
<rule ref="SlevomatCodingStandard.Arrays.TrailingArrayComma"/>
3031

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
dynamo_php:
2-
client:
3-
region: eu-central-1
2+
client: Aws\DynamoDb\DynamoDbClient
3+
marshaler: Aws\DynamoDb\Marshaler
4+
serializer: Symfony\Component\Serializer\SerializerInterface

sandbox/config/services.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,10 @@ services:
99
resource: '../src/'
1010
exclude:
1111
- '../src/Kernel.php'
12+
13+
Aws\DynamoDb\Marshaler:
14+
15+
Aws\DynamoDb\DynamoDbClient:
16+
arguments:
17+
$args:
18+
region: eu-central-1

src/DynamoPHPBundle.php

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
namespace EduardoMarques\DynamoPHPBundle;
66

7-
use Aws\DynamoDb\DynamoDbClient;
8-
use Aws\DynamoDb\Marshaler;
97
use EduardoMarques\DynamoPHP\Metadata\MetadataLoader;
108
use EduardoMarques\DynamoPHP\ODM\EntityManager;
119
use EduardoMarques\DynamoPHP\ODM\OpArgsBuilder;
@@ -15,6 +13,7 @@
1513
use Symfony\Component\Config\Definition\Configurator\DefinitionConfigurator;
1614
use Symfony\Component\DependencyInjection\ContainerBuilder;
1715
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
16+
use Symfony\Component\DependencyInjection\Loader\Configurator\ReferenceConfigurator;
1817
use Symfony\Component\HttpKernel\Bundle\AbstractBundle;
1918

2019
final class DynamoPHPBundle extends AbstractBundle
@@ -27,8 +26,18 @@ public function configure(DefinitionConfigurator $definition): void
2726
/** @phpstan-ignore-next-line */
2827
$definition->rootNode()
2928
->children()
30-
->variableNode('client')->end()
31-
->variableNode('marshaler')->end()
29+
->scalarNode('client')->info('Service ID of the AWS DynamoDB client to use')
30+
->isRequired()
31+
->cannotBeEmpty()
32+
->end()
33+
->scalarNode('marshaler')->info('Service ID of the AWS Marshaler to use')
34+
->isRequired()
35+
->cannotBeEmpty()
36+
->end()
37+
->scalarNode('serializer')->info('Service ID of the Symfony Serializer to use')
38+
->isRequired()
39+
->cannotBeEmpty()
40+
->end()
3241
->end();
3342
}
3443

@@ -38,53 +47,48 @@ public function configure(DefinitionConfigurator $definition): void
3847
*/
3948
public function loadExtension(array $config, ContainerConfigurator $container, ContainerBuilder $builder): void
4049
{
41-
$services = $container->services();
42-
43-
if (
44-
false === $builder->hasDefinition(DynamoDbClient::class)
45-
&& false === $builder->hasAlias(DynamoDbClient::class)
46-
) {
47-
$services->set(DynamoDbClient::class)
48-
->factory([DynamoDbClient::class, 'factory'])
49-
->arg(0, $config['client'] ?? [])
50-
->public();
51-
}
50+
$clientId = $config['client'];
51+
$marshalerId = $config['marshaler'];
52+
$serializerId = $config['serializer'];
5253

53-
if (
54-
false === $builder->hasDefinition(Marshaler::class)
55-
&& false === $builder->hasAlias(Marshaler::class)
56-
) {
57-
$services->set(Marshaler::class)
58-
->arg(0, $config['marshaler'] ?? [])
59-
->public();
60-
}
54+
$services = $container->services();
6155

62-
$services->set(MetadataLoader::class)
63-
->autowire()
64-
->autoconfigure()
65-
->private();
56+
$services->set(MetadataLoader::class)->autowire()->autoconfigure()->private();
6657

6758
$services->set(EntityNormalizer::class)
59+
->arg('$metadataLoader', new ReferenceConfigurator(MetadataLoader::class))
60+
->arg('$normalizer', new ReferenceConfigurator($serializerId))
6861
->autowire()
6962
->autoconfigure()
70-
->public();
63+
->private();
7164

7265
$services->set(EntityDenormalizer::class)
66+
->arg('$metadataLoader', new ReferenceConfigurator(MetadataLoader::class))
67+
->arg('$denormalizer', new ReferenceConfigurator($serializerId))
7368
->autowire()
7469
->autoconfigure()
75-
->public();
70+
->private();
7671

7772
$services->set(EntitySerializer::class)
73+
->arg('$entityNormalizer', new ReferenceConfigurator(EntityNormalizer::class))
74+
->arg('$entityDenormalizer', new ReferenceConfigurator(EntityDenormalizer::class))
75+
->arg('$marshaler', new ReferenceConfigurator($marshalerId))
7876
->autowire()
7977
->autoconfigure()
8078
->public();
8179

8280
$services->set(OpArgsBuilder::class)
81+
->arg('$normalizer', new ReferenceConfigurator($serializerId))
82+
->arg('$marshaler', new ReferenceConfigurator($marshalerId))
8383
->autowire()
8484
->autoconfigure()
8585
->public();
8686

8787
$services->set(EntityManager::class)
88+
->arg('$dynamoDbClient', new ReferenceConfigurator($clientId))
89+
->arg('$metadataLoader', new ReferenceConfigurator(MetadataLoader::class))
90+
->arg('$entitySerializer', new ReferenceConfigurator(EntitySerializer::class))
91+
->arg('$opArgsBuilder', new ReferenceConfigurator(OpArgsBuilder::class))
8892
->autowire()
8993
->autoconfigure()
9094
->public();

0 commit comments

Comments
 (0)