1 | <?php |
||
12 | class SqlFileWriter |
||
13 | { |
||
14 | private $migrationsColumnName; |
||
15 | |||
16 | private $migrationsTableName; |
||
17 | |||
18 | private $destPath; |
||
19 | |||
20 | /** @var null|OutputWriter */ |
||
21 | private $outputWriter; |
||
22 | |||
23 | /** |
||
24 | * @param string $migrationsColumnName |
||
25 | * @param string $migrationsTableName |
||
26 | * @param string $destPath |
||
27 | * @param \Doctrine\DBAL\Migrations\OutputWriter $outputWriter |
||
28 | */ |
||
29 | 18 | public function __construct( |
|
30 | $migrationsColumnName, |
||
31 | $migrationsTableName, |
||
32 | $destPath, |
||
33 | OutputWriter $outputWriter = null |
||
34 | ) { |
||
35 | 18 | if (empty($migrationsColumnName)) { |
|
36 | 1 | $this->throwInvalidArgumentException('Migrations column name cannot be empty.'); |
|
37 | } |
||
38 | |||
39 | 17 | if (empty($migrationsTableName)) { |
|
40 | 2 | $this->throwInvalidArgumentException('Migrations table name cannot be empty.'); |
|
41 | } |
||
42 | |||
43 | 15 | if (empty($destPath)) { |
|
44 | $this->throwInvalidArgumentException('Destination file must be specified.'); |
||
45 | } |
||
46 | |||
47 | 15 | $this->migrationsColumnName = $migrationsColumnName; |
|
48 | 15 | $this->migrationsTableName = $migrationsTableName; |
|
49 | 15 | $this->destPath = $destPath; |
|
50 | 15 | $this->outputWriter = $outputWriter; |
|
51 | 15 | } |
|
52 | |||
53 | /** |
||
54 | * @param array $queriesByVersion array Keys are versions and values are arrays of SQL queries (they must be castable to string) |
||
55 | * @param string $direction |
||
56 | * @return int|bool |
||
57 | */ |
||
58 | 14 | public function write(array $queriesByVersion, $direction) |
|
59 | { |
||
60 | 14 | $path = $this->buildMigrationFilePath(); |
|
61 | 14 | $string = $this->buildMigrationFile($queriesByVersion, $direction); |
|
62 | |||
63 | 14 | if ($this->outputWriter) { |
|
64 | 8 | $this->outputWriter->write("\n" . sprintf('Writing migration file to "<info>%s</info>"', $path)); |
|
65 | } |
||
66 | |||
67 | 14 | return file_put_contents($path, $string); |
|
68 | } |
||
69 | |||
70 | 14 | private function buildMigrationFile(array $queriesByVersion, string $direction) : string |
|
71 | { |
||
72 | 14 | $string = sprintf("-- Doctrine Migration File Generated on %s\n", date('Y-m-d H:i:s')); |
|
73 | |||
74 | 14 | foreach ($queriesByVersion as $version => $queries) { |
|
75 | 14 | $string .= "\n-- Version " . $version . "\n"; |
|
76 | |||
77 | 14 | foreach ($queries as $query) { |
|
78 | 14 | $string .= $query . ";\n"; |
|
79 | } |
||
80 | |||
81 | |||
82 | 14 | $string .= $this->getVersionUpdateQuery($version, $direction); |
|
83 | } |
||
84 | |||
85 | 14 | return $string; |
|
86 | } |
||
87 | |||
88 | 14 | private function getVersionUpdateQuery(string $version, string $direction) : string |
|
98 | |||
99 | 14 | private function buildMigrationFilePath() : string |
|
100 | { |
||
101 | 14 | $path = $this->destPath; |
|
110 | |||
111 | 3 | protected function throwInvalidArgumentException($message) |
|
115 | } |
||
116 |