1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Apps\Console; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use Ffcms\Console\Command; |
7
|
|
|
use Ffcms\Core\Helper\FileSystem\File; |
8
|
|
|
use Ffcms\Core\Helper\Type\Str; |
9
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
11
|
|
|
use Symfony\Component\Console\Input\InputOption; |
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class MigrationCreateCommand. Create new migrations files |
16
|
|
|
* @package Apps\Console |
17
|
|
|
*/ |
18
|
|
|
class MigrationCreateCommand extends Command |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Configure command |
22
|
|
|
*/ |
23
|
|
|
public function configure() |
24
|
|
|
{ |
25
|
|
|
$this->setName('migration:create') |
26
|
|
|
->setDescription('Create new migration for cms database') |
27
|
|
|
->addArgument('name', InputArgument::REQUIRED, 'Set name of new migration. Example: create_demo_table') |
28
|
|
|
->addOption('dir', 'dir', InputOption::VALUE_OPTIONAL, 'Set output directory for new migration file. Example: /vendor/myname/package/migrations'); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Create new migration php file based on carcase template |
33
|
|
|
* @param InputInterface $input |
34
|
|
|
* @param OutputInterface $output |
35
|
|
|
* @return void |
36
|
|
|
*/ |
37
|
|
|
public function execute(InputInterface $input, OutputInterface $output) |
38
|
|
|
{ |
39
|
|
|
// get migration name from params |
40
|
|
|
$name = $input->getArgument('name'); |
41
|
|
|
$name = Str::lowerCase($name); |
|
|
|
|
42
|
|
|
// get output directory |
43
|
|
|
$dir = $this->option('dir'); |
44
|
|
|
if ($dir === null || Str::likeEmpty($dir)) { |
45
|
|
|
$dir = '/Private/Migrations/'; |
46
|
|
|
} else { |
47
|
|
|
$dir = rtrim($dir, '\\/'); |
48
|
|
|
$dir .= '/'; |
49
|
|
|
} |
50
|
|
|
// parse table name |
51
|
|
|
list ($action, $table, $etc) = explode('_', $name); |
52
|
|
|
if ($table === null || Str::likeEmpty($table)) { |
53
|
|
|
$table = 'table'; |
54
|
|
|
} |
55
|
|
|
// create suffix for filename |
56
|
|
|
$suffix = date('Y-m-d-H-i-s'); |
57
|
|
|
// work with migration template: read & parse & save |
58
|
|
|
$tpl = File::read('/Private/Carcase/Migration.tphp'); |
59
|
|
|
$classContent = Str::replace(['%class%', '%table%'], [$name, $table], $tpl); |
|
|
|
|
60
|
|
|
$fullPath = $dir . $name . '-' . $suffix . '.php'; |
61
|
|
|
File::write($fullPath, $classContent); |
62
|
|
|
// show success msg |
63
|
|
|
$output->write('New migration is created: ' . $fullPath); |
64
|
|
|
} |
65
|
|
|
} |