1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dami; |
4
|
|
|
|
5
|
|
|
use Dami\Migration\MigrationFiles; |
6
|
|
|
use Dami\Migration\SchemaTable; |
7
|
|
|
use Dami\Migration\Api\MigrationApi; |
8
|
|
|
use Rentgen\Schema\Info; |
9
|
|
|
use Rentgen\Schema\Manipulation; |
10
|
|
|
|
11
|
|
|
class Migration |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @param SchemaTable $schemaTable description |
15
|
|
|
* @param MigrationFiles $migrationFiles description |
16
|
|
|
* @param Manipulation $schemaManipulation description |
17
|
|
|
* @param Info $schemaInfo description |
18
|
|
|
*/ |
19
|
|
|
public function __construct(SchemaTable $schemaTable, MigrationFiles $migrationFiles, Manipulation $schemaManipulation, Info $schemaInfo) |
20
|
|
|
{ |
21
|
|
|
$this->schemaTable = $schemaTable; |
|
|
|
|
22
|
|
|
$this->migrationFiles = $migrationFiles; |
|
|
|
|
23
|
|
|
$this->schemaManipulation = $schemaManipulation; |
|
|
|
|
24
|
|
|
$this->schemaInfo = $schemaInfo; |
|
|
|
|
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Migrate the schema to the given version. |
29
|
|
|
* |
30
|
|
|
* @return integer Number of migrations. |
31
|
|
|
*/ |
32
|
|
|
public function migrate($version = null, $message = null) |
33
|
|
|
{ |
34
|
|
|
return $this->execute($version, $message); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Migrate the schema to previous version. |
39
|
|
|
* |
40
|
|
|
* @return integer Number of migrations. |
41
|
|
|
*/ |
42
|
|
|
public function migrateToPreviousVersion($message) |
43
|
|
|
{ |
44
|
|
|
return $this->execute($this->schemaTable->getPreviousVersion(), $message); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Create an instance of migration class. |
49
|
|
|
* |
50
|
|
|
* @param $className Name of migration class. |
51
|
|
|
* |
52
|
|
|
* @return mixed |
53
|
|
|
*/ |
54
|
|
|
protected function createMigrationApiInstance($className) |
55
|
|
|
{ |
56
|
|
|
return new $className($this->schemaManipulation, $this->schemaInfo); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Execute migrate. |
61
|
|
|
* |
62
|
|
|
* @param string $version The version of migration to rollback or migrate. |
63
|
|
|
* |
64
|
|
|
* @return integer Number of executed migrations. |
65
|
|
|
*/ |
66
|
|
|
private function execute($version = null, $message = null) |
67
|
|
|
{ |
68
|
|
|
$migrateUp = null === $version || $version > $this->schemaTable->getCurrentVersion(); |
69
|
|
|
$files = $this->migrationFiles->get($version); |
70
|
|
|
if (null === $files) { |
71
|
|
|
return 0; |
72
|
|
|
} |
73
|
|
|
$this->schemaManipulation->execute('BEGIN'); |
74
|
|
|
try { |
75
|
|
|
foreach ($files as $file) { |
76
|
|
|
|
77
|
|
|
require_once $file->getPath(); |
78
|
|
|
|
79
|
|
|
$definition = $this->createMigrationApiInstance($file->getClassName()); |
80
|
|
|
|
81
|
|
|
if ($message) { |
82
|
|
|
$message($file->getName(), $file->getVersion()); |
83
|
|
|
} |
84
|
|
|
if ($migrateUp) { |
85
|
|
|
$definition->up(); |
86
|
|
|
} else { |
87
|
|
|
$definition->down(); |
88
|
|
|
} |
89
|
|
|
foreach ($definition->getActions() as $action) { |
90
|
|
|
if (!is_callable($action)) { |
91
|
|
|
throw new \InvalidArgumentException('Migration must be callable'); |
92
|
|
|
} |
93
|
|
|
$action = call_user_func_array($action, array()); |
94
|
|
|
if ($action instanceof MigrationApi) { |
95
|
|
|
$action->execute(); |
|
|
|
|
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
$this->schemaTable->migrateToVersion($file->getVersion()); |
99
|
|
|
} |
100
|
|
|
$this->schemaManipulation->execute('COMMIT'); |
101
|
|
|
} catch (\Exception $e) { |
102
|
|
|
$this->schemaManipulation->execute('ROLLBACK'); |
103
|
|
|
throw $e; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return count($files); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: