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+ }
0 commit comments