1+ <?php
2+
3+ namespace Codervio \Envmanager \Editor ;
4+
5+ use ArrayStore ;
6+ use Formatter ;
7+
8+ class EnvWriter
9+ {
10+ protected $ buffer ;
11+
12+ protected $ arr ;
13+
14+ protected $ forceclear = false ;
15+
16+ public function __construct ()
17+ {
18+ $ this ->arr = new ArrayStore ();
19+ $ this ->formatter = new Formatter ;
20+ }
21+
22+ public function set ($ buffer )
23+ {
24+ if (is_null ($ buffer )) {
25+ return (string )$ buffer ;
26+ }
27+
28+ $ this ->buffer = $ buffer ;
29+ }
30+
31+ public function clearBuffer ()
32+ {
33+ unset($ this ->buffer );
34+ }
35+
36+ public function clearAll ()
37+ {
38+ $ this ->forceclear = true ;
39+ }
40+
41+ private function ensureForcedClear ()
42+ {
43+ if ($ this ->forceclear ) {
44+ unset($ this ->buffer );
45+ }
46+ }
47+
48+ public function get ()
49+ {
50+ $ this ->ensureForcedClear ();
51+
52+ $ result = '' ;
53+
54+ foreach ($ this ->arr ->getArrayCopy () as $ value ) {
55+
56+ if (is_null ($ value )) {
57+ $ result .= $ this ->formatter ->addEmptyLine ();
58+ } else if (isset ($ value ['comment ' ]) && (!isset ($ value ['key ' ]))) {
59+ $ result .= $ this ->formatter ->formatComment ($ value ['comment ' ]).PHP_EOL ;
60+ } else {
61+ $ result .= $ this ->formatter ->formatSetter ($ value ).PHP_EOL ;
62+ }
63+ }
64+
65+ return $ result ;
66+ }
67+
68+ public function put ($ key , $ value = null , $ comment = null , $ export = false )
69+ {
70+ // handle put to array buffer
71+
72+ $ packArray = compact ('key ' , 'value ' , 'comment ' , 'export ' );
73+
74+ $ result = $ this ->formatter ->setKeys ($ packArray );
75+
76+ $ this ->arr ->offsetSet ($ key , $ result );
77+ }
78+
79+ public function appendComment ($ comment )
80+ {
81+ $ this ->arr ->append (array ('comment ' => $ comment ));
82+ }
83+
84+ public function appendLine ()
85+ {
86+ $ this ->arr ->append (null );
87+ }
88+
89+ public function remove ($ key )
90+ {
91+ if (!$ this ->arr ->offsetExists ($ key )) {
92+ return false ;
93+ }
94+
95+ $ this ->arr ->offsetUnset ($ key );
96+ }
97+
98+ public function removeComment ($ removeKey )
99+ {
100+ $ arrays = $ this ->arr ->getArrayCopy ();
101+
102+ foreach ($ arrays as $ key => $ value )
103+ {
104+
105+ if (isset ($ value ['comment ' ])) {
106+
107+ if ($ value ['comment ' ] === $ removeKey ) {
108+
109+ $ this ->arr ->offsetUnset ($ key );
110+ return true ;
111+ }
112+ }
113+ }
114+
115+ return false ;
116+ }
117+ }
0 commit comments