-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathDotenvWriter.php
180 lines (161 loc) · 4.17 KB
/
DotenvWriter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php namespace Jackiedo\DotenvEditor;
use Jackiedo\DotenvEditor\Contracts\DotenvFormatter as DotenvFormatterContract;
use Jackiedo\DotenvEditor\Contracts\DotenvWriter as DotenvWriterContract;
use Jackiedo\DotenvEditor\Exceptions\UnableWriteToFileException;
/**
* The DotenvWriter writer.
*
* @package Jackiedo\DotenvEditor
* @author Jackie Do <anhvudo@gmail.com>
*/
class DotenvWriter implements DotenvWriterContract
{
/**
* The content buffer
*
* @var string
*/
protected $buffer;
/**
* The formatter instance
*
* @var \Jackiedo\DotenvEditor\DotenvFormatter
*/
protected $formatter;
/**
* Create a new writer instance
*
* @param Jackiedo\DotenvEditor\Contracts\DotenvFormatter $formatter
*/
public function __construct(DotenvFormatterContract $formatter)
{
$this->formatter = $formatter;
}
/**
* Tests file for writability. If the file doesn't exist, check
* the parent directory for writability so the file can be created.
*
* @throws \Jackiedo\DotenvEditor\Exceptions\UnableWriteToFileException
*
* @return void
*/
protected function ensureFileIsWritable($filePath)
{
if ((is_file($filePath) && !is_writable($filePath)) || (!is_file($filePath) && !is_writable(dirname($filePath)))) {
throw new UnableWriteToFileException(sprintf('Unable to write to the file at %s.', $filePath));
}
}
/**
* Set buffer with content
*
* @param string $content
*
* @return DotenvWriter
*/
public function setBuffer($content)
{
$this->buffer = trim($content) . PHP_EOL;
return $this;
}
/**
* Return content in buffer
*
* @return string
*/
public function getBuffer()
{
return $this->buffer;
}
/**
* Append new line to buffer
*
* @param string|null $text
*
* @return DotenvWriter
*/
protected function appendLine($text = null)
{
$this->buffer .= $text . PHP_EOL;
return $this;
}
/**
* Append empty line to buffer
*
* @return DotenvWriter
*/
public function appendEmptyLine()
{
return $this->appendLine();
}
/**
* Append comment line to buffer
*
* @param string $comment
*
* @return DotenvWriter
*/
public function appendCommentLine($comment)
{
return $this->appendLine('# '.$comment);
}
/**
* Append one setter to buffer
*
* @param string $key
* @param string|null $value
* @param string|null $comment
* @param boolean $export
*
* @return DotenvWriter
*/
public function appendSetter($key, $value = null, $comment = null, $export = false)
{
$line = $this->formatter->formatSetterLine($key, $value, $comment, $export);
return $this->appendLine($line);
}
/**
* Update one setter in buffer
*
* @param string $key
* @param string|null $value
* @param string|null $comment
* @param boolean $export
*
* @return DotenvWriter
*/
public function updateSetter($key, $value = null, $comment = null, $export = false)
{
$pattern = "/^(export\h)?\h*{$key}=.*/m";
$line = $this->formatter->formatSetterLine($key, $value, $comment, $export);
$this->buffer = preg_replace_callback($pattern, function () use ($line) {
return $line;
}, $this->buffer);
return $this;
}
/**
* Delete one setter in buffer
*
* @param string $key
*
* @return DotenvWriter
*/
public function deleteSetter($key)
{
$pattern = "/^(export\h)?\h*{$key}=.*\n/m";
$this->buffer = preg_replace($pattern, null, $this->buffer);
return $this;
}
/**
* Save buffer to special file path
*
* @param string $filePath
*
* @return DotenvWriter
*/
public function save($filePath)
{
$this->ensureFileIsWritable($filePath);
file_put_contents($filePath, $this->buffer);
return $this;
}
}