1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\Migrations\Tools\Console\Command; |
6
|
|
|
|
7
|
|
|
use Exception; |
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
9
|
|
|
use Symfony\Component\Console\Input\InputOption; |
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
11
|
|
|
use function assert; |
12
|
|
|
use function is_string; |
13
|
|
|
use function key; |
14
|
|
|
use function sprintf; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The GenerateCommand class is responsible for generating a blank migration class for you to modify to your needs. |
18
|
|
|
*/ |
19
|
|
|
class GenerateCommand extends DoctrineCommand |
20
|
|
|
{ |
21
|
|
|
/** @var string */ |
22
|
|
|
protected static $defaultName = 'migrations:generate'; |
23
|
|
|
|
24
|
|
|
protected function configure() : void |
25
|
|
|
{ |
26
|
|
|
$this |
27
|
|
|
->setAliases(['generate']) |
28
|
|
|
->setDescription('Generate a blank migration class.') |
29
|
|
|
->addOption( |
30
|
|
|
'editor-cmd', |
31
|
|
|
null, |
32
|
|
|
InputOption::VALUE_OPTIONAL, |
33
|
|
|
'Open file with this command upon creation.' |
34
|
|
|
) |
35
|
|
|
->addOption( |
36
|
|
|
'namespace', |
37
|
|
|
null, |
38
|
|
|
InputOption::VALUE_REQUIRED, |
39
|
|
|
'The namespace to use for the migration (must be in the list of configured namespaces)' |
40
|
|
|
) |
41
|
|
|
->setHelp(<<<EOT |
42
|
|
|
The <info>%command.name%</info> command generates a blank migration class: |
43
|
|
|
|
44
|
|
|
<info>%command.full_name%</info> |
45
|
|
|
|
46
|
|
|
You can optionally specify a <comment>--editor-cmd</comment> option to open the generated file in your favorite editor: |
47
|
|
|
|
48
|
|
|
<info>%command.full_name% --editor-cmd=mate</info> |
49
|
|
|
EOT |
50
|
|
|
); |
51
|
|
|
|
52
|
|
|
parent::configure(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) : ?int |
56
|
|
|
{ |
57
|
|
|
$configuration = $this->getDependencyFactory()->getConfiguration(); |
58
|
|
|
|
59
|
|
|
$migrationGenerator = $this->getDependencyFactory()->getMigrationGenerator(); |
60
|
|
|
|
61
|
|
|
$namespace = $input->getOption('namespace') ?: null; |
62
|
|
|
|
63
|
|
|
$dirs = $configuration->getMigrationDirectories(); |
64
|
|
|
if ($namespace === null) { |
65
|
|
|
$namespace = key($dirs); |
66
|
|
|
} elseif (! isset($dirs[$namespace])) { |
67
|
|
|
throw new Exception(sprintf('Path not defined for the namespace %s', $namespace)); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
assert(is_string($namespace)); |
71
|
|
|
|
72
|
|
|
$fqcn = $this->getDependencyFactory()->getClassNameGenerator()->generateClassName($namespace); |
73
|
|
|
|
74
|
|
|
$path = $migrationGenerator->generateMigration($fqcn); |
75
|
|
|
|
76
|
|
|
$editorCommand = $input->getOption('editor-cmd'); |
77
|
|
|
|
78
|
|
|
if ($editorCommand !== null) { |
79
|
|
|
assert(is_string($editorCommand)); |
80
|
|
|
$this->procOpen($editorCommand, $path); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$output->writeln([ |
84
|
|
|
sprintf('Generated new migration class to "<info>%s</info>"', $path), |
85
|
|
|
'', |
86
|
|
|
sprintf( |
87
|
|
|
'To run just this migration for testing purposes, you can use <info>migrations:execute --up \'%s\'</info>', |
88
|
|
|
$fqcn |
89
|
|
|
), |
90
|
|
|
'', |
91
|
|
|
sprintf( |
92
|
|
|
'To revert the migration you can use <info>migrations:execute --down \'%s\'</info>', |
93
|
|
|
$fqcn |
94
|
|
|
), |
95
|
|
|
]); |
96
|
|
|
|
97
|
|
|
return 0; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|