|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Part of Cli for CodeIgniter |
|
4
|
|
|
* |
|
5
|
|
|
* @author Kenji Suzuki <https://github.com/kenjis> |
|
6
|
|
|
* @license MIT License |
|
7
|
|
|
* @copyright 2015 Kenji Suzuki |
|
8
|
|
|
* @link https://github.com/kenjis/codeigniter-cli |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Kenjis\CodeIgniter_Cli\Command\Generate; |
|
12
|
|
|
|
|
13
|
|
|
use Aura\Cli\Stdio; |
|
14
|
|
|
use Aura\Cli\Context; |
|
15
|
|
|
use Aura\Cli\Status; |
|
16
|
|
|
use Kenjis\CodeIgniter_Cli\Command\Command; |
|
17
|
|
|
use CI_Controller; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @property \CI_Loader $load |
|
21
|
|
|
* @property \CI_Config $config |
|
22
|
|
|
*/ |
|
23
|
|
|
class Migration extends Command |
|
24
|
|
|
{ |
|
25
|
|
|
public function __construct(Context $context, Stdio $stdio, CI_Controller $ci) |
|
26
|
|
|
{ |
|
27
|
|
|
parent::__construct($context, $stdio, $ci); |
|
28
|
|
|
$this->load->config('migration'); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param string $type |
|
33
|
|
|
* @param string $classname |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __invoke($type, $classname) |
|
36
|
|
|
{ |
|
37
|
|
|
if ($classname === null) { |
|
38
|
|
|
$this->stdio->errln( |
|
39
|
|
|
'<<red>>Classname is needed<<reset>>' |
|
40
|
|
|
); |
|
41
|
|
|
$this->stdio->errln( |
|
42
|
|
|
' eg, generate migration CreateUserTable' |
|
43
|
|
|
); |
|
44
|
|
|
return Status::USAGE; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$migration_path = $this->config->item('migration_path'); |
|
48
|
|
|
$migration_type = $this->config->item('migration_type'); |
|
49
|
|
|
|
|
50
|
|
|
$file_path = $this->generateFilename( |
|
51
|
|
|
$migration_path, $migration_type, $classname |
|
52
|
|
|
); |
|
53
|
|
|
|
|
54
|
|
|
// check file exist |
|
55
|
|
|
if (file_exists($file_path)) { |
|
56
|
|
|
$this->stdio->errln( |
|
57
|
|
|
"<<red>>The file \"$file_path\" already exists<<reset>>" |
|
58
|
|
|
); |
|
59
|
|
|
return Status::FAILURE; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
// check class exist |
|
63
|
|
|
foreach (glob($migration_path . '*_*.php') as $file) { |
|
64
|
|
|
$name = basename($file, '.php'); |
|
65
|
|
|
if (preg_match($migration_type === 'timestamp' ? '/^\d{14}_(\w+)$/' : '/^\d{3}_(\w+)$/', $name, $match)) { |
|
66
|
|
|
if (strtolower($match[1]) === strtolower($classname)) { |
|
67
|
|
|
$this->stdio->errln( |
|
68
|
|
|
"<<red>>The Class \"$match[1]\" already exists<<reset>>" |
|
69
|
|
|
); |
|
70
|
|
|
return Status::FAILURE; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$template = file_get_contents(__DIR__ . '/templates/Migration.txt'); |
|
76
|
|
|
$search = [ |
|
77
|
|
|
'@@classname@@', |
|
78
|
|
|
'@@date@@', |
|
79
|
|
|
]; |
|
80
|
|
|
$replace = [ |
|
81
|
|
|
$classname, |
|
82
|
|
|
date('Y/m/d H:i:s'), |
|
83
|
|
|
]; |
|
84
|
|
|
$output = str_replace($search, $replace, $template); |
|
85
|
|
|
$generated = @file_put_contents($file_path, $output, LOCK_EX); |
|
86
|
|
|
|
|
87
|
|
|
if ($generated !== false) { |
|
88
|
|
|
$this->stdio->outln('<<green>>Generated: ' . $file_path . '<<reset>>'); |
|
89
|
|
|
} else { |
|
90
|
|
|
$this->stdio->errln( |
|
91
|
|
|
"<<red>>Can't write to \"$file_path\"<<reset>>" |
|
92
|
|
|
); |
|
93
|
|
|
return Status::FAILURE; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
private function generateFilename($migration_path, $migration_type, $classname) |
|
98
|
|
|
{ |
|
99
|
|
|
if ($migration_type === 'sequential') { |
|
100
|
|
|
$migrations = []; |
|
101
|
|
|
|
|
102
|
|
|
// find max version |
|
103
|
|
|
foreach (glob($migration_path . '*_*.php') as $file) { |
|
104
|
|
|
$name = basename($file, '.php'); |
|
105
|
|
|
|
|
106
|
|
|
if (preg_match('/^\d{3}_(\w+)$/', $name)) { |
|
107
|
|
|
$number = sscanf($name, '%[0-9]+', $number) ? $number : '0'; |
|
108
|
|
|
$migrations[] = $number; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
$version = 0; |
|
113
|
|
|
if ($migrations !== []) { |
|
114
|
|
|
$version = max($migrations); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
return $migration_path . sprintf('%03d', ++$version) . '_' . $classname . '.php'; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
return $migration_path . date('YmdHis') . '_' . $classname . '.php'; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|