diff --git a/src/Phinx/Console/Command/AbstractCommand.php b/src/Phinx/Console/Command/AbstractCommand.php index 88cafc424..a55625968 100644 --- a/src/Phinx/Console/Command/AbstractCommand.php +++ b/src/Phinx/Console/Command/AbstractCommand.php @@ -107,7 +107,6 @@ abstract class AbstractCommand extends Command */ protected function configure(): void { - $this->addOption('--configuration', '-c', InputOption::VALUE_REQUIRED, 'The configuration file to load'); $this->addOption('--parser', '-p', InputOption::VALUE_REQUIRED, 'Parser used to read the config file. Defaults to YAML'); $this->addOption('--no-info', null, InputOption::VALUE_NONE, 'Hides all debug information'); } diff --git a/src/Phinx/Console/Command/Init.php b/src/Phinx/Console/Command/Init.php index 3b634f72d..dd071e225 100644 --- a/src/Phinx/Console/Command/Init.php +++ b/src/Phinx/Console/Command/Init.php @@ -88,9 +88,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int */ protected function resolvePath(InputInterface $input, string $format): string { - // get the migration path from the config - $path = (string)$input->getArgument('path'); - if (!in_array($format, static::$supportedFormats, true)) { throw new InvalidArgumentException(sprintf( 'Invalid format "%s". Format must be either ' . implode(', ', static::$supportedFormats) . '.', @@ -98,9 +95,27 @@ protected function resolvePath(InputInterface $input, string $format): string )); } - // Fallback + // We either get the path to where to create the config path by: + // 1. The path argument if set + // 2. The configuration option if set + // 3. Fallback to a default path of the current directory + $path = (string)$input->getArgument('path'); + if (!$path) { - $path = getcwd() . DIRECTORY_SEPARATOR . self::FILE_NAME . '.' . $format; + $path = $input->hasOption('configuration') ? (string)$input->getOption('configuration') : null; + if ($path) { + $path = (string)$input->getOption('configuration'); + if (DIRECTORY_SEPARATOR === '/') { + $isAbsolute = ($path[0] === '/'); + } else { + $isAbsolute = (preg_match('/^[a-zA-Z]:\\\\/', $path) === 1 || $path[0] === '\\'); + } + if (!$isAbsolute) { + $path = getcwd() . DIRECTORY_SEPARATOR . $path; + } + } else { + $path = getcwd() . DIRECTORY_SEPARATOR . self::FILE_NAME . '.' . $format; + } } // Adding file name if necessary diff --git a/src/Phinx/Console/PhinxApplication.php b/src/Phinx/Console/PhinxApplication.php index ca71911bb..a5f523b21 100644 --- a/src/Phinx/Console/PhinxApplication.php +++ b/src/Phinx/Console/PhinxApplication.php @@ -20,7 +20,9 @@ use Phinx\Console\Command\Status; use Phinx\Console\Command\Test; use Symfony\Component\Console\Application; +use Symfony\Component\Console\Input\InputDefinition; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; /** @@ -54,6 +56,19 @@ public function __construct() ]); } + /** + * Setup default input definition. + * + * @return \Symfony\Component\Console\Input\InputDefinition the overridden input definition. + */ + protected function getDefaultInputDefinition(): InputDefinition + { + $definition = parent::getDefaultInputDefinition(); + $definition->addOption(new InputOption('--configuration', '-c', InputOption::VALUE_REQUIRED, 'The configuration file to load')); + + return $definition; + } + /** * Runs the current application. * diff --git a/tests/Phinx/Console/Command/InitTest.php b/tests/Phinx/Console/Command/InitTest.php index c7cc0134b..f7ea49f65 100644 --- a/tests/Phinx/Console/Command/InitTest.php +++ b/tests/Phinx/Console/Command/InitTest.php @@ -82,9 +82,53 @@ public function testCustomNameConfigIsWritten($format) $this->writeConfig(uniqid() . $format); } + public function configurationOptionDataProvider(): array + { + return [ + ['phinx.php'], + [sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'phinx.php'], + ]; + } + + /** + * @dataProvider configurationOptionDataProvider + */ + public function testConfigurationOption($configPath): void + { + $currentDir = getcwd(); + $expectedPath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'phinx.php'; + try { + chdir(sys_get_temp_dir()); + $application = new PhinxApplication(); + $application->add(new Init()); + $command = $application->find('init'); + $commandTester = new CommandTester($command); + + $command = [ + '--configuration' => $configPath, + 'command' => $command->getName(), + ]; + + $exitCode = $commandTester->execute($command, ['decorated' => false]); + $this->assertEquals(AbstractCommand::CODE_SUCCESS, $exitCode); + + $this->assertStringContainsString( + "created $expectedPath", + $commandTester->getDisplay(), + ); + + $this->assertFileExists( + $expectedPath, + 'Phinx configuration not existent', + ); + } finally { + chdir($currentDir); + } + } + public function testDefaults() { - $current_dir = getcwd(); + $currentDir = getcwd(); try { chdir(sys_get_temp_dir()); @@ -107,13 +151,13 @@ public function testDefaults() 'Phinx configuration not existent', ); } finally { - chdir($current_dir); + chdir($currentDir); } } public function testYamlFormat() { - $current_dir = getcwd(); + $currentDir = getcwd(); try { chdir(sys_get_temp_dir()); @@ -136,7 +180,7 @@ public function testYamlFormat() 'Phinx configuration not existent', ); } finally { - chdir($current_dir); + chdir($currentDir); } }