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