|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Rey\BitrixMigrations; |
|
4
|
|
|
|
|
5
|
|
|
use Rey\BitrixMigrations\Configuration\ConfigurationInterface; |
|
6
|
|
|
use Rey\BitrixMigrations\Configuration\DoctrineConfiguration; |
|
7
|
|
|
use Doctrine\DBAL\DriverManager as DoctrineDriverManager; |
|
8
|
|
|
use Doctrine\DBAL\Migrations\OutputWriter; |
|
9
|
|
|
use Symfony\Component\Console\Output\ConsoleOutput; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class for storing MigrationManager configuration |
|
13
|
|
|
* |
|
14
|
|
|
* @api |
|
15
|
|
|
*/ |
|
16
|
|
|
class Configuration implements ConfigurationInterface |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var array |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $parameters; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var \Doctrine\DBAL\Connection|null |
|
25
|
|
|
*/ |
|
26
|
|
|
private $doctrineConnection = null; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var \Rey\BitrixMigrations\Configuration\DoctrineConfiguration|null |
|
30
|
|
|
*/ |
|
31
|
|
|
private $doctrineConfiguration = null; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Sets the default values for parameters |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct() |
|
37
|
|
|
{ |
|
38
|
|
|
$this->parameters['db']['dbname'] = ''; |
|
39
|
|
|
$this->parameters['db']['user'] = 'root'; |
|
40
|
|
|
$this->parameters['db']['password'] = ''; |
|
41
|
|
|
$this->parameters['db']['host'] = '127.0.0.1'; |
|
42
|
|
|
$this->parameters['db']['driver'] = 'pdo_mysql'; |
|
43
|
|
|
|
|
44
|
|
|
$this->parameters['migrations']['name'] = 'BXMigrations'; |
|
45
|
|
|
$this->parameters['migrations']['migrations_namespace'] = 'BXMigrations'; |
|
46
|
|
|
$this->parameters['migrations']['table_name'] = 'doctrine_migration_versions'; |
|
47
|
|
|
$this->parameters['migrations']['migrations_directory'] = 'migrations'; |
|
48
|
|
|
$this->parameters['migrations']['abstract_class'] = 'Rey\BitrixMigrations\AbstractMigration'; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* {@inheritdoc} |
|
53
|
|
|
*/ |
|
54
|
|
|
public function setConnectionParameters(array $params) |
|
55
|
|
|
{ |
|
56
|
|
|
$this->parameters['db'] = array_replace($this->parameters['db'], $params); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* {@inheritdoc} |
|
61
|
|
|
*/ |
|
62
|
|
|
public function getConnectionParameters() |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->parameters['db']; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* {@inheritdoc} |
|
69
|
|
|
*/ |
|
70
|
|
|
public function setMigrationsParameters(array $params) |
|
71
|
|
|
{ |
|
72
|
|
|
$this->parameters['migrations'] = array_replace($this->parameters['migrations'], $params); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* {@inheritdoc} |
|
77
|
|
|
*/ |
|
78
|
|
|
public function getMigrationsParameters() |
|
79
|
|
|
{ |
|
80
|
|
|
return $this->parameters['migrations']; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* {@inheritdoc} |
|
85
|
|
|
*/ |
|
86
|
|
|
public function getDoctrineDbConnection() |
|
87
|
|
|
{ |
|
88
|
|
|
if ($this->doctrineConnection === null) { |
|
89
|
|
|
$connection = DoctrineDriverManager::getConnection($this->getConnectionParameters()); |
|
90
|
|
|
|
|
91
|
|
|
if ($connection->getDatabasePlatform() instanceof \Doctrine\DBAL\Platforms\MySqlPlatform) { |
|
92
|
|
|
$abstractPlatform = $connection->getDatabasePlatform(); |
|
93
|
|
|
$abstractPlatform->registerDoctrineTypeMapping('enum', 'string'); |
|
94
|
|
|
$abstractPlatform->registerDoctrineTypeMapping('set', 'string'); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
$this->doctrineConnection = $connection; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return $this->doctrineConnection; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* {@inheritdoc} |
|
105
|
|
|
*/ |
|
106
|
|
|
public function getDoctrineConfiguration() |
|
107
|
|
|
{ |
|
108
|
|
|
if ($this->doctrineConfiguration === null) { |
|
109
|
|
|
$params = $this->getMigrationsParameters(); |
|
110
|
|
|
|
|
111
|
|
|
$output = $this->getOutputWriter(); |
|
112
|
|
|
$config = new DoctrineConfiguration($this->getDoctrineDbConnection(), $output); |
|
113
|
|
|
|
|
114
|
|
|
$config->setName($params['name']); |
|
115
|
|
|
$config->setMigrationsDirectory($params['migrations_directory']); |
|
116
|
|
|
$config->setMigrationsNamespace($params['migrations_namespace']); |
|
117
|
|
|
$config->setMigrationsTableName($params['table_name']); |
|
118
|
|
|
$config->setMigrationsAbstractClass($params['abstract_class']); |
|
119
|
|
|
|
|
120
|
|
|
$this->doctrineConfiguration = $config; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
return $this->doctrineConfiguration; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Get Doctrine OutputWriter |
|
128
|
|
|
* |
|
129
|
|
|
* @return \Doctrine\DBAL\Migrations\OutputWriter |
|
130
|
|
|
*/ |
|
131
|
|
|
private function getOutputWriter() |
|
132
|
|
|
{ |
|
133
|
|
|
$consoleOutput = new ConsoleOutput(); |
|
134
|
|
|
$consoleOutput->setDecorated(true); |
|
135
|
|
|
|
|
136
|
|
|
$outputWriter = new OutputWriter(function($msg) use($consoleOutput) { |
|
137
|
|
|
//intercept errors message when empty sql |
|
138
|
|
|
if (strpos($msg, 'was executed but did not result in any SQL statements.</error>') !== false) { |
|
139
|
|
|
return; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
$consoleOutput->writeln($msg); |
|
143
|
|
|
}); |
|
144
|
|
|
|
|
145
|
|
|
return $outputWriter; |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|