|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Scaffolder\Compilers\AngularJs; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\File; |
|
6
|
|
|
use Scaffolder\Compilers\AbstractCompiler; |
|
7
|
|
|
use Scaffolder\Compilers\Support\FileToCompile; |
|
8
|
|
|
use Scaffolder\Compilers\Support\PathParser; |
|
9
|
|
|
use Scaffolder\Support\Directory; |
|
10
|
|
|
use Scaffolder\Support\CamelCase; |
|
11
|
|
|
use Scaffolder\Support\Validator; |
|
12
|
|
|
use stdClass ; |
|
13
|
|
|
|
|
14
|
|
|
class ListTemplateCompiler extends AbstractCompiler |
|
15
|
|
|
{ |
|
16
|
|
|
protected $cachePrefix = 'list_template_'; |
|
17
|
|
|
protected $stubFilename = 'ListTemplate.html' ; |
|
18
|
|
|
|
|
19
|
|
|
public function __construct($scaffolderConfig, $modelData = null) |
|
20
|
|
|
{ |
|
21
|
|
|
$this->stubsDirectory = __DIR__ . '/../../../../stubs/AngularJs/'; |
|
22
|
|
|
parent::__construct($scaffolderConfig, $modelData); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Replace and store the Stub. |
|
27
|
|
|
* |
|
28
|
|
|
* @return string |
|
29
|
|
|
*/ |
|
30
|
|
|
public function replaceAndStore() |
|
31
|
|
|
{ |
|
32
|
|
|
|
|
33
|
|
|
return $this->replaceInputFields() |
|
|
|
|
|
|
34
|
|
|
->replaceBelongsToManyFields() |
|
35
|
|
|
->store(new FileToCompile(false, $this->modelData->modelHash)); |
|
36
|
|
|
|
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Replace input fields |
|
41
|
|
|
* |
|
42
|
|
|
* @return $this |
|
43
|
|
|
*/ |
|
44
|
|
|
private function replaceInputFields(){ |
|
45
|
|
|
|
|
46
|
|
|
$inputFields = $this->getInputFields(); |
|
47
|
|
|
|
|
48
|
|
|
$this->stub = str_replace('{{columns_inputs}}', $inputFields, $this->stub); |
|
49
|
|
|
|
|
50
|
|
|
return $this; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* get search conditions |
|
55
|
|
|
* |
|
56
|
|
|
* @return $this |
|
57
|
|
|
*/ |
|
58
|
|
View Code Duplication |
public function getInputFields(){ |
|
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
$inputFields = $eagerFields = ''; |
|
61
|
|
|
|
|
62
|
|
|
foreach ($this->modelData->fields as $field) |
|
63
|
|
|
{ |
|
64
|
|
|
$fieldStub = $this->getInputStubByField($field); |
|
65
|
|
|
|
|
66
|
|
|
if($field->foreignKey){ |
|
67
|
|
|
$fieldStub = $this->replaceForeingStrings($field, $fieldStub) ; |
|
68
|
|
|
$fieldStub = str_replace('{{foreign_model_name}}', strtolower(CamelCase::convertToCamelCase($field->foreignKey->table)), $fieldStub); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$inputFields .= $this->replaceFieldInput($field, $fieldStub) ; |
|
72
|
|
|
|
|
73
|
|
|
// Check foreign key |
|
74
|
|
|
if ($field->foreignKey && isset($field->foreignKey->eager) && $field->foreignKey->eager) |
|
75
|
|
|
{ |
|
76
|
|
|
// search eager fields |
|
77
|
|
|
$foreignModelData = $this->getModelData($field->foreignKey->table); |
|
78
|
|
|
$foreignControllerCompiler = new ListTemplateCompiler($this->scaffolderConfig, $foreignModelData); |
|
79
|
|
|
$foreignControllerCompiler->setEagerTable($this->modelData->tableName); |
|
80
|
|
|
$eagerFields .= $foreignControllerCompiler->getInputFields(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
// replace table name |
|
86
|
|
|
$inputFields = str_replace('{{table_name}}', $this->modelData->tableName, $inputFields); |
|
87
|
|
|
|
|
88
|
|
|
$this->stub = str_replace('{{eager_objects_inputs}}', $eagerFields, $this->stub); |
|
89
|
|
|
|
|
90
|
|
|
return $inputFields ; |
|
91
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* replace field stub with fields and validations |
|
96
|
|
|
* |
|
97
|
|
|
* @param string $field |
|
98
|
|
|
* @param string $fieldStub |
|
99
|
|
|
* |
|
100
|
|
|
* @return $this |
|
101
|
|
|
*/ |
|
102
|
|
|
protected function replaceFieldInput($field, $fieldStub){ |
|
103
|
|
|
$fieldStub = $this->replaceFieldStrings($field, $fieldStub) ; |
|
|
|
|
|
|
104
|
|
|
$fieldStub = $this->replaceFieldValidations($field, $fieldStub) ; |
|
105
|
|
|
|
|
106
|
|
|
return $fieldStub ; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* replace field stub with fields and validations |
|
111
|
|
|
* |
|
112
|
|
|
* @param string $field |
|
113
|
|
|
* @param string $fieldStub |
|
114
|
|
|
* |
|
115
|
|
|
* @return $this |
|
116
|
|
|
*/ |
|
117
|
|
View Code Duplication |
protected function replaceFieldValidations($field, $fieldStub){ |
|
|
|
|
|
|
118
|
|
|
$fieldStub = $this->replaceFieldStrings($field, $fieldStub) ; |
|
|
|
|
|
|
119
|
|
|
|
|
120
|
|
|
$validationsConverted = Validator::convertValidations($field->validations, true); |
|
121
|
|
|
|
|
122
|
|
|
$inputValidations = '' ; |
|
123
|
|
|
|
|
124
|
|
|
foreach ($validationsConverted as $attribute => $value) { |
|
125
|
|
|
if($value) |
|
126
|
|
|
$inputValidations .= ' '.$attribute.'="'. $value.'"' ; |
|
127
|
|
|
else |
|
128
|
|
|
$inputValidations .= ' '.$attribute ; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
$fieldStub = str_replace('{{field_validation}}', $inputValidations, $fieldStub); |
|
132
|
|
|
|
|
133
|
|
|
return $fieldStub ; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Replace belongs to many fields |
|
138
|
|
|
* |
|
139
|
|
|
* @return $this |
|
140
|
|
|
*/ |
|
141
|
|
|
protected function replaceBelongsToManyFields() { |
|
142
|
|
|
|
|
143
|
|
|
$belongToManyFields = ''; |
|
144
|
|
|
|
|
145
|
|
|
foreach ($this->modelData->reverseRelationships as $relationship) |
|
146
|
|
|
{ |
|
147
|
|
|
if ($relationship->type == "belongsToMany") { |
|
148
|
|
|
$fieldStub = File::get($this->stubsDirectory . 'List/'. CamelCase::convertToCamelCase($relationship->ui). '.html'); |
|
149
|
|
|
|
|
150
|
|
|
$fieldStub = str_replace('{{related_table}}',CamelCase::convertToCamelCase($relationship->relatedTable), $fieldStub); |
|
151
|
|
|
$fieldStub = str_replace('{{related_table_lw}}', strtolower(CamelCase::convertToCamelCase($relationship->relatedTable)), $fieldStub); |
|
152
|
|
|
$fieldStub = str_replace('{{table_name}}', $this->modelData->tableName, $fieldStub); |
|
153
|
|
|
$fieldStub = str_replace('{{related_table_lw_pl}}', CamelCase::pluralize(strtolower($relationship->relatedTable)), $fieldStub); |
|
154
|
|
|
|
|
155
|
|
|
$belongToManyFields .= $fieldStub; |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
$this->stub = str_replace('{{belongs_to_many_inputs}}', $belongToManyFields, $this->stub); |
|
160
|
|
|
|
|
161
|
|
|
return $this; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* get input field stub by ui type |
|
167
|
|
|
* |
|
168
|
|
|
* @param string $field |
|
169
|
|
|
* |
|
170
|
|
|
* @return $this |
|
171
|
|
|
*/ |
|
172
|
|
|
private $inputStub = []; |
|
173
|
|
View Code Duplication |
private function getInputStubByField($field){ |
|
|
|
|
|
|
174
|
|
|
|
|
175
|
|
|
if($field->index == 'primary'){ |
|
176
|
|
|
$uiType = 'primary' ; |
|
177
|
|
|
} |
|
178
|
|
|
elseif(isset($field->foreignKey) && $field->foreignKey){ |
|
179
|
|
|
if(isset($field->foreignKey->eager) && $field->foreignKey->eager) |
|
180
|
|
|
$uiType = 'foreign_eager' ; |
|
181
|
|
|
else |
|
182
|
|
|
$uiType = $field->type->ui ; |
|
183
|
|
|
} |
|
184
|
|
|
else { |
|
185
|
|
|
$uiType = $field->type->ui ; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
if(array_key_exists($uiType, $this->inputStub)){ |
|
189
|
|
|
return $this->inputStub[$uiType]; |
|
190
|
|
|
} |
|
191
|
|
|
else { |
|
192
|
|
|
$this->inputStub[$uiType] = File::get($this->stubsDirectory . 'List/'. CamelCase::convertToCamelCase($uiType). '.html'); |
|
193
|
|
|
|
|
194
|
|
|
return $this->inputStub[$uiType]; |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* Get output filename |
|
200
|
|
|
* |
|
201
|
|
|
* |
|
202
|
|
|
* @return $this |
|
203
|
|
|
*/ |
|
204
|
|
|
protected function getOutputFilename() |
|
205
|
|
|
{ |
|
206
|
|
|
$folder = PathParser::parse($this->scaffolderConfig->generator->paths->pages).$this->modelData->tableName.'/list/' ; |
|
207
|
|
|
|
|
208
|
|
|
Directory::createIfNotExists($folder, 0755, true); |
|
209
|
|
|
|
|
210
|
|
|
return $folder .$this->modelData->tableName . '_list.html'; |
|
|
|
|
|
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
} |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.