1 | <?php |
||
14 | class MigrationManager |
||
15 | { |
||
16 | |||
17 | /** |
||
18 | * Migrations directory. |
||
19 | * |
||
20 | * @var string |
||
21 | */ |
||
22 | private $_migrationsDirectory; |
||
23 | |||
24 | /** |
||
25 | * Container. |
||
26 | * |
||
27 | * @var \ArrayAccess |
||
28 | */ |
||
29 | private $_container; |
||
30 | |||
31 | /** |
||
32 | * Migration manager context. |
||
33 | * |
||
34 | * @var MigrationContext |
||
35 | */ |
||
36 | private $_context; |
||
37 | |||
38 | /** |
||
39 | * Migration runners. |
||
40 | * |
||
41 | * @var AbstractMigrationRunner[] |
||
42 | */ |
||
43 | private $_migrationRunners = array(); |
||
44 | |||
45 | /** |
||
46 | * Creates migration manager instance. |
||
47 | * |
||
48 | * @param string $migrations_directory Migrations directory. |
||
49 | * @param \ArrayAccess $container Container. |
||
50 | * |
||
51 | * @throws \InvalidArgumentException When migrations directory does not exist. |
||
52 | */ |
||
53 | 16 | public function __construct($migrations_directory, \ArrayAccess $container) |
|
64 | |||
65 | /** |
||
66 | * Registers a migration runner. |
||
67 | * |
||
68 | * @param AbstractMigrationRunner $migration_runner Migration runner. |
||
69 | * |
||
70 | * @return void |
||
71 | */ |
||
72 | 13 | public function registerMigrationRunner(AbstractMigrationRunner $migration_runner) |
|
76 | |||
77 | /** |
||
78 | * Creates new migration. |
||
79 | * |
||
80 | * @param string $name Migration name. |
||
81 | * @param string $file_extension Migration file extension. |
||
82 | * |
||
83 | * @return string |
||
84 | * @throws \InvalidArgumentException When migration name/file extension is invalid. |
||
85 | * @throws \LogicException When new migration already exists. |
||
86 | */ |
||
87 | 6 | public function createMigration($name, $file_extension) |
|
88 | { |
||
89 | 6 | if ( preg_replace('/[a-z\d\._]/', '', $name) ) { |
|
90 | 3 | throw new \InvalidArgumentException( |
|
91 | 'The migration name can consist only from alpha-numeric characters, as well as dots and underscores.' |
||
92 | 3 | ); |
|
93 | } |
||
94 | |||
95 | 3 | if ( !in_array($file_extension, $this->getMigrationFileExtensions()) ) { |
|
96 | 1 | throw new \InvalidArgumentException( |
|
97 | 1 | 'The migration runner for "' . $file_extension . '" file extension is not registered.' |
|
98 | 1 | ); |
|
99 | } |
||
100 | |||
101 | 2 | $migration_file = $this->_migrationsDirectory . '/' . date('Ymd_Hi') . '_' . $name . '.' . $file_extension; |
|
102 | |||
103 | 2 | if ( file_exists($migration_file) ) { |
|
104 | 1 | throw new \LogicException('The migration file "' . basename($migration_file) . '" already exists.'); |
|
105 | } |
||
106 | |||
107 | 2 | file_put_contents($migration_file, $this->_migrationRunners[$file_extension]->getTemplate()); |
|
108 | |||
109 | 2 | return basename($migration_file); |
|
110 | } |
||
111 | |||
112 | /** |
||
113 | * Returns supported migration file extensions. |
||
114 | * |
||
115 | * @return array |
||
116 | * @throws \LogicException When no migration runners added. |
||
117 | */ |
||
118 | 11 | public function getMigrationFileExtensions() |
|
119 | { |
||
120 | 11 | if ( !$this->_migrationRunners ) { |
|
121 | 1 | throw new \LogicException('No migrations runners registered.'); |
|
122 | } |
||
123 | |||
124 | 10 | return array_keys($this->_migrationRunners); |
|
125 | } |
||
126 | |||
127 | /** |
||
128 | * Executes outstanding migrations. |
||
129 | * |
||
130 | * @param MigrationContext $context Context. |
||
131 | * |
||
132 | * @return void |
||
133 | */ |
||
134 | 6 | public function run(MigrationContext $context) |
|
148 | |||
149 | /** |
||
150 | * Sets current context. |
||
151 | * |
||
152 | * @param MigrationContext $context Context. |
||
153 | * |
||
154 | * @return void |
||
155 | */ |
||
156 | 6 | protected function setContext(MigrationContext $context) |
|
161 | |||
162 | /** |
||
163 | * Creates migration table, when missing. |
||
164 | * |
||
165 | * @return void |
||
166 | */ |
||
167 | 6 | protected function createMigrationsTable() |
|
187 | |||
188 | /** |
||
189 | * Returns all migrations. |
||
190 | * |
||
191 | * @return array |
||
192 | */ |
||
193 | 6 | protected function getAllMigrations() |
|
211 | |||
212 | /** |
||
213 | * Returns executed migrations. |
||
214 | * |
||
215 | * @return array |
||
216 | */ |
||
217 | 6 | protected function getExecutedMigrations() |
|
224 | |||
225 | /** |
||
226 | * Executes migrations. |
||
227 | * |
||
228 | * @param array $migrations Migrations. |
||
229 | * |
||
230 | * @return void |
||
231 | */ |
||
232 | 6 | protected function executeMigrations(array $migrations) |
|
255 | |||
256 | /** |
||
257 | * Deletes migrations. |
||
258 | * |
||
259 | * @param array $migrations Migrations. |
||
260 | * |
||
261 | * @return void |
||
262 | */ |
||
263 | 6 | protected function deleteMigrations(array $migrations) |
|
273 | |||
274 | } |
||
275 |