1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace T4web\Migrations\Service; |
4
|
|
|
|
5
|
|
|
use T4web\Filesystem\Filesystem; |
6
|
|
|
use T4web\Migrations\Config; |
7
|
|
|
use T4web\Migrations\Exception\RuntimeException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Migration class generator |
11
|
|
|
*/ |
12
|
|
|
class Generator |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $migrationsDir; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $migrationNamespace; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var Filesystem |
26
|
|
|
*/ |
27
|
|
|
protected $filesystem; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param Config $config |
31
|
|
|
* @param Filesystem $filesystem |
32
|
|
|
* @throws \Exception |
33
|
|
|
*/ |
34
|
|
|
public function __construct(Config $config, Filesystem $filesystem) |
35
|
|
|
{ |
36
|
|
|
$this->migrationsDir = $config->getDir(); |
37
|
|
|
$this->migrationNamespace = $config->getNamespace(); |
38
|
|
|
$this->filesystem = $filesystem; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Generate new migration skeleton class |
43
|
|
|
* |
44
|
|
|
* @return string path to new skeleton class file |
45
|
|
|
* @throws \Exception |
46
|
|
|
*/ |
47
|
|
|
public function generate() |
48
|
|
|
{ |
49
|
|
|
$className = 'Version_' . date('YmdHis', time()); |
50
|
|
|
$classPath = $this->migrationsDir . DIRECTORY_SEPARATOR . $className . '.php'; |
51
|
|
|
|
52
|
|
|
if ($this->filesystem->exists($classPath)) { |
53
|
|
|
throw new RuntimeException(sprintf('Migration %s exists!', $className)); |
54
|
|
|
} |
55
|
|
|
$this->filesystem->put($classPath, $this->getTemplate($className)); |
56
|
|
|
|
57
|
|
|
return $classPath; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Get migration skeleton class raw text |
62
|
|
|
* |
63
|
|
|
* @param string $className |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
|
|
protected function getTemplate($className) |
67
|
|
|
{ |
68
|
|
|
return sprintf( |
69
|
|
|
'<?php |
70
|
|
|
|
71
|
|
|
namespace %s; |
72
|
|
|
|
73
|
|
|
use T4web\Migrations\Migration\AbstractMigration; |
74
|
|
|
|
75
|
|
|
class %s extends AbstractMigration |
76
|
|
|
{ |
77
|
|
|
public static $description = "Migration description"; |
78
|
|
|
|
79
|
|
|
public function up() |
80
|
|
|
{ |
81
|
|
|
/** @var \Zend\Db\ResultSet\ResultSet $result */ |
82
|
|
|
//$result = $this->executeQuery(/*Sql instruction*/); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function down() |
86
|
|
|
{ |
87
|
|
|
//throw new \RuntimeException(\'No way to go down!\'); |
88
|
|
|
//$this->executeQuery(/*Sql instruction*/); |
89
|
|
|
} |
90
|
|
|
}', |
91
|
|
|
$this->migrationNamespace, |
92
|
|
|
$className |
93
|
|
|
); |
94
|
|
|
|
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|