-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlaravelCode.php
191 lines (152 loc) · 6.74 KB
/
laravelCode.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
//has the template for generating node files
class laravelCode
{
public $dir_name = 'larvavel/eloquent/';
public $index_file_name = 'index.php';
public $output_dir = './laravelModels'; //this has to be here... its set from the outside!!
public $ORM_file_content = [];
public $ORM_base_file_content = [];
public $ORM_process_file_content = [];
public $ORM_file_name = [];
public $ORM_base_file_name = [];
public $ORM_process_file_name = [];
public $overwrite = false;
public $debug = true;
public $c = ''; //comma character starts life as an empty string...
public $other_tables = []; //where we store our table names..
public $links_js = '';
public $map = [];
public $relations = [];
//var $namespace = 'namespace nod\orm;';
//var $namespace = 'namespace App/Models;';
public $namespace = '';
public function generate($table_data)
{
$object_name = $table_data['object_name'];
$table_name = $table_data['table_name'];
$database = $table_data['database'];
$object_label = $table_data['object_label'];
$has_many = $table_data['has_many'];
$belongs_to = $table_data['belongs_to'];
$file_name = "$object_name.php";
$full_file = "$this->output_dir/$file_name";
$this->ORM_file_name[$object_name] = $full_file;
$base_file_name = "$object_name".'Base.php';
$full_base_file = "$this->output_dir/$base_file_name";
$this->ORM_base_file_name[$object_name] = $full_base_file;
$process_file_name = "$object_name.log";
$full_process_file = "$this->output_dir/$process_file_name";
$this->ORM_process_file_name[$object_name] = $full_process_file;
$this->each_table_start($table_data);
//this is just for logging the process...
$this->ORM_process_file_content[$object_name] = "Object $object_name has_many:\n";
$this->ORM_process_file_content[$object_name] .= var_export($has_many, true);
$this->ORM_process_file_content[$object_name] .= "Object $object_name belongs_to:\n";
$this->ORM_process_file_content[$object_name] .= var_export($belongs_to, true);
/*
foreach($table_data['table_cols'] as $col){
$this->each_col(array(
'object_name' => $object_name,
'table_name' => $table_name,
'database' => $database,
'object_label' => $object_label,
'col_name' => $col,
));
}
*/
$this->each_table_end($table_data);
$this->write_file($object_name);
}
public function each_table_start($table_data)
{
$object_name = $table_data['object_name'];
$table_name = $table_data['table_name'];
$database = $table_data['database'];
$object_label = $table_data['object_label'];
$namespace = $this->namespace;
//start headers...
$this->ORM_file_content[$object_name] = "<?php
$namespace
class $object_name extends $object_name".'Base{
//put custom code here... look in the base class for generated relations..
}
?>';
$this->ORM_base_file_content[$object_name] = "<?php
//Generated by buildORM from the $database:$table_name
$namespace
class $object_name"."Base extends BaseORM{ //which extends Eloquent...
var \$table = '$table_name';
protected \$guarded = array(); //everything can be added via auto-fill... override this in custom code...
";
// $this->index_text .= "\n//importing $object_name from table $database:$this_table\n";
// $this->index_text .= "var $object_name = sequelize.import(__dirname + '/$this_file_name');\n";
// $this->index_text .= "exports.$object_name = $object_name;\n";
}
public function each_table_end($table_data)
{
$object_name = $table_data['object_name'];
$table_name = $table_data['table_name'];
$database = $table_data['database'];
$object_label = $table_data['object_label'];
$has_many = $table_data['has_many'];
$belongs_to = $table_data['belongs_to'];
// echo "\n\n\n has many \n\n";
// var_export($has_many);
// echo "\n\n\n belongs to \n\n";
// var_export($belongs_to);
foreach ($belongs_to as $key => $belongs_to_array) {
$prefix = $belongs_to_array['prefix'];
$type = $belongs_to_array['type'];
if ($prefix == $type) {
$function_name = $type;
} else {
$function_name = $prefix.'_'.$type;
}
$link_field = strtolower($function_name.'_id');
$this->ORM_base_file_content[$object_name] .= "
//autogenerated this function...
public function $function_name()
{
return \$this->belongs_to('$type','$link_field');
}
";
}
foreach ($has_many as $key => $has_many_array) {
$prefix = $has_many_array['prefix'];
$type = $has_many_array['type'];
if ($prefix == $object_name) {
$link_field = $prefix.'_id';
$function_name = $type.'_bunch';
} else {
$link_field = $prefix.'_'.$type.'_id';
$function_name = $type."_from_$prefix".'_bunch';
}
$link_field = strtolower($link_field);
$this->ORM_base_file_content[$object_name] .= "
//autogenerated this function...
public function $function_name()
{
return \$this->hasMany('$type',$link_field);
// return \$this->hasOne('$type',$link_field);
}
";
}
$this->ORM_base_file_content[$object_name] .= "
}//the end of class $object_name
?>";
}
public function write_file($object_name)
{
//always write the auto-generated stuff..
file_put_contents($this->ORM_base_file_name[$object_name], $this->ORM_base_file_content[$object_name]);
//always write the generate log for this file...
file_put_contents($this->ORM_process_file_name[$object_name], $this->ORM_process_file_content[$object_name]);
if (!file_exists($this->ORM_file_name[$object_name]) || $this->overwrite) {
//but only write the child class if it does not already exist...
//unless we are overwriting everything...
//so that user customizations are remembered.
file_put_contents($this->ORM_file_name[$object_name], $this->ORM_file_content[$object_name]);
}
}
}//end class