Skip to content
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

Environment variables object #208

Closed

Conversation

nikolaposa
Copy link

Extracted logic for getting/setting variables into a separate EnvironmentVariables object in order to have a clear separation of responsibilities between loading values from a file and accessing values using native PHP facilitators.

EnvironmentVariables object can be used standalone, for read-only purposes, as an alternative for using getenv() function or global variables. It can also injected into a DI container so that environment variables are available when constructing services. For example:

$container->set('env', EnvironmentVariables::createImmutable());

$container->set('service', function (ContainerInterface $container) {
    $env = $container->get('env');
    return new My\Service($env['FOO'], $env['BAR']);
});

@j4m3s
Copy link

j4m3s commented Mar 3, 2017

This looks like a worthwhile refactoring, and makes a lot of sense from a concerns and testing perspective. You've even used protected variables for open/closed principles!

I'm not a maintainer I'm afraid, but see this as valuable, particularly as it opens the door for people to extend EnvironmentVariables to put their own end to the $_ENV vs $_SERVER vs putenv in a clean and swappable fashion.

@Caroga
Copy link

Caroga commented Mar 23, 2017

+1

@Caroga
Copy link

Caroga commented Apr 12, 2017

Any updates would be much appreciated.

*/
public static function create()
{
return new self(false);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since this class is not final, we need "static" here, not "self". Alternatively, it could be desirable to actually make the class final.

Copy link
Author

Choose a reason for hiding this comment

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

If we go down the "final" path, it would be good to do protected ->private modifications also, but since there's no specific interface that describes EnvironmentVariables implementations, I think there's no point in making this class final.

So I'll fix this method to support late static binding as you suggested.

Copy link
Owner

Choose a reason for hiding this comment

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

@GrahamCampbell can you approve this review since the references to 'self' here have been changed to 'static'?

@nikolaposa nikolaposa force-pushed the feature/env-variables-object branch from 877659e to 89cdc3f Compare April 12, 2017 13:18
@nikolaposa
Copy link
Author

Why is the build failing, those error messages does not make any sense? Race condition or something, I guess I should sync my branch with changes on the master.

@nikolaposa
Copy link
Author

Note that after syncing with changes on the master branch introduced with #215, I had to slightly adapt them. To be honest, I really don't understand why those changes were needed at the first place.

@piotr-cz
Copy link
Contributor

piotr-cz commented Nov 14, 2017

It's sometimes helpful to load variables into an array without setting them in environment.
My use case is populating phinx environments config file as it allows migrations on specific environment from CLI:

phinx migrate --environment=testing

Related issues: #113, #163,

@vlucas
Copy link
Owner

vlucas commented Nov 15, 2017

I like this overall. What are your thoughts on reading in all the ENV vars (maybe merging $_ENV, $_SERVER, etc. and caching them locally, then having get return those cached env vars first? I only bring this up because many people seem to have issues with getenv, etc. being called from multiple different areas in their project at different times.

@nikolaposa
Copy link
Author

@vlucas That is definitely something that can be considered, but I think it kinda goes beyond the scope of the initial idea. This was a simple refactor for a better separation of concerns, as well as to encapsulate/abstract logic for dealing with environment variables, and use it in an object-oriented fashion, as opposed to using getenv() or $_ENV directly. I didn't want to introduce any changes that might affect existing behaviour.

Therefore, I see yours and many other ideas as a follow up on this one, which after being merged opens opportunities for other improvements as well. I would rather improve things gradually, in smaller increments, rather than having one big PR with lots of (unrelated) changes in it.

*/
public static function create()
{
return new self(false);
Copy link
Owner

Choose a reason for hiding this comment

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

@GrahamCampbell can you approve this review since the references to 'self' here have been changed to 'static'?

@Korbeil
Copy link
Contributor

Korbeil commented May 19, 2018

@GrahamCampbell any news about this approval ?
(since the self() has been replaced by static())

@GrahamCampbell
Copy link
Collaborator

I'm working on rebasing this now. Having to check that every change that was made since is being copied into your new class.

@GrahamCampbell
Copy link
Collaborator

Replaced by #300. Gone with a more abstract approach, so it can be truly flexible.

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.

7 participants