rougin /
refinery
| 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 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|null |
||
| 23 | */ |
||
| 24 | protected $describe = null; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var \Rougin\Refinery\Parser |
||
| 28 | */ |
||
| 29 | protected $parser; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Initializes the column instance. |
||
| 33 | */ |
||
| 34 | 39 | public function __construct() |
|
| 35 | { |
||
| 36 | 39 | $column = new Column; |
|
| 37 | |||
| 38 | 39 | $column->setDataType('integer'); |
|
| 39 | |||
| 40 | 39 | $column->setLength(10); |
|
| 41 | |||
| 42 | 39 | $column->setAutoIncrement(true); |
|
| 43 | |||
| 44 | 39 | $this->column = $column; |
|
| 45 | 39 | } |
|
| 46 | |||
| 47 | /** |
||
| 48 | * Sets the column instance to be used. |
||
| 49 | * |
||
| 50 | * @param \Rougin\Describe\Column $column |
||
| 51 | * @return self |
||
| 52 | */ |
||
| 53 | 12 | public function column(Column $column) |
|
| 54 | { |
||
| 55 | 12 | $this->column = $column; |
|
| 56 | |||
| 57 | 12 | return $this; |
|
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Sets the Describe instance. |
||
| 62 | * |
||
| 63 | * @param \Rougin\Describe\Describe $describe |
||
| 64 | * @return self |
||
| 65 | */ |
||
| 66 | 6 | public function describe(Describe $describe) |
|
| 67 | { |
||
| 68 | 6 | $this->describe = $describe; |
|
| 69 | |||
| 70 | 6 | return $this; |
|
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Builds the migration file. |
||
| 75 | * |
||
| 76 | * @param string $name |
||
| 77 | * @return boolean |
||
| 78 | */ |
||
| 79 | 33 | public function make($name) |
|
| 80 | { |
||
| 81 | 33 | $this->parser = new Parser((string) $name); |
|
| 82 | |||
| 83 | 33 | $migration = __DIR__ . '/Templates/Migration.txt'; |
|
| 84 | |||
| 85 | 33 | $file = file_get_contents($migration); |
|
| 86 | |||
| 87 | 33 | $file = str_replace('$NAME', $name, $file); |
|
| 88 | |||
| 89 | 33 | if ($this->describe instanceof Describe) { |
|
| 90 | 6 | $file = str_replace('$UP', $this->database(), $file); |
|
| 91 | |||
| 92 | 6 | $down = $this->template('DeleteTable'); |
|
| 93 | |||
| 94 | 6 | return str_replace('$DOWN', (string) $down, $file); |
|
| 95 | } |
||
| 96 | |||
| 97 | 27 | return $this->prepare($file, $this->column); |
|
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Returns a string value of the specified data type. |
||
| 102 | * |
||
| 103 | * @param boolean|string $value |
||
| 104 | * @return string |
||
| 105 | */ |
||
| 106 | 33 | protected function type($value) |
|
| 107 | { |
||
| 108 | 33 | if (is_string($value) === true) { |
|
| 109 | 33 | $value === 'string' && $value = 'varchar'; |
|
| 110 | |||
| 111 | 33 | return mb_strtoupper((string) $value); |
|
| 112 | } |
||
| 113 | |||
| 114 | 33 | return $value ? 'TRUE' : 'FALSE'; |
|
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Generates code based from the column instance. |
||
| 119 | * |
||
| 120 | * @param \Rougin\Describe\Column $column |
||
| 121 | * @return string |
||
| 122 | */ |
||
| 123 | 33 | protected function create(Column $column, $file) |
|
| 124 | { |
||
| 125 | 33 | $increment = $this->type($column->isAutoIncrement()); |
|
| 126 | |||
| 127 | 33 | $unsigned = $this->type($column->isUnsigned()); |
|
| 128 | |||
| 129 | 33 | $file = str_replace('$DEFAULT', $column->getDefaultValue(), $file); |
|
| 130 | |||
| 131 | 33 | $file = str_replace('$INCREMENT', $increment, $file); |
|
| 132 | |||
| 133 | 33 | $file = str_replace('$LENGTH', $column->getLength(), $file); |
|
| 134 | |||
| 135 | 33 | $file = str_replace('$NULL', $this->type($column->isNull()), $file); |
|
| 136 | |||
| 137 | 33 | $file = str_replace('$NAME', $column->getField(), $file); |
|
| 138 | |||
| 139 | 33 | $file = str_replace('$TYPE', $this->type($column->getDataType()), $file); |
|
| 140 | |||
| 141 | 33 | return str_replace('$UNSIGNED', $unsigned, (string) $file); |
|
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Creates a migration based from a database schema. |
||
| 146 | * |
||
| 147 | * @return string |
||
| 148 | */ |
||
| 149 | 6 | protected function database() |
|
| 150 | { |
||
| 151 | 6 | $table = (string) $this->parser->table(); |
|
| 152 | |||
| 153 | 6 | $up = '$this->dbforge->create_table(\'' . $table . '\');'; |
|
| 154 | |||
| 155 | 6 | $columns = $this->describe->columns($table); |
|
|
0 ignored issues
–
show
|
|||
| 156 | |||
| 157 | 6 | foreach ($columns as $column) { |
|
| 158 | 6 | $create = $this->template('CreateColumn'); |
|
| 159 | |||
| 160 | 6 | $data = $this->create($column, $create); |
|
| 161 | |||
| 162 | 6 | $up = $data . "\r\n\r\n " . $up; |
|
| 163 | 2 | } |
|
| 164 | |||
| 165 | 6 | return $up; |
|
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Prepares the contents of the migration file. |
||
| 170 | * |
||
| 171 | * @param string $file |
||
| 172 | * @param \Rougin\Describe\Column $column |
||
| 173 | * @return string |
||
| 174 | */ |
||
| 175 | 27 | protected function prepare($file, Column $column) |
|
| 176 | { |
||
| 177 | 27 | $text = $this->parser->column() ? 'Column' : 'Table'; |
|
| 178 | |||
| 179 | 27 | $up = $this->template('Create' . $text); |
|
| 180 | |||
| 181 | 27 | $up = (string) $this->create($column, $up); |
|
| 182 | |||
| 183 | 27 | $down = $this->template((string) 'Delete' . $text); |
|
| 184 | |||
| 185 | 27 | $delete = array('delete', 'remove'); |
|
| 186 | |||
| 187 | 27 | $exists = in_array($this->parser->command(), $delete); |
|
| 188 | |||
| 189 | 27 | $exists && list($up, $down) = array($down, $up); |
|
| 190 | |||
| 191 | 27 | $file = (string) str_replace('$UP', $up, $file); |
|
| 192 | |||
| 193 | 27 | return (string) str_replace('$DOWN', $down, $file); |
|
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Creates a template based on given filename. |
||
| 198 | * |
||
| 199 | * @param string $filename |
||
| 200 | * @return string |
||
| 201 | */ |
||
| 202 | 33 | protected function template($filename) |
|
| 203 | { |
||
| 204 | 33 | $file = __DIR__ . '/Templates/' . $filename . '.txt'; |
|
| 205 | |||
| 206 | 33 | $file = (string) file_get_contents((string) $file); |
|
| 207 | |||
| 208 | 33 | $file = str_replace('$TABLE', $this->parser->table(), $file); |
|
| 209 | |||
| 210 | 33 | $column = (string) $this->parser->column(); |
|
| 211 | |||
| 212 | 33 | $updated = str_replace('$NAME', $column, $file); |
|
| 213 | |||
| 214 | 33 | return $column === '' ? $file : $updated; |
|
| 215 | } |
||
| 216 | } |
||
| 217 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.