Skip to content

Commit 21cee19

Browse files
authored
Make configuration a top-level option (#2375)
1 parent 07eb298 commit 21cee19

4 files changed

Lines changed: 83 additions & 10 deletions

File tree

src/Phinx/Console/Command/AbstractCommand.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ abstract class AbstractCommand extends Command
107107
*/
108108
protected function configure(): void
109109
{
110-
$this->addOption('--configuration', '-c', InputOption::VALUE_REQUIRED, 'The configuration file to load');
111110
$this->addOption('--parser', '-p', InputOption::VALUE_REQUIRED, 'Parser used to read the config file. Defaults to YAML');
112111
$this->addOption('--no-info', null, InputOption::VALUE_NONE, 'Hides all debug information');
113112
}

src/Phinx/Console/Command/Init.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,19 +88,34 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8888
*/
8989
protected function resolvePath(InputInterface $input, string $format): string
9090
{
91-
// get the migration path from the config
92-
$path = (string)$input->getArgument('path');
93-
9491
if (!in_array($format, static::$supportedFormats, true)) {
9592
throw new InvalidArgumentException(sprintf(
9693
'Invalid format "%s". Format must be either ' . implode(', ', static::$supportedFormats) . '.',
9794
$format,
9895
));
9996
}
10097

101-
// Fallback
98+
// We either get the path to where to create the config path by:
99+
// 1. The path argument if set
100+
// 2. The configuration option if set
101+
// 3. Fallback to a default path of the current directory
102+
$path = (string)$input->getArgument('path');
103+
102104
if (!$path) {
103-
$path = getcwd() . DIRECTORY_SEPARATOR . self::FILE_NAME . '.' . $format;
105+
$path = $input->hasOption('configuration') ? (string)$input->getOption('configuration') : null;
106+
if ($path) {
107+
$path = (string)$input->getOption('configuration');
108+
if (DIRECTORY_SEPARATOR === '/') {
109+
$isAbsolute = ($path[0] === '/');
110+
} else {
111+
$isAbsolute = (preg_match('/^[a-zA-Z]:\\\\/', $path) === 1 || $path[0] === '\\');
112+
}
113+
if (!$isAbsolute) {
114+
$path = getcwd() . DIRECTORY_SEPARATOR . $path;
115+
}
116+
} else {
117+
$path = getcwd() . DIRECTORY_SEPARATOR . self::FILE_NAME . '.' . $format;
118+
}
104119
}
105120

106121
// Adding file name if necessary

src/Phinx/Console/PhinxApplication.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
use Phinx\Console\Command\Status;
2121
use Phinx\Console\Command\Test;
2222
use Symfony\Component\Console\Application;
23+
use Symfony\Component\Console\Input\InputDefinition;
2324
use Symfony\Component\Console\Input\InputInterface;
25+
use Symfony\Component\Console\Input\InputOption;
2426
use Symfony\Component\Console\Output\OutputInterface;
2527

2628
/**
@@ -54,6 +56,19 @@ public function __construct()
5456
]);
5557
}
5658

59+
/**
60+
* Setup default input definition.
61+
*
62+
* @return \Symfony\Component\Console\Input\InputDefinition the overridden input definition.
63+
*/
64+
protected function getDefaultInputDefinition(): InputDefinition
65+
{
66+
$definition = parent::getDefaultInputDefinition();
67+
$definition->addOption(new InputOption('--configuration', '-c', InputOption::VALUE_REQUIRED, 'The configuration file to load'));
68+
69+
return $definition;
70+
}
71+
5772
/**
5873
* Runs the current application.
5974
*

tests/Phinx/Console/Command/InitTest.php

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,53 @@ public function testCustomNameConfigIsWritten($format)
8282
$this->writeConfig(uniqid() . $format);
8383
}
8484

85+
public function configurationOptionDataProvider(): array
86+
{
87+
return [
88+
['phinx.php'],
89+
[sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'phinx.php'],
90+
];
91+
}
92+
93+
/**
94+
* @dataProvider configurationOptionDataProvider
95+
*/
96+
public function testConfigurationOption($configPath): void
97+
{
98+
$currentDir = getcwd();
99+
$expectedPath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'phinx.php';
100+
try {
101+
chdir(sys_get_temp_dir());
102+
$application = new PhinxApplication();
103+
$application->add(new Init());
104+
$command = $application->find('init');
105+
$commandTester = new CommandTester($command);
106+
107+
$command = [
108+
'--configuration' => $configPath,
109+
'command' => $command->getName(),
110+
];
111+
112+
$exitCode = $commandTester->execute($command, ['decorated' => false]);
113+
$this->assertEquals(AbstractCommand::CODE_SUCCESS, $exitCode);
114+
115+
$this->assertStringContainsString(
116+
"created $expectedPath",
117+
$commandTester->getDisplay(),
118+
);
119+
120+
$this->assertFileExists(
121+
$expectedPath,
122+
'Phinx configuration not existent',
123+
);
124+
} finally {
125+
chdir($currentDir);
126+
}
127+
}
128+
85129
public function testDefaults()
86130
{
87-
$current_dir = getcwd();
131+
$currentDir = getcwd();
88132

89133
try {
90134
chdir(sys_get_temp_dir());
@@ -107,13 +151,13 @@ public function testDefaults()
107151
'Phinx configuration not existent',
108152
);
109153
} finally {
110-
chdir($current_dir);
154+
chdir($currentDir);
111155
}
112156
}
113157

114158
public function testYamlFormat()
115159
{
116-
$current_dir = getcwd();
160+
$currentDir = getcwd();
117161

118162
try {
119163
chdir(sys_get_temp_dir());
@@ -136,7 +180,7 @@ public function testYamlFormat()
136180
'Phinx configuration not existent',
137181
);
138182
} finally {
139-
chdir($current_dir);
183+
chdir($currentDir);
140184
}
141185
}
142186

0 commit comments

Comments
 (0)