|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Flex; |
| 13 | + |
| 14 | +use Composer\IO\IOInterface; |
| 15 | +use Composer\Util\ProcessExecutor; |
| 16 | + |
| 17 | +/** |
| 18 | + * @author Maxime Hélias <[email protected]> |
| 19 | + */ |
| 20 | +class FilesManager |
| 21 | +{ |
| 22 | + private $io; |
| 23 | + protected $path; |
| 24 | + |
| 25 | + private $writtenFiles = []; |
| 26 | + private $files; |
| 27 | + |
| 28 | + public function __construct(IOInterface $io, Lock $lock, string $rootDir) |
| 29 | + { |
| 30 | + $this->io = $io; |
| 31 | + |
| 32 | + $this->path = new Path($rootDir); |
| 33 | + $this->files = array_count_values( |
| 34 | + array_map( |
| 35 | + function (string $file) { |
| 36 | + return realpath($file) ?: ''; |
| 37 | + }, array_reduce( |
| 38 | + array_column($lock->all(), 'files'), |
| 39 | + function (array $carry, array $package) { |
| 40 | + return array_merge($carry, $package); |
| 41 | + }, |
| 42 | + [] |
| 43 | + ) |
| 44 | + ) |
| 45 | + ); |
| 46 | + } |
| 47 | + |
| 48 | + public function shouldWriteFile(string $file, bool $overwrite, bool $skipQuestion): bool |
| 49 | + { |
| 50 | + if (isset($this->writtenFiles[$file])) { |
| 51 | + return false; |
| 52 | + } |
| 53 | + $this->writtenFiles[$file] = true; |
| 54 | + |
| 55 | + if (!file_exists($file)) { |
| 56 | + return true; |
| 57 | + } |
| 58 | + |
| 59 | + if (!$overwrite) { |
| 60 | + return false; |
| 61 | + } |
| 62 | + |
| 63 | + if (!filesize($file)) { |
| 64 | + return true; |
| 65 | + } |
| 66 | + |
| 67 | + if ($skipQuestion) { |
| 68 | + return true; |
| 69 | + } |
| 70 | + |
| 71 | + exec('git status --short --ignored --untracked-files=all -- '.ProcessExecutor::escape($file).' 2>&1', $output, $status); |
| 72 | + |
| 73 | + if (0 !== $status) { |
| 74 | + return $this->io->askConfirmation(\sprintf('Cannot determine the state of the "%s" file, overwrite anyway? [y/N] ', $file), false); |
| 75 | + } |
| 76 | + |
| 77 | + if (empty($output[0]) || preg_match('/^[ AMDRCU][ D][ \t]/', $output[0])) { |
| 78 | + return true; |
| 79 | + } |
| 80 | + |
| 81 | + $name = basename($file); |
| 82 | + $name = \strlen($output[0]) - \strlen($name) === strrpos($output[0], $name) ? substr($output[0], 3) : $name; |
| 83 | + |
| 84 | + return $this->io->askConfirmation(\sprintf('File "%s" has uncommitted changes, overwrite? [y/N] ', $name), false); |
| 85 | + } |
| 86 | + |
| 87 | + public function getRemovableFilesFromRecipeAndLock(Recipe $recipe): array |
| 88 | + { |
| 89 | + $removableFiles = $recipe->getFiles(); |
| 90 | + // Compare file paths by their real path to abstract OS differences |
| 91 | + foreach (array_keys($removableFiles) as $file) { |
| 92 | + $file = realpath($file); |
| 93 | + if (!isset($this->files[$file])) { |
| 94 | + continue; |
| 95 | + } |
| 96 | + |
| 97 | + --$this->files[$file]; |
| 98 | + |
| 99 | + if ($this->files[$file] <= 0) { |
| 100 | + unset($removableFiles[$file]); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + return $removableFiles; |
| 105 | + } |
| 106 | +} |
0 commit comments