From 797da7a9debbc55053c95b34127c1dcc8f4fed02 Mon Sep 17 00:00:00 2001 From: Matthew Peveler Date: Tue, 2 Sep 2025 15:39:09 +0000 Subject: [PATCH 1/4] Make configuration a top-level option --- src/Phinx/Console/Command/AbstractCommand.php | 1 - src/Phinx/Console/Command/Init.php | 24 +++++++++++++++---- src/Phinx/Console/PhinxApplication.php | 15 ++++++++++++ 3 files changed, 34 insertions(+), 6 deletions(-) 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..3924c7568 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,26 @@ 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; + if ($input->hasOption('configuration')) { + $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..576dc85f4 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 InputDefinition the overridden input definition. + */ + protected function getDefaultInputDefinition(): InputDefinition + { + $definition = parent::getDefaultInputDefinition(); + $definition->addOption(new \Symfony\Component\Console\Input\InputOption('--configuration', '-c', InputOption::VALUE_REQUIRED, 'The configuration file to load')); + + return $definition; + } + /** * Runs the current application. * From 74d65ded73929171019fad17375cc3c00088fd01 Mon Sep 17 00:00:00 2001 From: Matthew Peveler Date: Tue, 2 Sep 2025 15:52:53 +0000 Subject: [PATCH 2/4] phpcs feedback --- src/Phinx/Console/PhinxApplication.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Phinx/Console/PhinxApplication.php b/src/Phinx/Console/PhinxApplication.php index 576dc85f4..a5f523b21 100644 --- a/src/Phinx/Console/PhinxApplication.php +++ b/src/Phinx/Console/PhinxApplication.php @@ -22,7 +22,7 @@ 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\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; /** @@ -59,12 +59,12 @@ public function __construct() /** * Setup default input definition. * - * @return InputDefinition the overridden input definition. + * @return \Symfony\Component\Console\Input\InputDefinition the overridden input definition. */ protected function getDefaultInputDefinition(): InputDefinition { $definition = parent::getDefaultInputDefinition(); - $definition->addOption(new \Symfony\Component\Console\Input\InputOption('--configuration', '-c', InputOption::VALUE_REQUIRED, 'The configuration file to load')); + $definition->addOption(new InputOption('--configuration', '-c', InputOption::VALUE_REQUIRED, 'The configuration file to load')); return $definition; } From d3037045317b0507eb8196203462f0c04924e392 Mon Sep 17 00:00:00 2001 From: Matthew Peveler Date: Tue, 2 Sep 2025 15:56:10 +0000 Subject: [PATCH 3/4] fix using default --- src/Phinx/Console/Command/Init.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Phinx/Console/Command/Init.php b/src/Phinx/Console/Command/Init.php index 3924c7568..dd071e225 100644 --- a/src/Phinx/Console/Command/Init.php +++ b/src/Phinx/Console/Command/Init.php @@ -102,7 +102,8 @@ protected function resolvePath(InputInterface $input, string $format): string $path = (string)$input->getArgument('path'); if (!$path) { - if ($input->hasOption('configuration')) { + $path = $input->hasOption('configuration') ? (string)$input->getOption('configuration') : null; + if ($path) { $path = (string)$input->getOption('configuration'); if (DIRECTORY_SEPARATOR === '/') { $isAbsolute = ($path[0] === '/'); From 257956a914fd6020a64df2edefbc86a1596763f0 Mon Sep 17 00:00:00 2001 From: Matthew Peveler Date: Tue, 2 Sep 2025 16:10:47 +0000 Subject: [PATCH 4/4] add tests --- tests/Phinx/Console/Command/InitTest.php | 52 ++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 4 deletions(-) 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); } }