Skip to content

Instantly share code, notes, and snippets.

@SerafimArts
Last active October 26, 2016 12:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SerafimArts/4e1ba41848af3c3dbeb2376c57caf108 to your computer and use it in GitHub Desktop.
Save SerafimArts/4e1ba41848af3c3dbeb2376c57caf108 to your computer and use it in GitHub Desktop.
Laravel environment installer

Laravel environment installer

EN

This is CLI UI installer for new .env variables defined in .env.example

Installation

  • Add EnvironmentInstaller.php to app/Console/EnvironmentInstaller.php
  • Open composer.json
  • Add "App\\Console\\EnvironmentInstaller::build" string to post-install-cmd and post-update-cmd
  • Run composer install
  • Enjoy =)

RU

Это консольный интерфейс установщика .env переменных, определённых в файле .env.example.

Установка

  • Добавить файл EnvironmentInstaller.php в app/Console/EnvironmentInstaller.php
  • Открыть composer.json
  • И добавить строку "App\\Console\\EnvironmentInstaller::build" в секции post-install-cmd и post-update-cmd
  • Запустить, например composer install
  • Наслаждаться =)
<?php
/**
* Put this file to "app/Console/EnvironmentInstaller.php"
*/
namespace App\Console;
use Dotenv\Dotenv;
use Composer\Script\Event;
use Illuminate\Support\Str;
use Composer\IO\IOInterface;
use Illuminate\Foundation\Application;
/**
* Class EnvironmentInstaller
* @package App\Console
*/
class EnvironmentInstaller
{
/**
* @var string
*/
private $path;
/**
* @var string
*/
private $file;
/**
* @var array
*/
private $exists = [];
/**
* EnvironmentInstaller constructor.
* @param Application $app
* @param Event $event
*/
public function __construct(Application $app, Event $event)
{
$this->path = $app->environmentPath();
$this->file = $app->environmentFile();
$io = $event->getIO();
$this->loadDefaults();
$fp = fopen($this->file() , 'a+');
$io->write('Setup environment.');
foreach ($this->sync($app, $event->getIO()) as $key => $value) {
if (!$this->has($key)) {
$question = ' ' . $key . ' [' . $value . ']: ';
$value = $io->ask($question, $value);
}
fwrite($fp, $key . '=' . $value . "\n");
}
fclose($fp);
}
/**
* @param string $suffix
* @return string
*/
private function file($suffix = '')
{
return $this->path . '/' . $this->file . $suffix;
}
/**
* @return void
*/
private function loadDefaults()
{
if (is_file($this->file())) {
$env = new Dotenv($this->path, $this->file);
foreach ($this->read($env) as $key => $value) {
$this->exists[] = $key;
}
}
}
/**
* @param Dotenv $env
* @return \Generator
*/
public function read(Dotenv $env)
{
foreach ($env->load() as $field) {
list($name, $value) = explode('=', $field);
if (!Str::startsWith($name, '#')) {
yield $name => $value;
}
}
}
/**
* @param Application $app
* @param IOInterface $io
* @return \Generator
*/
private function sync(Application $app, IOInterface $io)
{
$variables = $this->read(new Dotenv($app->environmentPath(), $app->environmentFile() . '.example'));
foreach ($variables as $key => $value) {
yield $key => $value;
}
}
/**
* @param string $key
* @return bool
*/
private function has($key)
{
return in_array($key, $this->exists, true);
}
/**
* @param Event $event
* @return static
*/
public static function build(Event $event)
{
return new static(app(), $event);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment