Completed
Push — master ( d8bbba...ff2348 )
by Rougin
04:25
created

Builder::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.568
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Rougin\Refinery;
4
5
use Rougin\Describe\Describe;
6
use Rougin\Describe\Column;
7
8
/**
9
 * Builder
10
 *
11
 * @package Refinery
12
 * @author  Rougin Royce Gutib <[email protected]>
13
 */
14
class Builder
15
{
16
    /**
17
     * @var \Rougin\Describe\Column
18
     */
19
    protected $column;
20
21
    /**
22
     * @var \Rougin\Describe\Describe
23
     */
24
    protected $describe;
25
26
    /**
27
     * @var \Rougin\Refinery\Parser
28
     */
29
    protected $parser;
30
31
    /**
32
     * Initializes the column instance.
33
     */
34
    public function __construct()
35
    {
36
        $column = new Column;
37
38
        $column->setDataType('integer');
39
40
        $column->setLength(10);
41
42
        $column->setAutoIncrement(true);
43
44
        $this->column = $column;
45
    }
46
47
    /**
48
     * Sets the column instance to be used.
49
     *
50
     * @param  \Rougin\Describe\Column $column
51
     * @return self
52
     */
53
    public function column(Column $column)
54
    {
55
        $this->column = $column;
56
57
        return $this;
58
    }
59
60
    /**
61
     * Sets the Describe instance.
62
     *
63
     * @param  \Rougin\Describe\Describe $describe
64
     * @return self
65
     */
66
    public function describe(Describe $describe)
67
    {
68
        $this->describe = $describe;
69
70
        return $this;
71
    }
72
73
    /**
74
     * Builds the migration file.
75
     *
76
     * @param  string $name
77
     * @return boolean
78
     */
79
    public function make($name)
80
    {
81
        $this->parser = new Parser((string) $name);
82
83
        $migration = __DIR__ . '/Templates/Migration.txt';
84
85
        $file = file_get_contents($migration);
86
87
        $file = str_replace('$NAME', $name, $file);
88
89
        if ($this->describe instanceof Describe) {
90
            $file = str_replace('$UP', $this->database(), $file);
91
92
            $down = $this->template('DeleteTable');
93
94
            return str_replace('$DOWN', (string) $down, $file);
95
        }
96
97
        return $this->prepare($file, $this->column);
98
    }
99
100
    /**
101
     * Returns a boolean string value.
102
     *
103
     * @param  boolean $value
104
     * @return string
105
     */
106
    protected function boolean($value)
107
    {
108
        return $value ? 'TRUE' : 'FALSE';
109
    }
110
111
    /**
112
     * Generates code based from the column instance.
113
     *
114
     * @param  \Rougin\Describe\Column $column
115
     * @return string
116
     */
117
    protected function create(Column $column, $file)
118
    {
119
        $unsigned = $this->boolean($column->isUnsigned());
120
121
        $increment = $this->boolean($column->isAutoIncrement());
122
123
        $null = (string) $this->boolean($column->isNull());
124
125
        $file = str_replace('$DEFAULT', $column->getDefaultValue(), $file);
126
127
        $file = str_replace('$INCREMENT', $increment, $file);
128
129
        $file = str_replace('$LENGTH', $column->getLength(), $file);
130
131
        $file = str_replace('$NAME', $column->getField(), $file);
132
133
        $file = str_replace('$NULL', $null, (string) $file);
134
135
        $file = str_replace('$TYPE', $column->getDataType(), $file);
136
137
        return str_replace('$UNSIGNED', $unsigned, $file);
138
    }
139
140
    /**
141
     * Creates a migration based from a database schema.
142
     *
143
     * @return string
144
     */
145
    protected function database()
146
    {
147
        $table = (string) $this->parser->table();
148
149
        $up = '$this->dbforge->create_table(\'' . $table . '\');';
150
151
        $columns = $this->describe->columns($table);
152
153
        foreach ($columns as $column) {
154
            $create = $this->template('CreateColumn');
155
156
            $data = $this->create($column, $create);
157
158
            $up = $data . "\r\n\r\n        " . $up;
159
        }
160
161
        return $up;
162
    }
163
164
    /**
165
     * Prepares the contents of the migration file.
166
     *
167
     * @param  string                  $file
168
     * @param  \Rougin\Describe\Column $column
169
     * @return string
170
     */
171
    protected function prepare($file, Column $column)
172
    {
173
        $text = $this->parser->column() ? 'Column' : 'Table';
174
175
        $up = $this->template('Create' . $text);
176
177
        $up = (string) $this->create($column, $up);
178
179
        $down = $this->template((string) 'Delete' . $text);
180
181
        $delete = array('delete', 'remove');
182
183
        $exists = in_array($this->parser->command(), $delete);
184
185
        $exists && list($up, $down) = array($down, $up);
186
187
        $file = (string) str_replace('$UP', $up, $file);
188
189
        return (string) str_replace('$DOWN', $down, $file);
190
    }
191
192
    /**
193
     * Creates a template based on given filename.
194
     *
195
     * @param  string $filename
196
     * @return string
197
     */
198
    protected function template($filename)
199
    {
200
        $file = __DIR__ . '/Templates/' . $filename . '.txt';
201
202
        $file = (string) file_get_contents((string) $file);
203
204
        $file = str_replace('$TABLE', $this->parser->table(), $file);
205
206
        $column = (string) $this->parser->column();
207
208
        $updated = str_replace('$NAME', $column, $file);
209
210
        return $column === '' ? $file : $updated;
211
    }
212
}
213