69

Simple PHP tool to visually manage .env files – Oscar Mwanandimai – Medium

 6 years ago
source link: https://medium.com/@oscarmwana/simple-php-tool-to-visually-manage-env-files-d0a590586db2?source=linkShare-c4fd4c89766b-1516291819
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
1*qFTEJLpomAo3F6iOiLhcgQ.png

I work largely with Laravel apps and post deployment I have to SSH into different servers to manage .env files as we scale the apps and I have to update configs, so this tool is at this point very biased. I like the visual environment editor Laravel’s Envoyer provides per project as well as the Config Variables settings in Heroku. Now I won’t go as far as the Heroku version but the simple Envoyer one is an easy benchmark.

Assumptions

  1. You use Laravel and Laravel Homestead
  2. You understand this is my proof of concept
  3. An understanding that “simple” = to very little code + a library or two

Implementation

The implementation requires a few simple tasks to be completed (omitting various validations like the existence of the file etc).

  1. Tell the app my projects’ root directory
  2. Structurally store a list of my projects somewhere
  3. Update the projects’ .env

Code is available on Github here

Telling my app where my projects’ root directory is

The idea is to somehow offer multiple per installation config options without changing the source code itself, enter the dotenv library. Long game: perhaps as part of adding a project you define a location type, for those of us that have both Xampp and Homestead on our machines, that matches a location config in your .env.

// .envhomestead=C:\Users\oscarm\code

Store a list of my projects for the app to reference

Enter Symfony’s Yaml Component, I could have used any other storage option really but I chose controversy. I have always been keen to learn a ton about parsing different file types in PHP/Ruby after I tried to figure out how I could create a web interface for my Homestead.yaml file to manage it and clean it up — it gets messy after a few projects. This package made it simple, with a few lines of code below it read my entire yaml file as an array and back to yaml again.

// read the yaml file and return array
$sites = Yaml::parseFile($sitesFile);
if (isset($_POST[‘site’], $_POST[‘type’])) { array_push($sites, [‘name’ => $_POST[‘site’], ‘type’ => $_POST[‘type’]]); // updated projects array to yaml
$yaml = Yaml::dump($sites); file_put_contents($sitesFile, $yaml);}

Update the projects’ .env

This task was dead simple, when I clicked to view the project’s env I loaded a textarea with its content. Now to update the .env I just posted the updated file back and replaced the existing .env with the updated content.

if (isset($_POST['env'], $_POST['update_site'])) {    try {        file_put_contents(getSitePath($_POST['update_site']), $_POST['env']);    } catch (\Exception $e) {        var_dump($e);    }    header("Refresh:0");}

Next steps

While a simple tool was fun to do and share the complex parts are still uncovered. SSHing into remote servers and updating the .env there, as that is my particular use case. While I have created some prototypes in other languages I have not tackled the challenge in PHP. Adding more configuration features would be handy too, however I would have to play around with a million and one tools to see config patterns used by different frameworks and in other languages too.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK