-
Notifications
You must be signed in to change notification settings - Fork 42
/
ReplaceBlock.php
192 lines (170 loc) · 4.29 KB
/
ReplaceBlock.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
181
182
183
184
185
186
187
188
189
190
191
192
<?php
declare(strict_types=1);
namespace EcEuropa\Toolkit\Task\File;
use Robo\Exception\TaskException;
use Robo\Result;
use Robo\Task\BaseTask;
/**
* Replace a block of content in given file.
*
* ``` php
* <?php
* // Insert content between the 'start' and 'end'.
* $this->taskReplaceBlock('file.txt')
* ->start('#block-start')
* ->end('#block-end')
* ->content('This content will be between the start and the end')
* ->run();
* // Insert content between the 'start' and 'end', and exclude 'start' and 'end'.
* $this->taskReplaceBlock('file.txt')
* ->start('#block-start')
* ->end('#block-end')
* ->excludeStartEnd()
* ->content('This content will be between the start and the end')
* ->run();
* ?>
* ```
*/
class ReplaceBlock extends BaseTask
{
/**
* The file to process.
*
* @var string
*/
protected string $filename;
/**
* The start string to match.
*
* @var string
*/
protected string $start;
/**
* The end string to match.
*
* @var string
*/
protected string $end;
/**
* The content to write into the file.
*
* @var string
*/
protected string $content = '';
/**
* If true, the start and end matches will be removed.
*
* @var bool
*/
private bool $excludeStartEnd = false;
/**
* Class constructor.
*/
public function __construct(string $filename = '')
{
if (!empty($filename)) {
$this->filename($filename);
}
}
/**
* Set the filename.
*
* @param string $filename
* The file to process.
*
* @return $this
*
* @throws \Robo\Exception\TaskException
*/
public function filename(string $filename)
{
if (!file_exists($filename)) {
throw new TaskException(__CLASS__, "The file $filename could not be found.");
}
$this->filename = $filename;
return $this;
}
/**
* Set the start block.
*
* @param string $start
* The start block.
*
* @return $this
*/
public function start(string $start)
{
$this->start = $start;
return $this;
}
/**
* Set the end block.
*
* @param string $end
* The end block.
*
* @return $this
*/
public function end(string $end)
{
$this->end = $end;
return $this;
}
/**
* Set the content.
*
* @param string $content
* The content to insert.
*
* @return $this
*/
public function content(string $content)
{
$this->content = $content;
return $this;
}
/**
* Mark the start and end to be removed.
*
* @param bool $exclude
* Whether to exclude the 'start' and 'end'.
*
* @return $this
*/
public function excludeStartEnd(bool $exclude = true)
{
$this->excludeStartEnd = $exclude;
return $this;
}
/**
* Run the task.
*/
public function run()
{
$message = "You must provide a '%s' value.";
if (empty($this->filename)) {
return Result::error($this, sprintf($message, 'filename'));
}
if (empty($this->start)) {
return Result::error($this, sprintf($message, 'start'));
}
if (empty($this->end)) {
return Result::error($this, sprintf($message, 'end'));
}
$pattern = '~(' . preg_quote($this->start) . ')(.+?)(' . preg_quote($this->end) . ')~s';
$file = file_get_contents($this->filename);
if (!$this->excludeStartEnd) {
$this->content = '\1' . $this->content . '\3';
}
$result = preg_replace($pattern, $this->content, $file, -1, $count);
if ($count > 0) {
if (file_put_contents($this->filename, $result) === false) {
return Result::error($this, sprintf('Error writing to file %s.', $this->filename));
}
$this->printTaskSuccess('{filename} updated. {count} items replaced', ['filename' => $this->filename, 'count' => $count]);
} else {
$this->printTaskInfo('{filename} unchanged. {count} items replaced', ['filename' => $this->filename, 'count' => $count]);
}
return Result::success($this);
}
}