|
1
|
|
|
<?php namespace Mascame\Artificer\Model; |
|
2
|
|
|
|
|
3
|
|
|
use Schema; |
|
4
|
|
|
|
|
5
|
|
|
// Todo: get column type http://stackoverflow.com/questions/18562684/how-to-get-database-field-type-in-laravel |
|
6
|
|
|
class ModelSchema |
|
7
|
|
|
{ |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @var array |
|
11
|
|
|
*/ |
|
12
|
|
|
public $tables; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var array |
|
16
|
|
|
*/ |
|
17
|
|
|
public $models; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
public $class; |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var array |
|
27
|
|
|
*/ |
|
28
|
|
|
public $columns; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param ModelObtainer $modelObtainer |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct(ModelObtainer $modelObtainer) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->models = $modelObtainer->models; |
|
36
|
|
|
$this->tables = $this->getTables($this->models); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param $column |
|
41
|
|
|
* @return bool |
|
42
|
|
|
*/ |
|
43
|
|
|
public function hasColumn($column) |
|
44
|
|
|
{ |
|
45
|
|
|
return (is_array($column) && in_array($column, $this->columns)) ? true : false; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param $table |
|
50
|
|
|
* @return bool |
|
51
|
|
|
*/ |
|
52
|
|
|
public function hasTable($table) |
|
53
|
|
|
{ |
|
54
|
|
|
return Schema::hasTable($table); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @return mixed |
|
59
|
|
|
*/ |
|
60
|
|
|
public function getTable($modelName) |
|
61
|
|
|
{ |
|
62
|
|
|
if (!$modelName) { |
|
63
|
|
|
$modelName = Model::getCurrent(); |
|
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
if (isset($this->models[$modelName]['table'])) { |
|
67
|
|
|
return $this->models[$modelName]['table']; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return $this->getInstance($modelName)->getTable(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param $models |
|
75
|
|
|
* @return array |
|
76
|
|
|
*/ |
|
77
|
|
|
public function getTables($models) |
|
78
|
|
|
{ |
|
79
|
|
|
$tables = array(); |
|
80
|
|
|
|
|
81
|
|
|
foreach ($models as $model) { |
|
82
|
|
|
$table = $this->getTable($model['name']); |
|
83
|
|
|
$this->models[$model['name']]['table'] = $table; |
|
84
|
|
|
$tables[] = $table; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return $tables; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param $table |
|
92
|
|
|
* @return array |
|
93
|
|
|
*/ |
|
94
|
|
|
public function getColumns($table) |
|
95
|
|
|
{ |
|
96
|
|
|
return Schema::getColumnListing($table); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param $modelName |
|
101
|
|
|
* @return mixed |
|
102
|
|
|
*/ |
|
103
|
|
|
public function instantiate($modelName) |
|
104
|
|
|
{ |
|
105
|
|
|
$modelClass = $this->getClass($modelName); |
|
106
|
|
|
|
|
107
|
|
|
if ($this->isFake($modelName)) { |
|
108
|
|
|
$instance = (new FakeModel())->setup($this->models[$modelName]['fake']); |
|
109
|
|
|
} else { |
|
110
|
|
|
$instance = new $modelClass; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return $this->models[$modelName]['instance'] = $instance; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
public function isFake($modelName) |
|
117
|
|
|
{ |
|
118
|
|
|
return (isset($this->models[$modelName]['fake']) && $this->models[$modelName]['fake'] !== false); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @param $modelName |
|
123
|
|
|
* @return bool |
|
124
|
|
|
*/ |
|
125
|
|
|
public function hasInstance($modelName) |
|
126
|
|
|
{ |
|
127
|
|
|
return isset($this->models[$modelName]['instance']); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @param null $modelName |
|
132
|
|
|
* @return mixed |
|
133
|
|
|
*/ |
|
134
|
|
|
public function getInstance($modelName = null) |
|
135
|
|
|
{ |
|
136
|
|
|
($modelName) ?: $modelName = Model::getCurrent(); |
|
|
|
|
|
|
137
|
|
|
|
|
138
|
|
|
if ($this->hasInstance($modelName)) { |
|
139
|
|
|
return $this->models[$modelName]['instance']; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
return $this->instantiate($modelName); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @param $modelName |
|
147
|
|
|
* @return string |
|
148
|
|
|
*/ |
|
149
|
|
|
public function getClass($modelName) |
|
150
|
|
|
{ |
|
151
|
|
|
if (! in_array($modelName, array_keys($this->models))) return null; |
|
152
|
|
|
|
|
153
|
|
|
$model = $this->models[$modelName]; |
|
154
|
|
|
|
|
155
|
|
|
return $model['namespace'] . '\\' . $model['name']; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
} |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.