-
-
Notifications
You must be signed in to change notification settings - Fork 322
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
New definition type for reading environment variables. #176
Conversation
Hi, This looks awesome! This is definitely good to be merged, hopefully for the next version (4.3). I just have a few remarks:
Do you see any solution to that? I don't :( Using Also if you have time, that would be awesome if you could add a small test for this case: return [
// null default value
'optional-null' => DI\env('unknown', null),
]; just to check that we can set |
Actually regarding my comment about |
Thanks for the feedback :) As far as I'm aware environment variables can only ever be strings, so I don't think the false check is cause for concern. I'll try to get the dumper added later today. |
Oh, and the other test case you mentioned! |
All done. |
Awesome! I have merged this manually into the 4.3 branch (#174) so it will be in the next release. |
I have just released PHP-DI 4.3.0 which includes this feature. Thanks! |
Overview
This PR contains an implementation of a new definition type
EnvironmentVariableDefinition
which allows for easy reading of a system environment variable with fallback to a default value if the environment variable is not defined.Rationale
I've had the need to create several container definitions that look something like the following:
I figured this might be a common situation for other developers, especially those deploying their applications to systems like Heroku, or otherwise following the principles of 12-factor applications.
Usage
The following snippet is equivalent to the example given above:
The first parameter to
DI\env()
is the name of the environment variable to read, the second parameter is the default value to use in the event that the environment variable is not defined.If the environment variable is not defined and no default is provided, a
DefinitionException
is thrown when attempting to resolve the value.Finally, default values can reference other definitions using
DI\link()
:Tests
Unit tests and integration tests have been implemented.
Remaining Issues
I think this PR is usable as it stands, however one thing I was unsure of is how strict the
isResolvable()
implementation needs to be. For example, it does not currently follow references made withDI\link()
to check that they are also resolvable.Edit It looks perhaps I need to do some work on a definition dumper for this new definition type.
Hopefully this a is a feature that you would consider desirable, and of course if there's anything that needs to be corrected please let me know.