1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dami\Migration; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Finder\Finder; |
6
|
|
|
|
7
|
|
|
class MigrationFiles |
8
|
|
|
{ |
9
|
|
|
private $path; |
10
|
|
|
private $schemaTable; |
11
|
|
|
private $statusIntention = false; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @param string $path Migrations path. |
15
|
|
|
* @param SchemaTable $schemaTable SchemaTable instance. |
16
|
|
|
*/ |
17
|
|
|
public function __construct($path, SchemaTable $schemaTable) |
18
|
|
|
{ |
19
|
|
|
$this->path = $path; |
20
|
|
|
$this->schemaTable = $schemaTable; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Gets migration files. |
25
|
|
|
* |
26
|
|
|
* @param string $version Version of migration. |
27
|
|
|
* |
28
|
|
|
* @return MigrationFile[] |
29
|
|
|
*/ |
30
|
|
|
public function get($version = null) |
31
|
|
|
{ |
32
|
|
|
$currentVersion = $this->schemaTable->getCurrentVersion(); |
33
|
|
|
if (null !== $version && (int) $version === (int) $currentVersion) { |
34
|
|
|
return null; |
35
|
|
|
} |
36
|
|
|
$migrateUp = null === $version || $version >= $currentVersion; |
37
|
|
|
$migrationFiles = array(); |
38
|
|
|
foreach ($this->getFiles($migrateUp) as $file) { |
39
|
|
|
$filenameParser = new FileNameParser($file->getFileName()); |
40
|
|
|
|
41
|
|
|
$isMigrated = in_array($filenameParser->getVersion(), $this->schemaTable->getVersions()); |
42
|
|
|
|
43
|
|
|
$migrationFile = new MigrationFile($filenameParser->getMigrationName(), $file->getRealpath(), |
44
|
|
|
$filenameParser->getVersion(), $filenameParser->getMigrationClassName(), $isMigrated); |
45
|
|
|
|
46
|
|
|
if (false === $this->statusIntention) { |
47
|
|
|
if ($migrateUp && $isMigrated |
48
|
|
|
|| !$migrateUp && !$isMigrated) { |
49
|
|
|
continue; |
50
|
|
|
} |
51
|
|
|
if ($version == $migrationFile->getVersion()) { |
52
|
|
|
if ($migrateUp) { |
53
|
|
|
$migrationFiles[] = $migrationFile; |
54
|
|
|
} |
55
|
|
|
break; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
$migrationFiles[] = $migrationFile; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $migrationFiles; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* This method is called when status of migrations is checking. |
66
|
|
|
* |
67
|
|
|
* @return MigrationFiles |
68
|
|
|
*/ |
69
|
|
|
public function statusIntention() |
70
|
|
|
{ |
71
|
|
|
$this->statusIntention = true; |
72
|
|
|
|
73
|
|
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Gets files from directory. |
78
|
|
|
* |
79
|
|
|
* @param bool $migrateUp Is migration up. |
80
|
|
|
* |
81
|
|
|
* @return bool |
82
|
|
|
*/ |
83
|
|
|
private function getFiles($migrateUp) |
84
|
|
|
{ |
85
|
|
|
$finder = new Finder(); |
86
|
|
|
|
87
|
|
|
return $finder |
88
|
|
|
->files() |
89
|
|
|
->in($this->path) |
90
|
|
|
->sort(function (\SplFileInfo $a, \SplFileInfo $b) use ($migrateUp) { |
91
|
|
|
return $migrateUp |
92
|
|
|
? $a->getRealpath() > $b->getRealpath() |
93
|
|
|
: $a->getRealpath() < $b->getRealpath(); |
94
|
|
|
} |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|