1
|
|
|
<?php |
2
|
|
|
namespace Wandu\Database\Migrator; |
3
|
|
|
|
4
|
|
|
use SplFileInfo; |
5
|
|
|
use Wandu\Database\Contracts\Migrator\MigrateAdapterInterface; |
6
|
|
|
use Wandu\DI\ContainerInterface; |
7
|
|
|
|
8
|
|
|
class MigrationContainer |
9
|
|
|
{ |
10
|
|
|
/** @var \SplFileInfo */ |
11
|
|
|
protected $fileInfo; |
12
|
|
|
|
13
|
|
|
/** @var \Wandu\Database\Contracts\Migrator\MigrateAdapterInterface */ |
14
|
|
|
protected $adapter; |
15
|
|
|
|
16
|
|
|
/** @var \Wandu\DI\ContainerInterface */ |
17
|
|
|
protected $container; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param \SplFileInfo $fileInfo |
21
|
|
|
* @param \Wandu\Database\Contracts\Migrator\MigrateAdapterInterface $adapter |
22
|
|
|
* @param \Wandu\DI\ContainerInterface $container |
23
|
|
|
*/ |
24
|
|
|
public function __construct(SplFileInfo $fileInfo, MigrateAdapterInterface $adapter, ContainerInterface $container) |
25
|
|
|
{ |
26
|
|
|
$this->fileInfo = $fileInfo; |
27
|
|
|
$this->adapter = $adapter; |
28
|
|
|
$this->container = $container; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @return string |
33
|
|
|
*/ |
34
|
|
|
public function getName() |
35
|
|
|
{ |
36
|
|
|
return substr($this->fileInfo->getFilename(), 14, -4); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
|
|
public function getId() |
43
|
|
|
{ |
44
|
|
|
return substr($this->fileInfo->getFilename(), 0, 13); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return bool |
49
|
|
|
*/ |
50
|
|
|
public function isApplied() |
51
|
|
|
{ |
52
|
|
|
return !!$this->adapter->version($this->getId()); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function up() |
56
|
|
|
{ |
57
|
|
|
require $this->fileInfo->__toString(); |
58
|
|
|
$migrationName = $this->getName(); |
59
|
|
|
$this->container->create($migrationName)->up(); |
60
|
|
|
$this->adapter->initialize(); |
61
|
|
|
$this->adapter->up($this->getId()); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function down() |
65
|
|
|
{ |
66
|
|
|
require $this->fileInfo->__toString(); |
67
|
|
|
$migrationName = $this->getName(); |
68
|
|
|
$this->container->create($migrationName)->down(); |
69
|
|
|
$this->adapter->initialize(); |
70
|
|
|
$this->adapter->down($this->getId()); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|