Skip to content

Commit d392935

Browse files
committed
#6 add env console command
1 parent 6ba32ff commit d392935

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

src/Bootstrap.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ public function bootstrap($app)
3333
$app->controllerMap['modules-migrate'] = [
3434
'class' => 'nullref\core\console\ModulesMigrateController',
3535
];
36+
$app->controllerMap['env'] = [
37+
'class' => 'nullref\core\console\EnvController',
38+
];
3639
}
3740
if (YII_ENV_DEV && class_exists('yii\gii\Module')) {
3841
Event::on(Gii::className(), Gii::EVENT_BEFORE_ACTION, function (Event $event) {

src/components/DotenvManger.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
namespace nullref\core\components;
4+
5+
use Dotenv;
6+
use Yii;
7+
use yii\helpers\Console;
8+
9+
/**
10+
* @author Dmytro Karpovych
11+
* @copyright 2016 NRE
12+
*/
13+
class DotenvManger extends Dotenv
14+
{
15+
public static function modify()
16+
{
17+
$data = self::load(Yii::getAlias('@app/..'));
18+
19+
$result = implode(PHP_EOL, array_map(function ($item) {
20+
if (is_array($item)) {
21+
$default = $item[1];
22+
$name = $item[0];
23+
$value = Console::input("$name [$default] : ");
24+
if (empty($value)) {
25+
$value = $default;
26+
}
27+
return $name . '=' . $value;
28+
}
29+
return $item;
30+
}, $data));
31+
32+
file_put_contents(Yii::getAlias('@app/../.env'), $result);
33+
}
34+
35+
public static function load($path, $file = '.env')
36+
{
37+
if (!is_string($file)) {
38+
$file = '.env';
39+
}
40+
41+
$filePath = rtrim($path, '/') . '/' . $file;
42+
if (!is_readable($filePath) || !is_file($filePath)) {
43+
throw new \InvalidArgumentException(
44+
sprintf(
45+
'Dotenv: Environment file %s not found or not readable. ' .
46+
'Create file with your environment settings at %s',
47+
$file,
48+
$filePath
49+
)
50+
);
51+
}
52+
53+
// Read file into an array of lines with auto-detected line endings
54+
$autodetect = ini_get('auto_detect_line_endings');
55+
ini_set('auto_detect_line_endings', '1');
56+
$lines = file($filePath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
57+
ini_set('auto_detect_line_endings', $autodetect);
58+
59+
$data = [];
60+
61+
foreach ($lines as $line) {
62+
// Disregard comments
63+
if (strpos(trim($line), '#') === 0) {
64+
$data[] = $line;
65+
continue;
66+
}
67+
// Only use non-empty lines that look like setters
68+
if (strpos($line, '=') !== false) {
69+
$tmp = static::normaliseEnvironmentVariable($line, null);
70+
$data[] = $tmp;
71+
}
72+
}
73+
74+
return $data;
75+
}
76+
}

src/console/EnvController.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
/**
3+
* @author Dmytro Karpovych
4+
* @copyright 2016 NRE
5+
*/
6+
7+
8+
namespace nullref\core\console;
9+
10+
11+
use nullref\core\components\DotenvManger;
12+
use Yii;
13+
use yii\console\Controller;
14+
15+
class EnvController extends Controller
16+
{
17+
public function actionIndex()
18+
{
19+
DotenvManger::modify();
20+
}
21+
}

0 commit comments

Comments
 (0)