|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Flow\Bridge\Symfony\PostgreSqlBundle\Command; |
| 6 | + |
| 7 | +use function Flow\Filesystem\DSL\{native_local_filesystem, path}; |
| 8 | +use function Flow\PostgreSql\DSL\sql_format; |
| 9 | +use function Flow\Types\DSL\type_string; |
| 10 | +use Flow\Filesystem\Local\NativeLocalFilesystem; |
| 11 | +use Flow\Filesystem\Path as FsPath; |
| 12 | +use Symfony\Component\Console\Attribute\AsCommand; |
| 13 | +use Symfony\Component\Console\Command\Command; |
| 14 | +use Symfony\Component\Console\Input\{InputArgument, InputInterface, InputOption}; |
| 15 | +use Symfony\Component\Console\Output\OutputInterface; |
| 16 | + |
| 17 | +#[AsCommand(name: 'flow:sql:format', description: 'Format SQL using the flow-php/postgresql parser. Accepts a raw SQL string or a --path with glob support.')] |
| 18 | +final class FormatSqlCommand extends Command |
| 19 | +{ |
| 20 | + private readonly NativeLocalFilesystem $filesystem; |
| 21 | + |
| 22 | + public function __construct() |
| 23 | + { |
| 24 | + parent::__construct(); |
| 25 | + $this->filesystem = native_local_filesystem(); |
| 26 | + } |
| 27 | + |
| 28 | + protected function configure() : void |
| 29 | + { |
| 30 | + $this |
| 31 | + ->addArgument('sql', InputArgument::OPTIONAL, 'Raw SQL string to format. Ignored when --path is used.') |
| 32 | + ->addOption('path', 'p', InputOption::VALUE_REQUIRED, 'Path, directory or glob pattern (only files with .sql extension are processed)') |
| 33 | + ->addOption('write', 'w', InputOption::VALUE_NONE, 'Write formatted SQL back to source files instead of printing to stdout') |
| 34 | + ->addOption('check', null, InputOption::VALUE_NONE, 'Exit with non-zero status when any input is not already formatted (does not modify files)'); |
| 35 | + } |
| 36 | + |
| 37 | + protected function execute(InputInterface $input, OutputInterface $output) : int |
| 38 | + { |
| 39 | + $pathOption = $input->getOption('path'); |
| 40 | + $sqlArgument = $input->getArgument('sql'); |
| 41 | + $write = (bool) $input->getOption('write'); |
| 42 | + $check = (bool) $input->getOption('check'); |
| 43 | + |
| 44 | + if ($pathOption === null && $sqlArgument === null) { |
| 45 | + $output->writeln('<error>Either a SQL string argument or --path option must be provided.</error>'); |
| 46 | + |
| 47 | + return Command::FAILURE; |
| 48 | + } |
| 49 | + |
| 50 | + if ($pathOption !== null) { |
| 51 | + return $this->formatPath(type_string()->assert($pathOption), $write, $check, $output); |
| 52 | + } |
| 53 | + |
| 54 | + $sql = type_string()->assert($sqlArgument); |
| 55 | + $formatted = sql_format($sql); |
| 56 | + |
| 57 | + if ($check) { |
| 58 | + if (\trim($formatted) !== \trim($sql)) { |
| 59 | + $output->writeln('<error>SQL is not formatted.</error>'); |
| 60 | + |
| 61 | + return Command::FAILURE; |
| 62 | + } |
| 63 | + |
| 64 | + $output->writeln('<info>SQL is already formatted.</info>'); |
| 65 | + |
| 66 | + return Command::SUCCESS; |
| 67 | + } |
| 68 | + |
| 69 | + $output->writeln($formatted); |
| 70 | + |
| 71 | + return Command::SUCCESS; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @return list<FsPath> |
| 76 | + */ |
| 77 | + private function collectSqlFiles(FsPath $path) : array |
| 78 | + { |
| 79 | + $status = $this->filesystem->status($path); |
| 80 | + |
| 81 | + if ($status !== null && $status->isDirectory()) { |
| 82 | + $path = path(\rtrim($path->path(), '/') . '/**/*.sql', $path->options()); |
| 83 | + } |
| 84 | + |
| 85 | + $files = []; |
| 86 | + |
| 87 | + foreach ($this->filesystem->list($path) as $file) { |
| 88 | + if (!$file->isFile()) { |
| 89 | + continue; |
| 90 | + } |
| 91 | + |
| 92 | + if ($file->path->extension() !== 'sql') { |
| 93 | + continue; |
| 94 | + } |
| 95 | + |
| 96 | + $files[] = $file->path; |
| 97 | + } |
| 98 | + |
| 99 | + return $files; |
| 100 | + } |
| 101 | + |
| 102 | + private function formatPath(string $pathString, bool $write, bool $check, OutputInterface $output) : int |
| 103 | + { |
| 104 | + $files = $this->collectSqlFiles(path($pathString)); |
| 105 | + |
| 106 | + if ($files === []) { |
| 107 | + $output->writeln(\sprintf('<comment>No .sql files found at: %s</comment>', $pathString)); |
| 108 | + |
| 109 | + return Command::SUCCESS; |
| 110 | + } |
| 111 | + |
| 112 | + $unformatted = 0; |
| 113 | + $formattedCount = 0; |
| 114 | + |
| 115 | + foreach ($files as $file) { |
| 116 | + $original = $this->filesystem->readFrom($file)->content(); |
| 117 | + |
| 118 | + try { |
| 119 | + $formatted = sql_format($original); |
| 120 | + } catch (\Throwable $e) { |
| 121 | + $output->writeln(\sprintf('<error>Failed to format %s: %s</error>', $file->path(), $e->getMessage())); |
| 122 | + |
| 123 | + return Command::FAILURE; |
| 124 | + } |
| 125 | + |
| 126 | + $isDifferent = \trim($formatted) !== \trim($original); |
| 127 | + |
| 128 | + if ($check) { |
| 129 | + if ($isDifferent) { |
| 130 | + $unformatted++; |
| 131 | + $output->writeln(\sprintf('<error>Not formatted: %s</error>', $file->path())); |
| 132 | + } |
| 133 | + |
| 134 | + continue; |
| 135 | + } |
| 136 | + |
| 137 | + if ($write) { |
| 138 | + if ($isDifferent) { |
| 139 | + $this->filesystem->writeTo($file)->append($formatted)->close(); |
| 140 | + $formattedCount++; |
| 141 | + $output->writeln(\sprintf('<info>Formatted: %s</info>', $file->path())); |
| 142 | + } |
| 143 | + |
| 144 | + continue; |
| 145 | + } |
| 146 | + |
| 147 | + $output->writeln(\sprintf('<comment>-- %s</comment>', $file->path())); |
| 148 | + $output->writeln($formatted); |
| 149 | + $output->writeln(''); |
| 150 | + } |
| 151 | + |
| 152 | + if ($check) { |
| 153 | + if ($unformatted > 0) { |
| 154 | + $output->writeln(\sprintf('<error>%d file(s) not formatted.</error>', $unformatted)); |
| 155 | + |
| 156 | + return Command::FAILURE; |
| 157 | + } |
| 158 | + |
| 159 | + $output->writeln(\sprintf('<info>All %d file(s) are properly formatted.</info>', \count($files))); |
| 160 | + |
| 161 | + return Command::SUCCESS; |
| 162 | + } |
| 163 | + |
| 164 | + if ($write) { |
| 165 | + $output->writeln(\sprintf('<info>%d file(s) reformatted, %d unchanged.</info>', $formattedCount, \count($files) - $formattedCount)); |
| 166 | + } |
| 167 | + |
| 168 | + return Command::SUCCESS; |
| 169 | + } |
| 170 | +} |
0 commit comments