|
1
|
|
|
<?php namespace Xethron\MigrationsGenerator\Syntax; |
|
2
|
|
|
|
|
3
|
|
|
use Illuminate\Support\Facades\Config; |
|
4
|
|
|
use Illuminate\Support\Facades\DB; |
|
5
|
|
|
use Way\Generators\Syntax\Table as WayTable; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class Table |
|
9
|
|
|
* @package Xethron\MigrationsGenerator\Syntax |
|
10
|
|
|
*/ |
|
11
|
|
|
abstract class Table extends WayTable |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var string |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $table; |
|
17
|
|
|
|
|
18
|
|
|
public function run(array $fields, string $table, string $connection, $method = 'table'): string |
|
19
|
|
|
{ |
|
20
|
|
|
$table = $this->decorator->tableWithoutPrefix($table); |
|
21
|
|
|
$this->table = $table; |
|
22
|
|
|
if ($connection !== Config::get('database.default')) { |
|
23
|
|
|
$method = 'connection(\''.$connection.'\')->'.$method; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
$compiled = $this->compiler->compile($this->getTemplate($method), ['table' => $table, 'method' => $method]); |
|
27
|
|
|
return $this->replaceFieldsWith($this->getItems($fields), $compiled); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Return string for adding all foreign keys |
|
32
|
|
|
* |
|
33
|
|
|
* @param array $items |
|
34
|
|
|
* @return array |
|
35
|
|
|
*/ |
|
36
|
|
|
protected function getItems(array $items): array |
|
37
|
|
|
{ |
|
38
|
|
|
$result = []; |
|
39
|
|
|
foreach ($items as $item) { |
|
40
|
|
|
$result[] = $this->getItem($item); |
|
41
|
|
|
} |
|
42
|
|
|
return $result; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param array $item |
|
47
|
|
|
* @return string |
|
48
|
|
|
*/ |
|
49
|
|
|
abstract protected function getItem(array $item): string; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param string[] $decorators |
|
53
|
|
|
* @return string |
|
54
|
|
|
*/ |
|
55
|
|
|
protected function addDecorators(array $decorators): string |
|
56
|
|
|
{ |
|
57
|
|
|
$output = ''; |
|
58
|
|
|
foreach ($decorators as $decorator) { |
|
59
|
|
|
$output .= sprintf("->%s", $decorator); |
|
60
|
|
|
// Do we need to tack on the parentheses? |
|
61
|
|
|
if (strpos($decorator, '(') === false) { |
|
62
|
|
|
$output .= '()'; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
return $output; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|