Skip to content

Commit

Permalink
added alternative .env system
Browse files Browse the repository at this point in the history
  • Loading branch information
OwenMelbz committed May 30, 2018
1 parent ad36cbb commit 211c087
Show file tree
Hide file tree
Showing 9 changed files with 200 additions and 104 deletions.
2 changes: 2 additions & 0 deletions app/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public function getName(): string
/**
* Returns true/false depending if the unique modifier has been used.
*
* @param string $string
* @return bool
*/
private function checkIfUnique(string $string): bool
Expand All @@ -84,6 +85,7 @@ private function checkIfUnique(string $string): bool
/**
* Returns the name of the faker method to be used.
*
* @param string $string
* @return string
*/
private function getReplacementMethod(string $string): string
Expand Down
12 changes: 6 additions & 6 deletions app/Commands/ForgetMeNowCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

namespace App\Commands;

use App\Services\EnvService;
use App\Services\UtilityService;
use App\Services\DatabaseService;
use App\Services\ForgetDbService;
use App\Services\UtilityService;
use LaravelZero\Framework\Commands\Command;

/**
Expand Down Expand Up @@ -56,9 +57,9 @@ public function handle(): void

$connected = false;

if ($aDotEnv = UtilityService::checkIfCanUseDotEnv()) {
if ($aDotEnv = EnvService::checkIfCanUseDotEnv()) {
if ($this->confirm('We found a .env file in your current directory, do you want to use it to help define your DB connection?', !config('app.production'))) {
UtilityService::loadDotEnv();
EnvService::loadDotEnv();
}
}

Expand Down Expand Up @@ -125,7 +126,7 @@ public function fail(string $string): void
private function getDatabaseConfig(): array
{
$driverList = DatabaseService::getDriverOptions();
array_unshift($driverList, env('DB_CONNECTION', 'mysql'));
array_unshift($driverList, EnvService::get('DB_CONNECTION', 'mysql'));
$driverList = array_unique($driverList);
$driverList = array_values($driverList);

Expand Down Expand Up @@ -159,10 +160,9 @@ private function getDatabaseConfig(): array
if ($option === 'password') {
$usersConfiguration[$option] = $this->secret('Database:: ' . $option, $default);

if (is_null($usersConfiguration[$option]) && $envPass = env('DB_PASSWORD', null)) {
if (is_null($usersConfiguration[$option]) && $envPass = EnvService::get('DB_PASSWORD', null)) {
$usersConfiguration[$option] = $envPass;
}

} else {
$usersConfiguration[$option] = $this->ask('Database:: ' . $option, $default);
}
Expand Down
4 changes: 4 additions & 0 deletions app/Commands/UpdateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
use App\Services\UtilityService;
use LaravelZero\Framework\Commands\Command;

/**
* Class UpdateCommand
* @package App\Commands
*/
class UpdateCommand extends Command
{

Expand Down
2 changes: 1 addition & 1 deletion app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function boot(): void
{
// We only want one instance of faker, this allows
// us to use unique() to prevent data clashes.
$this->app->singleton('faker', function ($app) {
$this->app->singleton('faker', function () {
return \Faker\Factory::create();
});
}
Expand Down
40 changes: 20 additions & 20 deletions app/Services/DatabaseService.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ public static function optionsForMySql(): array
{
return [
'driver' => 'mysql',
'host' => env('DB_HOST','127.0.0.1'),
'port' => env('DB_PORT','3306'),
'database' => env('DB_DATABASE','my_magical_database'),
'username' => env('DB_USERNAME','root'),
'password' => env('DB_PASSWORD',''),
'prefix' => env('DB_PREFIX',''),
'host' => EnvService::get('DB_HOST', '127.0.0.1'),
'port' => EnvService::get('DB_PORT', '3306'),
'database' => EnvService::get('DB_DATABASE', 'my_magical_database'),
'username' => EnvService::get('DB_USERNAME', 'root'),
'password' => EnvService::get('DB_PASSWORD', ''),
'prefix' => EnvService::get('DB_PREFIX', ''),
'unix_socket' => '',
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
Expand All @@ -83,12 +83,12 @@ public static function optionsForPgSql(): array
{
return [
'driver' => 'pgsql',
'host' => env('DB_HOST','127.0.0.1'),
'port' => env('DB_PORT','5432'),
'database' => env('DB_DATABASE','my_magical_database'),
'username' => env('DB_USERNAME','root'),
'password' => env('DB_PASSWORD',''),
'prefix' => env('DB_PREFIX',''),
'host' => EnvService::get('DB_HOST', '127.0.0.1'),
'port' => EnvService::get('DB_PORT', '5432'),
'database' => EnvService::get('DB_DATABASE', 'my_magical_database'),
'username' => EnvService::get('DB_USERNAME', 'root'),
'password' => EnvService::get('DB_PASSWORD', ''),
'prefix' => EnvService::get('DB_PREFIX', ''),
'charset' => 'utf8',
'schema' => 'public',
'sslmode' => 'prefer',
Expand All @@ -105,8 +105,8 @@ public static function optionsForSqlite(): array
{
return [
'driver' => 'sqlite',
'database' => env('DB_DATABASE','database.sqlite'),
'prefix' => env('DB_PREFIX',''),
'database' => EnvService::get('DB_DATABASE', 'database.sqlite'),
'prefix' => EnvService::get('DB_PREFIX', ''),
];
}

Expand All @@ -120,12 +120,12 @@ public static function optionsForSqlSrv(): array
{
return [
'driver' => 'sqlsrv',
'host' => env('DB_HOST','127.0.0.1'),
'port' => env('DB_PORT','1433'),
'database' => env('DB_DATABASE','my_magical_database'),
'username' => env('DB_USERNAME','root'),
'password' => env('DB_PASSWORD',''),
'prefix' => env('DB_PREFIX',''),
'host' => EnvService::get('DB_HOST', '127.0.0.1'),
'port' => EnvService::get('DB_PORT', '1433'),
'database' => EnvService::get('DB_DATABASE', 'my_magical_database'),
'username' => EnvService::get('DB_USERNAME', 'root'),
'password' => EnvService::get('DB_PASSWORD', ''),
'prefix' => EnvService::get('DB_PREFIX', ''),
'charset' => 'utf8',
];
}
Expand Down
49 changes: 49 additions & 0 deletions app/Services/EnvService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace App\Services;

use josegonzalez\Dotenv\Loader as DotEnv;

/**
* Class EnvService
* @package App\Services
*/
class EnvService
{

public static $env = [];

/**
* Checks to see if there is a .env file in the directory.
*
* @return bool
*/
public static function checkIfCanUseDotEnv(): bool
{
$potentialPath = getcwd() . '/.env';

return file_exists($potentialPath);
}

/**
* Load the environment variables.
*
* @return void
*/
public static function loadDotEnv(): void
{
static::$env = (new DotEnv(getcwd() . '/.env'))->parse()->toArray();
}

/**
* Return the environment variable by key.
*
* @param $key
* @param null $default
* @return string
*/
public static function get($key, $default = null)
{
return array_key_exists($key, static::$env) ? static::$env[$key] : $default;
}
}
24 changes: 0 additions & 24 deletions app/Services/UtilityService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace App\Services;

use Dotenv\Dotenv;
use Symfony\Component\Yaml\Yaml;

/**
Expand Down Expand Up @@ -158,27 +157,4 @@ public static function ordinal($number): string
}
}

/**
* Checks to see if there is a .env file in the directory.
*
* @return bool
*/
public static function checkIfCanUseDotEnv(): bool
{
$potentialPath = getcwd() . '/.env';

return file_exists($potentialPath);
}

/**
* Uses DotEnv to load the environment variables..
*
* @return void
*/
public static function loadDotEnv(): void
{
(new Dotenv(
getcwd()
))->load();
}
}
8 changes: 6 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
"php": "^7.1.3",
"fzaninotto/faker": "^1.7",
"illuminate/database": "5.6.*",
"josegonzalez/dotenv": "^3.2",
"knplabs/github-api": "^2.8",
"laravel-zero/framework": "5.6.*",
"php-http/guzzle6-adapter": "^1.1",
"symfony/yaml": "^4.0",
"vlucas/phpdotenv": "^2.4"
"symfony/yaml": "^4.0"
},
"require-dev": {
"phpunit/phpunit": "^7.0",
Expand Down Expand Up @@ -53,6 +53,10 @@
],
"build": [
"php forget-db app:build"
],
"link": [
"rm /usr/local/bin/forget-db || true",
"cp ./builds/application /usr/local/bin/forget-db"
]
},
"minimum-stability": "dev",
Expand Down
Loading

0 comments on commit 211c087

Please sign in to comment.