1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunstmaan\TranslatorBundle\Service\Command; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Migrations\Configuration\Configuration; |
6
|
|
|
use Doctrine\DBAL\Migrations\Tools\Console\Command\GenerateCommand; |
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
9
|
|
|
|
10
|
1 |
|
if (class_exists(\Doctrine\Migrations\Tools\Console\Command\GenerateCommand::class)) { |
11
|
|
|
/** |
12
|
|
|
* @deprecated This class is deprecated since KunstmaanTranslatorBundle 5.2 and will be removed in 6.0. |
13
|
|
|
*/ |
14
|
|
|
class DiffCommand extends \Doctrine\Migrations\Tools\Console\Command\GenerateCommand |
15
|
|
|
{ |
16
|
|
|
protected function configure(): void |
17
|
|
|
{ |
18
|
|
|
parent::configure(); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function execute(InputInterface $input, OutputInterface $output): ?int |
22
|
|
|
{ |
23
|
|
|
$configuration = $this->getMigrationConfiguration($input, $output); |
24
|
|
|
$sql = $this->getApplication()->getKernel()->getContainer()->get('kunstmaan_translator.service.migrations.migrations')->getDiffSqlArray(); |
25
|
|
|
|
26
|
|
|
$up = $this->buildCodeFromSql($configuration, $sql); |
27
|
|
|
$down = ''; |
28
|
|
|
|
29
|
|
|
if (!$up && !$down) { |
30
|
|
|
$output->writeln('No changes detected in your mapping information.', 'ERROR'); |
31
|
|
|
|
32
|
|
|
return 0; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$version = date('YmdHis'); |
36
|
|
|
$path = $this->generateMigration($configuration, $input, $version, $up, $down); |
37
|
|
|
|
38
|
|
|
$output->writeln(sprintf('Generated new migration class to "<info>%s</info>" from schema differences.', $path)); |
39
|
|
|
|
40
|
|
|
return 0; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
private function buildCodeFromSql(\Doctrine\Migrations\Configuration\Configuration $configuration, array $sql) |
44
|
|
|
{ |
45
|
|
|
$currentPlatform = $configuration->getConnection()->getDatabasePlatform()->getName(); |
46
|
|
|
$code = array( |
47
|
|
|
"\$this->abortIf(\$this->connection->getDatabasePlatform()->getName() != \"$currentPlatform\", \"Migration can only be executed safely on '$currentPlatform'.\");", '', |
48
|
|
|
); |
49
|
|
|
foreach ($sql as $query) { |
50
|
|
|
if (strpos($query, $configuration->getMigrationsTableName()) !== false) { |
51
|
|
|
continue; |
52
|
|
|
} |
53
|
|
|
$code[] = "\$this->addSql(\"$query\");"; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return implode("\n", $code); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
} else { |
60
|
|
|
/** |
61
|
|
|
* @deprecated This class is deprecated since KunstmaanTranslatorBundle 5.2 and will be removed in 6.0. |
62
|
|
|
*/ |
63
|
|
|
class DiffCommand extends GenerateCommand |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
protected function configure() |
66
|
|
|
{ |
67
|
|
|
parent::configure(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function execute(InputInterface $input, OutputInterface $output) |
71
|
|
|
{ |
72
|
|
|
$configuration = $this->getMigrationConfiguration($input, $output); |
73
|
|
|
$sql = $this->getApplication()->getKernel()->getContainer()->get('kunstmaan_translator.service.migrations.migrations')->getDiffSqlArray(); |
74
|
|
|
|
75
|
|
|
$up = $this->buildCodeFromSql($configuration, $sql); |
76
|
|
|
$down = ''; |
77
|
|
|
|
78
|
|
|
if (!$up && !$down) { |
79
|
|
|
$output->writeln('No changes detected in your mapping information.', 'ERROR'); |
80
|
|
|
|
81
|
|
|
return 0; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$version = date('YmdHis'); |
85
|
|
|
$path = $this->generateMigration($configuration, $input, $version, $up, $down); |
86
|
|
|
|
87
|
|
|
$output->writeln(sprintf('Generated new migration class to "<info>%s</info>" from schema differences.', $path)); |
88
|
|
|
|
89
|
|
|
return 0; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
private function buildCodeFromSql(Configuration $configuration, array $sql) |
93
|
|
|
{ |
94
|
|
|
$currentPlatform = $configuration->getConnection()->getDatabasePlatform()->getName(); |
95
|
|
|
$code = array( |
96
|
|
|
"\$this->abortIf(\$this->connection->getDatabasePlatform()->getName() != \"$currentPlatform\", \"Migration can only be executed safely on '$currentPlatform'.\");", '', |
97
|
|
|
); |
98
|
|
|
foreach ($sql as $query) { |
99
|
|
|
if (strpos($query, $configuration->getMigrationsTableName()) !== false) { |
100
|
|
|
continue; |
101
|
|
|
} |
102
|
|
|
$code[] = "\$this->addSql(\"$query\");"; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return implode("\n", $code); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
This check looks for classes that have been defined more than once in the same file.
If you can, we would recommend to use standard object-oriented programming techniques. For example, to avoid multiple types, it might make sense to create a common interface, and then multiple, different implementations for that interface.
This also has the side-effect of providing you with better IDE auto-completion, static analysis and also better OPCode caching from PHP.