-
Notifications
You must be signed in to change notification settings - Fork 888
Expand file tree
/
Copy pathInit.php
More file actions
173 lines (152 loc) · 5.3 KB
/
Init.php
File metadata and controls
173 lines (152 loc) · 5.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
declare(strict_types=1);
/**
* MIT License
* For full license information, please view the LICENSE file that was distributed with this source code.
*/
namespace Phinx\Console\Command;
use InvalidArgumentException;
use RuntimeException;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
#[AsCommand(name: 'init')]
class Init extends Command
{
protected const FILE_NAME = 'phinx';
/**
* @var string[]
*/
protected static array $supportedFormats = [
AbstractCommand::FORMAT_JSON,
AbstractCommand::FORMAT_YML_ALIAS,
AbstractCommand::FORMAT_YML,
AbstractCommand::FORMAT_PHP,
];
/**
* @var string|null
*/
// phpcs:ignore SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingNativeTypeHint
protected static $defaultName = 'init';
/**
* {@inheritDoc}
*
* @return void
*/
protected function configure(): void
{
$this->setDescription('Initialize the application for Phinx')
->addOption(
'--format',
'-f',
InputArgument::OPTIONAL,
'What format should we use to initialize?',
AbstractCommand::FORMAT_DEFAULT,
)
->addArgument('path', InputArgument::OPTIONAL, 'Which path should we initialize for Phinx?')
->setHelp(sprintf(
'%sInitializes the application for Phinx%s',
PHP_EOL,
PHP_EOL,
));
}
/**
* Initializes the application.
*
* @param \Symfony\Component\Console\Input\InputInterface $input Interface implemented by all input classes.
* @param \Symfony\Component\Console\Output\OutputInterface $output Interface implemented by all output classes.
* @return int 0 on success
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$format = strtolower($input->getOption('format'));
$path = $this->resolvePath($input, $format);
$this->writeConfig($path, $format);
$output->writeln("<info>created</info> {$path}");
return AbstractCommand::CODE_SUCCESS;
}
/**
* Return valid $path for Phinx's config file.
*
* @param \Symfony\Component\Console\Input\InputInterface $input Interface implemented by all input classes.
* @param string $format Format to resolve for
* @throws \InvalidArgumentException
* @return string
*/
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) . '.',
$format,
));
}
// Fallback
if (!$path) {
$path = getcwd() . DIRECTORY_SEPARATOR . self::FILE_NAME . '.' . $format;
}
// Adding file name if necessary
if (is_dir($path)) {
$path .= DIRECTORY_SEPARATOR . self::FILE_NAME . '.' . $format;
}
// Check if path is available
$dirname = dirname($path);
if (is_dir($dirname) && !is_file($path)) {
return $path;
}
// Path is valid, but file already exists
if (is_file($path)) {
throw new InvalidArgumentException(sprintf(
'Config file "%s" already exists.',
$path,
));
}
// Dir is invalid
throw new InvalidArgumentException(sprintf(
'Invalid path "%s" for config file.',
$path,
));
}
/**
* Writes Phinx's config in provided $path
*
* @param string $path Config file's path.
* @param string $format Format to use for config file
* @throws \InvalidArgumentException
* @throws \RuntimeException
* @return void
*/
protected function writeConfig(string $path, string $format = AbstractCommand::FORMAT_DEFAULT): void
{
// Check if dir is writable
$dirname = dirname($path);
if (!is_writable($dirname)) {
throw new InvalidArgumentException(sprintf(
'The directory "%s" is not writable',
$dirname,
));
}
if ($format === AbstractCommand::FORMAT_YML_ALIAS) {
$format = AbstractCommand::FORMAT_YML;
}
// load the config template
if (is_dir(__DIR__ . '/../../../../data')) {
$contents = file_get_contents(__DIR__ . '/../../../../data/' . self::FILE_NAME . '.' . $format . '.dist');
} else {
throw new RuntimeException(sprintf(
'Could not find template for format "%s".',
$format,
));
}
if (file_put_contents($path, $contents) === false) {
throw new RuntimeException(sprintf(
'The file "%s" could not be written to',
$path,
));
}
}
}