Skip to content

Commit 6969fb4

Browse files
committed
Initial commit
0 parents  commit 6969fb4

27 files changed

Lines changed: 1408 additions & 0 deletions

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
vendor/
2+
composer.lock
3+
phpunit.xml.dist

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CHANGELOG
2+
=========
3+
4+
1.0.0
5+
-----
6+
7+
* env editor

Editor/EnvWriter.php

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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+
}

Editor/Formatter.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Codervio\Envmanager\Editor;
4+
5+
class Formatter
6+
{
7+
protected $values = null;
8+
9+
public function setKeys($item)
10+
{
11+
if (array_key_exists('key', $item)) {
12+
$key = $item['key'];
13+
$value = array_key_exists('value', $item) ? $item['value'] : null;
14+
$comment = array_key_exists('comment', $item) ? $item['comment'] : null;
15+
$export = array_key_exists('export', $item) ? $item['export'] : null;
16+
17+
$packArray = compact('key', 'value', 'comment', 'export');
18+
19+
$this->values = $packArray;
20+
}
21+
22+
return $this->values;
23+
}
24+
25+
public function formatSetter($value)
26+
{
27+
return (string)"{$value['export']}{$value['key']}={$value['value']}{$value['comment']}";
28+
}
29+
30+
public function formatComment($comment)
31+
{
32+
return (string)'# '.$comment;
33+
}
34+
35+
public function addEmptyLine()
36+
{
37+
return (string)PHP_EOL;
38+
}
39+
}

EnvEditor.php

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
3+
namespace Codervio\Envmanager;
4+
5+
class EnvEditor extends EnvParser
6+
{
7+
private $content;
8+
9+
protected $writer;
10+
protected $reader;
11+
protected $context;
12+
13+
public function __construct($context)
14+
{
15+
new Prerequisities();
16+
17+
$this->writer = new EnvWriter();
18+
19+
if ($context instanceof EnvParser) {
20+
// $this->content = $content;
21+
22+
if ($context->loader->getLoadStatus()) {
23+
24+
$this->context = $context->loader->getFile();
25+
26+
$parsedValues = $context->parsercollector->getArrayCopy();
27+
28+
foreach ($parsedValues as $parsedKey => $parsedValue) {
29+
30+
$this->writer->put($parsedKey, $parsedValue);
31+
32+
}
33+
}
34+
35+
36+
} else {
37+
38+
$this->context = $context;
39+
40+
$loader = new Loader($context, array('env', 'main.env'));
41+
42+
$content = $loader->run();
43+
44+
if (!$content) {
45+
return null;
46+
}
47+
48+
$this->content = $content;
49+
}
50+
}
51+
52+
public function addEmptyLine()
53+
{
54+
$this->writer->appendLine();
55+
}
56+
57+
public function persist($key, $value = null, $comment = null, $export = false)
58+
{
59+
$this->writer->put($key, $value, $comment, $export);
60+
}
61+
62+
public function addComment($comment = null)
63+
{
64+
$this->writer->appendComment($comment);
65+
}
66+
67+
public function removeComment($value)
68+
{
69+
$this->writer->removeComment($value);
70+
}
71+
72+
public function remove($key)
73+
{
74+
$this->writer->remove($key);
75+
}
76+
77+
public function save()
78+
{
79+
return file_put_contents($this->context, $this->getContent());
80+
}
81+
82+
public function removeFile()
83+
{
84+
unlink($this->context);
85+
}
86+
87+
public function getContent()
88+
{
89+
return (string)$this->writer->get();
90+
}
91+
92+
public function clearContent()
93+
{
94+
$this->writer->clearBuffer();
95+
}
96+
97+
public function forceClear()
98+
{
99+
$this->writer->clearAll();
100+
}
101+
102+
}

0 commit comments

Comments
 (0)