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