Skip to content

Commit 164dd2a

Browse files
committed
check file && class exist
1 parent 29f8dac commit 164dd2a

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

src/Command/Generate/Migration.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,36 @@ public function __invoke($type, $classname)
4444
}
4545

4646
$migration_path = $this->config->item('migration_path');
47+
48+
$classname = ucfirst(strtolower($classname));
49+
4750
$file_path = $migration_path . date('YmdHis') . '_' . $classname . '.php';
4851

52+
//check file exist
53+
if (file_exists($file_path)) {
54+
$this->stdio->errln(
55+
"<<red>>The file \"$file_path\" already exists<<reset>>"
56+
);
57+
return Status::FAILURE;
58+
}
59+
60+
//check class exist
61+
foreach (glob($migration_path.'*_*.php') as $file)
62+
{
63+
$name = basename($file, '.php');
64+
65+
//use date('YmdHis') so...
66+
if (preg_match('/^\d{14}_(\w+)$/', $name, $match))
67+
{
68+
if ($match[1] === $classname) {
69+
$this->stdio->errln(
70+
"<<red>>The Class \"$classname\" already exists<<reset>>"
71+
);
72+
return Status::FAILURE;
73+
}
74+
}
75+
}
76+
4977
$template = file_get_contents(__DIR__ . '/templates/Migration.txt');
5078
$search = [
5179
'@@classname@@',

0 commit comments

Comments
 (0)