Skip to content

Feat: Add environment variable support for DSN #68

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 17, 2024
Merged

Conversation

RikudouSage
Copy link
Collaborator

Description

This uses the recently introduced Stringable support for basic parameters to allow runtime bound values in the container, which is the expected way to support env binding in Symfony apps (as opposed to just binding the parameter from the env variable at the time the container was built).

A proxy class for static values has been created as well so that we don't have to do another unnecessary round of if/else and just provide a service with the same name as opposed to distinguishing between the service (Stringable instances) and parameters (static string values), especially because they have different syntax in the services.yaml config (%parameter_name% vs @service_name).

Fixes #66

Type of change

  • New feature (non-breaking change which adds functionality)

How Has This Been Tested?

  • Manual Tests

Checklist:

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • Any dependent changes have been merged and published in downstream modules

@RikudouSage RikudouSage requested a review from sighphyre April 17, 2024 10:13
@@ -8,7 +8,7 @@
"symfony/http-client": "^5.0 | ^6.0 | ^7.0",
"symfony/cache": "^5.0 | ^6.0 | ^7.0",
"nyholm/psr7": "^1.0",
"unleash/client": "^1.6 | ^2.0",
"unleash/client": "^2.4",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice thank you!

Copy link
Member

@sighphyre sighphyre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know so little about Symfony that this is a partially blind review but the logic looks correct to me based on your previous work

@RikudouSage RikudouSage merged commit 9d64662 into main Apr 17, 2024
3 checks passed
@RikudouSage RikudouSage deleted the feat/env-dsn branch April 17, 2024 13:53

use Stringable;

final readonly class LateBoundDsnParameter implements Stringable
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RikudouSage @sighphyre this seems to me to be the wrong abstraction.

It seems to me it assumes the env var will directly evaluate to a DSN (feel free to correct if this is wrong), but that's often not the case.

For example, the config might look like this:

unleash_client:
    dsn: '%env(trim:file:UNLEASH_DSN_FILE)%'

with this workflow:

  1. UNLEASH_DSN_FILE=/run/secrets/unleash_dsn
  2. read this file and then trim its contents using built-in env var processors

This should typically all work by default because Symfony itself does the late binding for us, it knows the variable needs to be re-evaluated at runtime before it's passed to the service.

This currently doesn't happen to me, the env var evaluates to an empty string.

For comparison, FOS Elastica bundle supports this same process like so:

fos_elastica:
    clients:
        default:
            url: '%env(trim:file:SEARCH_DSN_FILE)%'

and there it works as expected.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you post it as a separate issue, please?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I'm just checking if my assumption about env var containing the DSN is correct?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in #73, notice also the issue with tags getting created constantly and end up being seen as releases by Packagist: https://packagist.org/packages/unleash/symfony-client-bundle

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: DSN does not work from environment variables
3 participants