1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Rougin\Refinery\Console; |
4
|
|
|
|
5
|
|
|
use Rougin\Describe\Column; |
6
|
|
|
use Rougin\Describe\Describe; |
7
|
|
|
use Rougin\Refinery\Builder; |
8
|
|
|
use Rougin\Refinery\Manager; |
9
|
|
|
use Symfony\Component\Console\Command\Command; |
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
|
|
|
* Create Command |
17
|
|
|
* |
18
|
|
|
* @package Refinery |
19
|
|
|
* @author Rougin Gutib <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class CreateCommand extends Command |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var \Rougin\Refinery\Builder |
25
|
|
|
*/ |
26
|
|
|
protected $builder; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var \Rougin\Describe\Describe |
30
|
|
|
*/ |
31
|
|
|
protected $describe; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var \Rougin\Refinery\Manager |
35
|
|
|
*/ |
36
|
|
|
protected $manager; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Initializes the command instance. |
40
|
|
|
* |
41
|
|
|
* @param \Rougin\Refinery\Builder $builder |
42
|
|
|
* @param \Rougin\Describe\Describe $describe |
43
|
|
|
* @param \Rougin\Refinery\Manager $manager |
44
|
|
|
*/ |
45
|
24 |
|
public function __construct(Builder $builder, Describe $describe, Manager $manager) |
46
|
|
|
{ |
47
|
24 |
|
$this->builder = $builder; |
48
|
|
|
|
49
|
24 |
|
$this->describe = $describe; |
50
|
|
|
|
51
|
24 |
|
parent::__construct(); |
52
|
|
|
|
53
|
24 |
|
$this->manager = $manager; |
54
|
24 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Configures the current command. |
58
|
|
|
* |
59
|
|
|
* @return void |
60
|
|
|
*/ |
61
|
24 |
|
protected function configure() |
62
|
|
|
{ |
63
|
24 |
|
$this->setName('create')->setDescription('Creates a new database migration file'); |
64
|
24 |
|
$this->addArgument('name', InputArgument::REQUIRED, 'Name of the migration file'); |
65
|
|
|
|
66
|
24 |
|
$this->addOption('auto-increment', null, InputOption::VALUE_OPTIONAL, 'Sets the "auto_increment" value', false); |
67
|
24 |
|
$this->addOption('default', null, InputOption::VALUE_OPTIONAL, 'Sets the "default" value of the column', ''); |
68
|
24 |
|
$this->addOption('from-database', null, InputOption::VALUE_NONE, 'Creates a migration from the database'); |
69
|
24 |
|
$this->addOption('length', null, InputOption::VALUE_OPTIONAL, 'Sets the "constraint" value of the column', 50); |
70
|
24 |
|
$this->addOption('null', null, InputOption::VALUE_OPTIONAL, 'Sets the column with a nullable value', false); |
71
|
24 |
|
$this->addOption('primary', null, InputOption::VALUE_OPTIONAL, 'Sets the column as the primary key', false); |
72
|
24 |
|
$this->addOption('type', null, InputOption::VALUE_OPTIONAL, 'Sets the data type of the column', 'varchar'); |
73
|
24 |
|
$this->addOption('unsigned', null, InputOption::VALUE_OPTIONAL, 'Sets the column with unsigned value', false); |
74
|
24 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Executes the command. |
78
|
|
|
* |
79
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface $input |
80
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $output |
81
|
|
|
* @return void |
82
|
|
|
*/ |
83
|
6 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
84
|
|
|
{ |
85
|
6 |
|
$name = $input->getArgument('name'); |
86
|
|
|
|
87
|
6 |
|
$column = $this->column($input); |
88
|
|
|
|
89
|
6 |
|
$this->builder->column($column); |
90
|
|
|
|
91
|
6 |
|
if ($input->getOption('from-database')) { |
92
|
3 |
|
$this->builder->describe($this->describe); |
93
|
1 |
|
} |
94
|
|
|
|
95
|
6 |
|
$file = $this->builder->make($name); |
|
|
|
|
96
|
|
|
|
97
|
6 |
|
$name = $this->manager->filename($name); |
|
|
|
|
98
|
|
|
|
99
|
6 |
|
$this->manager->create($name, $file); |
100
|
|
|
|
101
|
6 |
|
$message = '"' . $name . '" has been created.'; |
102
|
|
|
|
103
|
6 |
|
$output->writeln('<info>' . $message . '</info>'); |
104
|
6 |
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Initializes a column instance. |
108
|
|
|
* |
109
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface $input |
110
|
|
|
* @return \Rougin\Describe\Column |
111
|
|
|
*/ |
112
|
6 |
|
protected function column(InputInterface $input) |
113
|
|
|
{ |
114
|
6 |
|
$column = new Column; |
115
|
|
|
|
116
|
6 |
|
$column->setDefaultValue($input->getOption('default')); |
|
|
|
|
117
|
|
|
|
118
|
6 |
|
$column->setDataType(strtoupper($input->getOption('type'))); |
|
|
|
|
119
|
|
|
|
120
|
6 |
|
$column->setLength($input->getOption('length')); |
121
|
|
|
|
122
|
6 |
|
$column->setAutoIncrement($input->getOption('auto-increment')); |
123
|
|
|
|
124
|
6 |
|
$column->setNull($input->getOption('null')); |
125
|
|
|
|
126
|
6 |
|
$column->setUnsigned($input->getOption('unsigned')); |
127
|
|
|
|
128
|
6 |
|
$column->setPrimary($input->getOption('primary')); |
129
|
|
|
|
130
|
6 |
|
return $column; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|