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