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

Added a simple php output format #18

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

martinwittmann
Copy link

A use-case which I find very useful is to add a php output format.

This allows users to generate a simple php file, that can be included into existing projects which eliminates the need to vlucas/dotenv and marginally improves performance since the .env files are not parsed at every http request.

@ko1nksm
Copy link
Owner

ko1nksm commented Jul 18, 2024

Hi @martinwittmann, thanks for the contribution!

I ran this and got the following

$ ./shdotenv -f php
<?php

function __local_env() {
  $env = [];
  $env["VAR"] = "123";

  return $env;
}

Apparently it is a function that returns (the contents of .env) as an associative array.

Given that shdotenv is a tool for setting environment variables, I am thinking that the following output would be good. Assignments to arbitrary variables should be outside the scope of this project.

$ ./shdotenv -f php
<?php
putenv('VAR=123');
?>

Note:

  • It seems that $_ENV is no longer available in recent versions of PHP.
  • I know that the ?> at the end can be omitted, but I see no reason to omit it here.
  • The arguments of putenv must be correctly escaped so that they can be safely read.
  • If you want to assign to an associative array instead of an environment variable, it would be better to use the JSON format, for example.
  • shdotenv --help also needs to add php

However, what you really want seems to be a JSON to PHP code conversion tool. That could probably be easily made using PHP's var_export function.

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.

2 participants