Skip to content

Added a simple php output format#18

Open
martinwittmann wants to merge 2 commits into
ko1nksm:mainfrom
schwerpunkt-digital:main
Open

Added a simple php output format#18
martinwittmann wants to merge 2 commits into
ko1nksm:mainfrom
schwerpunkt-digital:main

Conversation

@martinwittmann

Copy link
Copy Markdown

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

ko1nksm commented Jul 18, 2024

Copy link
Copy Markdown
Owner

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