-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathCreateCommand.php
More file actions
61 lines (50 loc) · 2.02 KB
/
CreateCommand.php
File metadata and controls
61 lines (50 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
/**
* User: aguidet
* Date: 27/02/15
* Time: 17:17
*/
namespace Migrate\Command;
use Cocur\Slugify\Slugify;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;
class CreateCommand extends AbstractEnvCommand
{
protected function configure()
{
$this
->setName('migrate:create')
->setDescription('Create a SQL migration');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->checkEnv();
/* @var $questions QuestionHelper */
$questions = $this->getHelperSet()->get('question');
$descriptionQuestion = new Question("Please enter a description: ");
$description = $questions->ask($input, $output, $descriptionQuestion);
$editor=getenv("EDITOR");
if (empty($editor)) {
$editor="vi";
}
$editorQuestion = new Question("Please chose which editor to use <info>(default $editor)</info>: ", "$editor");
$editor=$questions->ask($input, $output, $editorQuestion);
$slugger = new Slugify();
$filename = $slugger->slugify($description);
$timestamp = str_pad(str_replace(".", "", microtime(true)), 14, "0");
$filename = $timestamp . '_' . $filename . '.sql';
if (file_exists($this->getMigrationDir() . '/../' .'migration.tpl')) {
$templateFile = file_get_contents($this->getMigrationDir() . '/../' .'migration.tpl');
} else {
$templateFile = file_get_contents(__DIR__ . '/../../templates/migration.tpl');
}
$templateFile = str_replace('{DESCRIPTION}', $description, $templateFile);
$migrationFullPath = $this->getMigrationDir() . '/' . $filename;
file_put_contents($migrationFullPath, $templateFile);
$output->writeln("<info>$migrationFullPath created</info>");
if (!defined('PHPUNIT')) {
system("$editor $migrationFullPath > `tty`");
}
}
}