|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace yentu\commands; |
|
4
|
|
|
|
|
5
|
|
|
use yentu\Migrations; |
|
6
|
|
|
use yentu\exceptions\CommandException; |
|
7
|
|
|
use clearice\io\Io; |
|
8
|
|
|
|
|
9
|
|
|
class Create extends Command |
|
10
|
|
|
{ |
|
11
|
|
|
private $migrations; |
|
12
|
|
|
private $io; |
|
13
|
|
|
|
|
14
|
|
|
public function __construct(Migrations $migrations, Io $io) |
|
15
|
|
|
{ |
|
16
|
|
|
$this->migrations = $migrations; |
|
17
|
|
|
$this->io = $io; |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @throws CommandException |
|
22
|
|
|
*/ |
|
23
|
|
|
public function run() |
|
24
|
|
|
{ |
|
25
|
|
|
if (isset($this->options['__args'])) { |
|
26
|
|
|
$this->createFile($this->options['__args'][0]); |
|
27
|
|
|
} else { |
|
28
|
|
|
$this->checkName(null); |
|
29
|
|
|
} |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param $name |
|
34
|
|
|
* @throws CommandException |
|
35
|
|
|
*/ |
|
36
|
|
|
private function checkExisting($name) |
|
37
|
|
|
{ |
|
38
|
|
|
if (count(glob($this->migrations->getPath("migrations/*_{$name}.php"))) > 0) { |
|
39
|
|
|
throw new CommandException("A migration already exists with the name {$name}"); |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @throws CommandException |
|
45
|
|
|
*/ |
|
46
|
|
|
private function checkPermission() |
|
47
|
|
|
{ |
|
48
|
|
|
if (!file_exists($this->migrations->getPath("migrations/"))) { |
|
49
|
|
|
throw new CommandException("The migrations directory `" . $this->migrations->getPath("migrations/") . "` does not exist."); |
|
50
|
|
|
} |
|
51
|
|
|
if (!is_dir($this->migrations->getPath("migrations/"))) { |
|
52
|
|
|
throw new CommandException($this->migrations->getPath("migrations/") . ' is not a directory'); |
|
53
|
|
|
} |
|
54
|
|
|
if (!is_writable($this->migrations->getPath("migrations/"))) { |
|
55
|
|
|
throw new CommandException("You do not have the permission to write to " . $this->migrations->getPath("migrations/")); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param $name |
|
61
|
|
|
* @throws CommandException |
|
62
|
|
|
*/ |
|
63
|
|
|
private function checkName($name) |
|
64
|
|
|
{ |
|
65
|
|
|
if ($name == '') { |
|
66
|
|
|
throw new CommandException( |
|
67
|
|
|
"Please provide a name for your new migration" |
|
68
|
|
|
); |
|
69
|
|
|
} else if (!preg_match("/[a-z][a-z0-9\_]*/", $name)) { |
|
70
|
|
|
throw new CommandException( |
|
71
|
|
|
"Migration names must always start with a lowercase alphabet and " |
|
72
|
|
|
. "can only consist of lower case alphabets, numbers and underscores." |
|
73
|
|
|
); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param $name |
|
79
|
|
|
* @throws CommandException |
|
80
|
|
|
*/ |
|
81
|
|
|
public function createFile($name) |
|
82
|
|
|
{ |
|
83
|
|
|
$this->checkExisting($name); |
|
84
|
|
|
$this->checkName($name); |
|
85
|
|
|
$this->checkPermission(); |
|
86
|
|
|
|
|
87
|
|
|
$timestamp = gmdate('YmdHis', time()); |
|
88
|
|
|
$code = new \yentu\CodeWriter(); |
|
89
|
|
|
$code->add(''); |
|
90
|
|
|
$code->add('\yentu\Yentu::begin()'); |
|
91
|
|
|
$code->add(''); |
|
92
|
|
|
$code->add('->end();'); |
|
93
|
|
|
$path = $this->migrations->getPath("migrations/{$timestamp}_{$name}.php"); |
|
94
|
|
|
file_put_contents($path, $code); |
|
95
|
|
|
$this->io->output("Added $path for new migration.\n"); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
} |
|
99
|
|
|
|