Skip to content

Commit 436bad9

Browse files
committed
Moving guts of method to a private method - no real change
1 parent 94a3e56 commit 436bad9

File tree

1 file changed

+65
-54
lines changed

1 file changed

+65
-54
lines changed

src/Configurator/AddLinesConfigurator.php

Lines changed: 65 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -25,61 +25,11 @@ class AddLinesConfigurator extends AbstractConfigurator
2525

2626
public function configure(Recipe $recipe, $config, Lock $lock, array $options = []): void
2727
{
28-
foreach ($config as $patch) {
29-
if (!isset($patch['file'])) {
30-
$this->write(sprintf('The "file" key is required for the "add-lines" configurator for recipe "%s". Skipping', $recipe->getName()));
31-
32-
continue;
33-
}
34-
35-
if (isset($patch['requires']) && !$this->isPackageInstalled($patch['requires'])) {
36-
continue;
37-
}
38-
39-
if (!isset($patch['content'])) {
40-
$this->write(sprintf('The "content" key is required for the "add-lines" configurator for recipe "%s". Skipping', $recipe->getName()));
41-
42-
continue;
43-
}
44-
$content = $patch['content'];
45-
46-
$file = $this->path->concatenate([$this->options->get('root-dir'), $patch['file']]);
47-
$warnIfMissing = isset($patch['warn_if_missing']) && $patch['warn_if_missing'];
48-
if (!is_file($file)) {
49-
$this->write([
50-
sprintf('Could not add lines to file <info>%s</info> as it does not exist. Missing lines:', $patch['file']),
51-
'<comment>"""</comment>',
52-
$content,
53-
'<comment>"""</comment>',
54-
'',
55-
], $warnIfMissing ? IOInterface::NORMAL : IOInterface::VERBOSE);
28+
$changes = $this->getConfigureFileChanges($recipe, $config);
5629

57-
continue;
58-
}
59-
60-
$this->write(sprintf('Patching file "%s"', $patch['file']));
61-
62-
if (!isset($patch['position'])) {
63-
$this->write(sprintf('The "position" key is required for the "add-lines" configurator for recipe "%s". Skipping', $recipe->getName()));
64-
65-
continue;
66-
}
67-
$position = $patch['position'];
68-
if (!\in_array($position, self::VALID_POSITIONS, true)) {
69-
$this->write(sprintf('The "position" key must be one of "%s" for the "add-lines" configurator for recipe "%s". Skipping', implode('", "', self::VALID_POSITIONS), $recipe->getName()));
70-
71-
continue;
72-
}
73-
74-
if (self::POSITION_AFTER_TARGET === $position && !isset($patch['target'])) {
75-
$this->write(sprintf('The "target" key is required when "position" is "%s" for the "add-lines" configurator for recipe "%s". Skipping', self::POSITION_AFTER_TARGET, $recipe->getName()));
76-
77-
continue;
78-
}
79-
$target = isset($patch['target']) ? $patch['target'] : null;
80-
81-
$newContents = $this->getPatchedContents($file, $content, $position, $target, $warnIfMissing);
82-
file_put_contents($file, $newContents);
30+
foreach ($changes as $file => $change) {
31+
$this->write(sprintf('[add-lines] Patching file "%s"', $file));
32+
file_put_contents($file, $change);
8333
}
8434
}
8535

@@ -147,6 +97,67 @@ public function update(RecipeUpdate $recipeUpdate, array $originalConfig, array
14797
$this->configure($recipeUpdate->getNewRecipe(), $filteredNewConfig, $recipeUpdate->getLock());
14898
}
14999

100+
public function getConfigureFileChanges(Recipe $recipe, $config): array
101+
{
102+
$changes = [];
103+
foreach ($config as $patch) {
104+
if (!isset($patch['file'])) {
105+
$this->write(sprintf('The "file" key is required for the "add-lines" configurator for recipe "%s". Skipping', $recipe->getName()));
106+
107+
continue;
108+
}
109+
110+
if (isset($patch['requires']) && !$this->isPackageInstalled($patch['requires'])) {
111+
continue;
112+
}
113+
114+
if (!isset($patch['content'])) {
115+
$this->write(sprintf('The "content" key is required for the "add-lines" configurator for recipe "%s". Skipping', $recipe->getName()));
116+
117+
continue;
118+
}
119+
$content = $patch['content'];
120+
121+
$file = $this->path->concatenate([$this->options->get('root-dir'), $patch['file']]);
122+
$warnIfMissing = isset($patch['warn_if_missing']) && $patch['warn_if_missing'];
123+
if (!is_file($file)) {
124+
$this->write([
125+
sprintf('Could not add lines to file <info>%s</info> as it does not exist. Missing lines:', $patch['file']),
126+
'<comment>"""</comment>',
127+
$content,
128+
'<comment>"""</comment>',
129+
'',
130+
], $warnIfMissing ? IOInterface::NORMAL : IOInterface::VERBOSE);
131+
132+
continue;
133+
}
134+
135+
if (!isset($patch['position'])) {
136+
$this->write(sprintf('The "position" key is required for the "add-lines" configurator for recipe "%s". Skipping', $recipe->getName()));
137+
138+
continue;
139+
}
140+
$position = $patch['position'];
141+
if (!\in_array($position, self::VALID_POSITIONS, true)) {
142+
$this->write(sprintf('The "position" key must be one of "%s" for the "add-lines" configurator for recipe "%s". Skipping', implode('", "', self::VALID_POSITIONS), $recipe->getName()));
143+
144+
continue;
145+
}
146+
147+
if (self::POSITION_AFTER_TARGET === $position && !isset($patch['target'])) {
148+
$this->write(sprintf('The "target" key is required when "position" is "%s" for the "add-lines" configurator for recipe "%s". Skipping', self::POSITION_AFTER_TARGET, $recipe->getName()));
149+
150+
continue;
151+
}
152+
$target = isset($patch['target']) ? $patch['target'] : null;
153+
154+
$newContents = $this->getPatchedContents($file, $content, $position, $target, $warnIfMissing);
155+
$changes[$file] = $newContents;
156+
}
157+
158+
return $changes;
159+
}
160+
150161
private function getPatchedContents(string $file, string $value, string $position, ?string $target, bool $warnIfMissing): string
151162
{
152163
$fileContents = file_get_contents($file);

0 commit comments

Comments
 (0)