33
44use Illuminate \Support \Str ;
55use Laraveldaily \Quickadmin \Cache \QuickCache ;
6+ use Laraveldaily \Quickadmin \Models \Crud ;
67
78class ControllerBuilder
89{
910 // Controller namespace
10- private $ namespace = 'App\Http\Controllers ' ;
11+ private $ namespace = 'App\Http\Controllers\Admin ' ;
1112 // Template
1213 private $ template ;
1314 // Global names
1415 private $ name ;
1516 private $ className ;
1617 private $ modelName ;
17- private $ requestName ;
18+ private $ createRequestName ;
19+ private $ updateRequestName ;
1820 private $ fileName ;
21+ private $ fields ;
22+ private $ relationships ;
23+ private $ files ;
1924
2025 /**
2126 * Build our controller file
2227 */
2328 public function build ()
2429 {
25- $ cache = new QuickCache ();
26- $ cached = $ cache ->get ('fieldsinfo ' );
27- $ this ->template = __DIR__ . '/../Templates/controller ' ;
28- $ this ->name = $ cached ['name ' ];
29- $ this ->fields = $ cached ['fields ' ];
30- $ this ->soft = $ cached ['soft_delete ' ];
30+ $ cache = new QuickCache ();
31+ $ cached = $ cache ->get ('fieldsinfo ' );
32+ $ this ->template = __DIR__ . '/../Templates/controller ' ;
33+ $ this ->name = $ cached ['name ' ];
34+ $ this ->fields = $ cached ['fields ' ];
35+ $ this ->relationships = $ cached ['relationships ' ];
36+ $ this ->files = $ cached ['files ' ];
3137 $ this ->names ();
3238 $ template = (string ) $ this ->loadTemplate ();
3339 $ template = $ this ->buildParts ($ template );
@@ -54,31 +60,175 @@ private function buildParts($template)
5460 $ template = str_replace ([
5561 '$NAMESPACE$ ' ,
5662 '$MODEL$ ' ,
57- '$REQUESTNAME$ ' ,
63+ '$CREATEREQUESTNAME$ ' ,
64+ '$UPDATEREQUESTNAME$ ' ,
5865 '$CLASS$ ' ,
5966 '$COLLECTION$ ' ,
6067 '$RESOURCE$ ' ,
68+ '$INDEXGET$ ' ,
69+ '$RELATIONSHIPS$ ' ,
70+ '$RELATIONSHIP_COMPACT$ ' ,
71+ '$RELATIONSHIP_COMPACT_EDIT$ ' ,
72+ '$RELATIONSHIP_NAMESPACES$ ' ,
73+ '$FILETRAIT$ ' ,
74+ '$FILESAVING$ '
6175 ], [
6276 $ this ->namespace ,
6377 $ this ->modelName ,
64- $ this ->requestName ,
78+ $ this ->createRequestName ,
79+ $ this ->updateRequestName ,
6580 $ this ->className ,
6681 strtolower ($ this ->modelName ),
6782 strtolower ($ this ->modelName ),
83+ $ this ->indexBuilder (),
84+ $ this ->relationshipsBuilder (),
85+ $ this ->compactBuilder (),
86+ $ this ->compactEditBuilder (),
87+ $ this ->relationshipsNamespaces (),
88+ $ this ->files > 0 ? 'use App\Http\Controllers\Traits\FileUploadTrait; ' : '' ,
89+ $ this ->files > 0 ? '$this->saveFiles($request); ' : ''
6890 ], $ template );
6991
7092 return $ template ;
7193 }
7294
95+ /**
96+ * Build our index template
97+ * @return mixed|string
98+ */
99+ public function indexBuilder ()
100+ {
101+ if ($ this ->relationships == 0 ) {
102+ return '$ ' . strtolower ($ this ->modelName ) . ' = ' . $ this ->modelName . '::all(); ' ;
103+ } else {
104+ $ relationship = '$ ' . strtolower ($ this ->modelName ) . ' = ' . $ this ->modelName . '::$WITH$get(); ' ;
105+ foreach ($ this ->fields as $ field ) {
106+ if ($ field ->type == 'relationship ' ) {
107+ $ relationship = str_replace ([
108+ '$WITH$ '
109+ ], [
110+ 'with(" ' . $ field ->relationship_name . '")->$WITH$ '
111+ ], $ relationship );
112+ }
113+ }
114+ $ relationship = str_replace ('$WITH$ ' , '' , $ relationship );
115+
116+ return $ relationship ;
117+ }
118+ }
119+
120+ public function relationshipsNamespaces ()
121+ {
122+ if ($ this ->relationships == 0 ) {
123+ return '' ;
124+ } else {
125+ $ cruds = Crud::all ()->keyBy ('id ' );
126+ $ relationships = '' ;
127+ $ first = true ;
128+ foreach ($ this ->fields as $ field ) {
129+ if ($ field ->type == 'relationship ' ) {
130+ $ crud = $ cruds [$ field ->relationship_id ];
131+ $ relationships .= 'use App \\' . ucfirst (Str::camel ($ crud ->name )) . "; \r\n" ;
132+ }
133+ }
134+
135+ return $ relationships ;
136+ }
137+ }
138+
139+ /**
140+ * Build relationships for forms
141+ * @return string
142+ */
143+ public function relationshipsBuilder ()
144+ {
145+ if ($ this ->relationships == 0 ) {
146+ return '' ;
147+ } else {
148+ $ cruds = Crud::all ()->keyBy ('id ' );
149+ $ relationships = '' ;
150+ $ first = true ;
151+ foreach ($ this ->fields as $ field ) {
152+ if ($ field ->type == 'relationship ' ) {
153+ // Formatting fix if the relationship is not first
154+ if (!$ first ) {
155+ $ relationships .= ' ' ;
156+ }
157+ $ crud = $ cruds [$ field ->relationship_id ];
158+ $ relationships .= '$ '
159+ . $ field ->relationship_name
160+ . ' = '
161+ . ucfirst (Str::camel ($ crud ->name ))
162+ . '::lists(" '
163+ . $ field ->relationship_field
164+ . '", "id"); '
165+ . "\r\n" ;
166+ }
167+ }
168+
169+ return $ relationships ;
170+ }
171+ }
172+
173+ /**
174+ * Build compact for create form
175+ * @return mixed|string
176+ */
177+ public function compactBuilder ()
178+ {
179+ if ($ this ->relationships == 0 ) {
180+ return '' ;
181+ } else {
182+ $ compact = ', compact($RELATIONS$) ' ;
183+ $ first = true ;
184+ foreach ($ this ->fields as $ field ) {
185+ if ($ field ->type == 'relationship ' ) {
186+ $ toReplace = '' ;
187+ if ($ first != true ) {
188+ $ toReplace .= ', ' ;
189+ } else {
190+ $ first = false ;
191+ }
192+ $ toReplace .= '" ' . $ field ->relationship_name . '"$RELATIONS$ ' ;
193+ $ compact = str_replace ('$RELATIONS$ ' , $ toReplace , $ compact );
194+ }
195+ }
196+ $ compact = str_replace ('$RELATIONS$ ' , '' , $ compact );
197+
198+ return $ compact ;
199+ }
200+ }
201+
202+ /**
203+ * Build compact for edit form
204+ * @return string
205+ */
206+ public function compactEditBuilder ()
207+ {
208+ if ($ this ->relationships == 0 ) {
209+ return '' ;
210+ } else {
211+ $ compact = '' ;
212+ foreach ($ this ->fields as $ field ) {
213+ if ($ field ->type == 'relationship ' ) {
214+ $ compact .= ', " ' . $ field ->relationship_name . '" ' ;
215+ }
216+ }
217+
218+ return $ compact ;
219+ }
220+ }
221+
73222 /**
74223 * Generate names
75224 */
76225 private function names ()
77226 {
78- $ camelName = ucfirst (Str::camel ($ this ->name ));
79- $ this ->className = $ camelName . 'Controller ' ;
80- $ this ->modelName = $ camelName ;
81- $ this ->requestName = $ camelName . 'Request ' ;
227+ $ camelName = ucfirst (Str::camel ($ this ->name ));
228+ $ this ->className = $ camelName . 'Controller ' ;
229+ $ this ->modelName = $ camelName ;
230+ $ this ->createRequestName = 'Create ' . $ camelName . 'Request ' ;
231+ $ this ->updateRequestName = 'Update ' . $ camelName . 'Request ' ;
82232
83233 $ fileName = $ this ->className . '.php ' ;
84234 $ this ->fileName = $ fileName ;
@@ -89,7 +239,10 @@ private function names()
89239 */
90240 private function publish ($ template )
91241 {
92- file_put_contents (app_path ('Http/Controllers/ ' . $ this ->fileName ), $ template );
242+ if (!file_exists (app_path ('Http\Controllers\Admin ' ))) {
243+ mkdir (app_path ('Http\Controllers\Admin ' ));
244+ }
245+ file_put_contents (app_path ('Http\Controllers\Admin \\' . $ this ->fileName ), $ template );
93246 }
94247
95248}
0 commit comments