|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: georg |
|
5
|
|
|
* Date: 5/29/2018 |
|
6
|
|
|
* Time: 11:53 AM |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Ghaskell\Scaffold; |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
use Ghaskell\Scaffold\Facades\Vibro; |
|
13
|
|
|
use Illuminate\Database\Migrations\Migration; |
|
14
|
|
|
use Ghaskell\Scaffold\ColumnPattern as Column; |
|
15
|
|
|
use Illuminate\Support\Str; |
|
16
|
|
|
use Illuminate\Filesystem\Filesystem; |
|
17
|
|
|
use phpDocumentor\Reflection\Types\Array_; |
|
18
|
|
|
|
|
19
|
|
|
class ModelPattern |
|
20
|
|
|
{ |
|
21
|
|
|
public $name; |
|
22
|
|
|
protected $rules; |
|
23
|
|
|
protected $fillable = []; |
|
24
|
|
|
protected $dates = []; |
|
25
|
|
|
protected $touches = []; |
|
26
|
|
|
protected $casts = []; |
|
27
|
|
|
protected $softDeletes; |
|
28
|
|
|
protected $migration; |
|
29
|
|
|
public $columns; |
|
30
|
|
|
protected $files; |
|
31
|
|
|
|
|
32
|
|
|
public function __construct() |
|
33
|
|
|
{ |
|
34
|
|
|
$this->files = new Filesystem(); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public static function make(string $migration) { |
|
38
|
|
|
$model = new ModelPattern(); |
|
39
|
|
|
$model->migration = $migration; |
|
40
|
|
|
|
|
41
|
|
|
$model->parseModelName() |
|
42
|
|
|
->parseColumns() |
|
43
|
|
|
->processColumns(); |
|
44
|
|
|
|
|
45
|
|
|
$model->namespace = "App\\" . config('scaffold.files.model.path'); |
|
|
|
|
|
|
46
|
|
|
$model->fqname = $model->namespace . "\\" . $model->name; |
|
|
|
|
|
|
47
|
|
|
return $model; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
private function parseModelName() |
|
51
|
|
|
{ |
|
52
|
|
|
preg_match("/(?<=create\(\')(.*)(?=\')/", $this->migration, $result); |
|
53
|
|
|
$this->name = Str::singular(Str::studly($result[0])); |
|
54
|
|
|
return $this; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
private function parseColumns() |
|
58
|
|
|
{ |
|
59
|
|
|
preg_match_all('/(?<=\$table\-\>)(.*)(?=\;)/', $this->migration, $lines); |
|
60
|
|
|
foreach ($lines[0] as $line) { |
|
61
|
|
|
preg_match("([^\(]+)", $line, $type); |
|
62
|
|
|
switch ($type[0]) { |
|
63
|
|
|
case 'timestamps': |
|
64
|
|
|
$this->columns[] = Column::make('created_at', 'timestamp'); |
|
65
|
|
|
$this->columns[] = Column::make('updated_at', 'timestamp'); |
|
66
|
|
|
break; |
|
67
|
|
|
case 'softDeletes': |
|
68
|
|
|
$this->softDeletes = true; |
|
69
|
|
|
$this->columns[] = Column::make('deleted_at', 'timestamp'); |
|
70
|
|
|
break; |
|
71
|
|
|
default: |
|
72
|
|
|
preg_match("/['](.*?)[']/", $line, $name); |
|
73
|
|
|
$this->columns[] = Column::make($name[1], $type[0]); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
return $this; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
private function processColumns() { |
|
80
|
|
|
foreach($this->columns as $column) { |
|
81
|
|
|
$this->processColumn($column); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
private function processColumn($column) { |
|
86
|
|
|
if (!empty($column->config['fillable'])) { |
|
87
|
|
|
$this->fillable[] = $column->name; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
if (!empty($column->config['dates'])) { |
|
91
|
|
|
$this->dates[] = $column->name; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
if (!empty($column->config['touches'])) { |
|
95
|
|
|
$this->touches[] = $column->name; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
if (!empty($column->config['casts'])) { |
|
99
|
|
|
$this->casts[$column->name] = $column->config['casts']; |
|
100
|
|
|
} |
|
101
|
|
|
if (!empty($column->config['rules'])) { |
|
102
|
|
|
$this->rules[$column->name] = $column->config['rules']; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function __get($key) { |
|
107
|
|
|
return self::arrayStringify($this->{$key}); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
|
|
111
|
|
|
public static function arrayStringify($array) |
|
112
|
|
|
{ |
|
113
|
|
|
if(count($array) > 0) { |
|
114
|
|
|
$export = str_replace(['array (', ')', '(', ')'], ['[', ']', '(', ')'], var_export($array, true)); |
|
115
|
|
|
$export = preg_replace("/ => \n[^\S\n]*\[/m", ' => [', $export); |
|
116
|
|
|
$export = preg_replace("/ => \[\n[^\S\n]*\]/m", ' => []', $export); |
|
117
|
|
|
$export = preg_replace('/[\r\n]+/', "\n", $export); |
|
118
|
|
|
$export = preg_replace('/[ \t]+/', ' ', $export); |
|
119
|
|
|
$export = preg_replace('/\d\s=>\s/', '', $export); |
|
120
|
|
|
$export = preg_replace('/\r\n|\r|\n/', '', $export); |
|
121
|
|
|
return $export; //don't fear the trailing comma |
|
122
|
|
|
} |
|
123
|
|
|
return null; |
|
124
|
|
|
} |
|
125
|
|
|
} |