|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Macpaw\PostgresSchemaBundle\Command\Doctrine; |
| 6 | + |
| 7 | +use Doctrine\DBAL\Connection; |
| 8 | +use Error; |
| 9 | +use Symfony\Component\Console\Command\Command; |
| 10 | +use Symfony\Component\Console\Input\ArrayInput; |
| 11 | +use Symfony\Component\Console\Input\InputArgument; |
| 12 | +use Symfony\Component\Console\Input\InputInterface; |
| 13 | +use Symfony\Component\Console\Input\InputOption; |
| 14 | +use Symfony\Component\Console\Output\OutputInterface; |
| 15 | + |
| 16 | +abstract class AbstractNestingDoctrineSchemaCommand extends AbstractDoctrineSchemaCommand |
| 17 | +{ |
| 18 | + public function __construct( |
| 19 | + string $commandName, |
| 20 | + private readonly Command $parentCommand, |
| 21 | + Connection $connection, |
| 22 | + ) { |
| 23 | + parent::__construct($commandName, $connection); |
| 24 | + } |
| 25 | + |
| 26 | + protected function configure(): void |
| 27 | + { |
| 28 | + parent::configure(); |
| 29 | + |
| 30 | + foreach ($this->parentCommand->getDefinition()->getArguments() as $argument) { |
| 31 | + $this->addArgument( |
| 32 | + $argument->getName(), |
| 33 | + $argument->isRequired() ? InputArgument::REQUIRED : InputArgument::OPTIONAL, |
| 34 | + $argument->getDescription(), |
| 35 | + $argument->getDefault(), |
| 36 | + ); |
| 37 | + } |
| 38 | + |
| 39 | + foreach ($this->parentCommand->getDefinition()->getOptions() as $option) { |
| 40 | + $this->addOption( |
| 41 | + $option->getName(), |
| 42 | + $option->getShortcut(), |
| 43 | + $option->isValueRequired() ? InputOption::VALUE_REQUIRED : InputOption::VALUE_OPTIONAL, |
| 44 | + $option->getDescription(), |
| 45 | + $option->getDefault(), |
| 46 | + ); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + protected function runCommand(string $commandName, InputInterface $input, OutputInterface $output): int |
| 51 | + { |
| 52 | + $application = $this->getApplication(); |
| 53 | + |
| 54 | + if ($application === null) { |
| 55 | + throw new Error('Application is not available'); |
| 56 | + } |
| 57 | + |
| 58 | + $command = $application->find($commandName); |
| 59 | + |
| 60 | + $arguments = []; |
| 61 | + foreach ($input->getArguments() as $name => $value) { |
| 62 | + if ($value === null) { |
| 63 | + continue; |
| 64 | + } |
| 65 | + |
| 66 | + if ($name === 'schema' || $name === 'command') { |
| 67 | + continue; |
| 68 | + } |
| 69 | + |
| 70 | + if ($this->getDefinition()->getArguments()[$name]->getDefault() === $value) { |
| 71 | + continue; |
| 72 | + } |
| 73 | + |
| 74 | + $arguments[$name] = $value; |
| 75 | + } |
| 76 | + |
| 77 | + $options = []; |
| 78 | + foreach ($input->getOptions() as $name => $value) { |
| 79 | + if ($value === null) { |
| 80 | + continue; |
| 81 | + } |
| 82 | + |
| 83 | + if ($this->getDefinition()->getOptions()[$name]->getDefault() === $value) { |
| 84 | + continue; |
| 85 | + } |
| 86 | + |
| 87 | + $options['--' . $name] = $value; |
| 88 | + } |
| 89 | + |
| 90 | + $commandInput = new ArrayInput([ |
| 91 | + ...$arguments, |
| 92 | + ...$options, |
| 93 | + ]); |
| 94 | + |
| 95 | + if ($input->getOption('no-interaction') === true) { |
| 96 | + $commandInput->setInteractive(false); |
| 97 | + } |
| 98 | + |
| 99 | + return $command->run($commandInput, $output); |
| 100 | + } |
| 101 | +} |
0 commit comments