|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Symfony\Flex\Configurator; |
| 4 | + |
| 5 | +use Composer\IO\IOInterface; |
| 6 | +use Symfony\Flex\Lock; |
| 7 | +use Symfony\Flex\Recipe; |
| 8 | +use Symfony\Flex\Update\RecipeUpdate; |
| 9 | + |
| 10 | +/** |
| 11 | + * @author Kevin Bond <[email protected]> |
| 12 | + * @author Ryan Weaver <[email protected]> |
| 13 | + */ |
| 14 | +class AddLinesConfigurator extends AbstractConfigurator |
| 15 | +{ |
| 16 | + private const POSITION_TOP = 'top'; |
| 17 | + private const POSITION_BOTTOM = 'bottom'; |
| 18 | + private const POSITION_AFTER_TARGET = 'after_target'; |
| 19 | + |
| 20 | + private const VALID_POSITIONS = [ |
| 21 | + self::POSITION_TOP, |
| 22 | + self::POSITION_BOTTOM, |
| 23 | + self::POSITION_AFTER_TARGET, |
| 24 | + ]; |
| 25 | + |
| 26 | + public function configure(Recipe $recipe, $config, Lock $lock, array $options = []): void |
| 27 | + { |
| 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); |
| 56 | + |
| 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 | + $this->patchFile($file, $content, $position, $target, $warnIfMissing); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + public function unconfigure(Recipe $recipe, $config, Lock $lock): void |
| 86 | + { |
| 87 | + foreach ($config as $patch) { |
| 88 | + if (!isset($patch['file'])) { |
| 89 | + $this->write(sprintf('The "file" key is required for the "add-lines" configurator for recipe "%s". Skipping', $recipe->getName())); |
| 90 | + |
| 91 | + continue; |
| 92 | + } |
| 93 | + |
| 94 | + // Ignore "requires": the target packages may have just become uninstalled. |
| 95 | + // Checking for a "content" match is enough. |
| 96 | + |
| 97 | + $file = $this->path->concatenate([$this->options->get('root-dir'), $patch['file']]); |
| 98 | + if (!is_file($file)) { |
| 99 | + continue; |
| 100 | + } |
| 101 | + |
| 102 | + if (!isset($patch['content'])) { |
| 103 | + $this->write(sprintf('The "content" key is required for the "add-lines" configurator for recipe "%s". Skipping', $recipe->getName())); |
| 104 | + |
| 105 | + continue; |
| 106 | + } |
| 107 | + $value = $patch['content']; |
| 108 | + |
| 109 | + $this->unPatchFile($file, $value); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + public function update(RecipeUpdate $recipeUpdate, array $originalConfig, array $newConfig): void |
| 114 | + { |
| 115 | + $originalConfig = array_filter($originalConfig, function ($item) { |
| 116 | + return !isset($item['requires']) || $this->isPackageInstalled($item['requires']); |
| 117 | + }); |
| 118 | + $newConfig = array_filter($newConfig, function ($item) { |
| 119 | + return !isset($item['requires']) || $this->isPackageInstalled($item['requires']); |
| 120 | + }); |
| 121 | + |
| 122 | + $filterDuplicates = function (array $sourceConfig, array $comparisonConfig) { |
| 123 | + $filtered = []; |
| 124 | + foreach ($sourceConfig as $sourceItem) { |
| 125 | + $found = false; |
| 126 | + foreach ($comparisonConfig as $comparisonItem) { |
| 127 | + if ($sourceItem['file'] === $comparisonItem['file'] && $sourceItem['content'] === $comparisonItem['content']) { |
| 128 | + $found = true; |
| 129 | + break; |
| 130 | + } |
| 131 | + } |
| 132 | + if (!$found) { |
| 133 | + $filtered[] = $sourceItem; |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + return $filtered; |
| 138 | + }; |
| 139 | + |
| 140 | + // remove any config where the file+value is the same before & after |
| 141 | + $filteredOriginalConfig = $filterDuplicates($originalConfig, $newConfig); |
| 142 | + $filteredNewConfig = $filterDuplicates($newConfig, $originalConfig); |
| 143 | + |
| 144 | + $this->unconfigure($recipeUpdate->getOriginalRecipe(), $filteredOriginalConfig, $recipeUpdate->getLock()); |
| 145 | + $this->configure($recipeUpdate->getNewRecipe(), $filteredNewConfig, $recipeUpdate->getLock()); |
| 146 | + } |
| 147 | + |
| 148 | + private function patchFile(string $file, string $value, string $position, ?string $target, bool $warnIfMissing) |
| 149 | + { |
| 150 | + $fileContents = file_get_contents($file); |
| 151 | + |
| 152 | + if (false !== strpos($fileContents, $value)) { |
| 153 | + return; // already includes value, skip |
| 154 | + } |
| 155 | + |
| 156 | + switch ($position) { |
| 157 | + case self::POSITION_BOTTOM: |
| 158 | + $fileContents .= "\n".$value; |
| 159 | + |
| 160 | + break; |
| 161 | + case self::POSITION_TOP: |
| 162 | + $fileContents = $value."\n".$fileContents; |
| 163 | + |
| 164 | + break; |
| 165 | + case self::POSITION_AFTER_TARGET: |
| 166 | + $lines = explode("\n", $fileContents); |
| 167 | + $targetFound = false; |
| 168 | + foreach ($lines as $key => $line) { |
| 169 | + if (false !== strpos($line, $target)) { |
| 170 | + array_splice($lines, $key + 1, 0, $value); |
| 171 | + $targetFound = true; |
| 172 | + |
| 173 | + break; |
| 174 | + } |
| 175 | + } |
| 176 | + $fileContents = implode("\n", $lines); |
| 177 | + |
| 178 | + if (!$targetFound) { |
| 179 | + $this->write([ |
| 180 | + sprintf('Could not add lines after "%s" as no such string was found in "%s". Missing lines:', $target, $file), |
| 181 | + '<comment>"""</comment>', |
| 182 | + $value, |
| 183 | + '<comment>"""</comment>', |
| 184 | + '', |
| 185 | + ], $warnIfMissing ? IOInterface::NORMAL : IOInterface::VERBOSE); |
| 186 | + } |
| 187 | + |
| 188 | + break; |
| 189 | + } |
| 190 | + |
| 191 | + file_put_contents($file, $fileContents); |
| 192 | + } |
| 193 | + |
| 194 | + private function unPatchFile(string $file, $value) |
| 195 | + { |
| 196 | + $fileContents = file_get_contents($file); |
| 197 | + |
| 198 | + if (false === strpos($fileContents, $value)) { |
| 199 | + return; // value already gone! |
| 200 | + } |
| 201 | + |
| 202 | + if (false !== strpos($fileContents, "\n".$value)) { |
| 203 | + $value = "\n".$value; |
| 204 | + } elseif (false !== strpos($fileContents, $value."\n")) { |
| 205 | + $value = $value."\n"; |
| 206 | + } |
| 207 | + |
| 208 | + $position = strpos($fileContents, $value); |
| 209 | + $fileContents = substr_replace($fileContents, '', $position, \strlen($value)); |
| 210 | + |
| 211 | + file_put_contents($file, $fileContents); |
| 212 | + } |
| 213 | + |
| 214 | + private function isPackageInstalled($packages): bool |
| 215 | + { |
| 216 | + if (\is_string($packages)) { |
| 217 | + $packages = [$packages]; |
| 218 | + } |
| 219 | + |
| 220 | + $installedRepo = $this->composer->getRepositoryManager()->getLocalRepository(); |
| 221 | + |
| 222 | + foreach ($packages as $packageName) { |
| 223 | + if (null === $installedRepo->findPackage($packageName, '*')) { |
| 224 | + return false; |
| 225 | + } |
| 226 | + } |
| 227 | + |
| 228 | + return true; |
| 229 | + } |
| 230 | +} |
0 commit comments