@@ -18,6 +18,12 @@ class ControllerCreator
1818 */
1919 protected $ files ;
2020
21+ protected $ DummyGridField = '' ;
22+
23+ protected $ DummyShowField = '' ;
24+
25+ protected $ DummyFormField = '' ;
26+
2127 /**
2228 * ControllerCreator constructor.
2329 *
@@ -40,14 +46,21 @@ public function __construct($name, $files = null)
4046 *
4147 * @return string
4248 */
43- public function create ($ model )
49+ public function create ($ model , $ fields )
4450 {
4551 $ path = $ this ->getpath ($ this ->name );
4652
4753 if ($ this ->files ->exists ($ path )) {
4854 throw new \Exception ("Controller [ $ this ->name ] already exists! " );
4955 }
5056
57+ $ this ->generateGridField ($ fields );
58+
59+ $ this ->generateShowField ($ fields );
60+
61+ $ this ->generateFormField ($ fields );
62+
63+
5164 $ stub = $ this ->files ->get ($ this ->getStub ());
5265
5366 $ this ->files ->put ($ path , $ this ->replace ($ stub , $ this ->name , $ model ));
@@ -67,8 +80,8 @@ protected function replace($stub, $name, $model)
6780 $ stub = $ this ->replaceClass ($ stub , $ name );
6881
6982 return str_replace (
70- ['DummyModelNamespace ' , 'DummyModel ' ],
71- [$ model , class_basename ($ model )],
83+ ['DummyModelNamespace ' , 'DummyModel ' , ' DummyGridField ' , ' DummyShowField ' , ' DummyFormField ' ],
84+ [$ model , class_basename ($ model ), $ this -> DummyGridField , $ this -> DummyShowField , $ this -> DummyFormField ],
7285 $ stub
7386 );
7487 }
@@ -125,4 +138,60 @@ public function getStub()
125138 {
126139 return __DIR__ .'/stubs/controller.stub ' ;
127140 }
141+
142+ public function generateFormField ($ fields = [] )
143+ {
144+ $ fields = array_filter ($ fields , function ($ field ) {
145+ return isset ($ field ['name ' ]) && !empty ($ field ['name ' ]);
146+ });
147+
148+ if (empty ($ fields )) {
149+ throw new \Exception ('Table fields can \'t be empty ' );
150+ }
151+
152+ foreach ($ fields as $ field ) {
153+ $ rows [] = "\$form->text(' {$ field ['name ' ]}', ' {$ field ['name ' ]}'); \n" ;
154+ }
155+
156+ $ this ->DummyFormField = trim (implode (str_repeat (' ' , 8 ), $ rows ), "\n" );
157+
158+ return $ this ;
159+ }
160+
161+ public function generateShowField ($ fields = [] )
162+ {
163+ $ fields = array_filter ($ fields , function ($ field ) {
164+ return isset ($ field ['name ' ]) && !empty ($ field ['name ' ]);
165+ });
166+
167+ if (empty ($ fields )) {
168+ throw new \Exception ('Table fields can \'t be empty ' );
169+ }
170+ foreach ($ fields as $ field ) {
171+ $ rows [] = "\$show-> {$ field ['name ' ]}(' {$ field ['name ' ]}'); \n" ;
172+ }
173+
174+ $ this ->DummyShowField = trim (implode (str_repeat (' ' , 8 ), $ rows ), "\n" );
175+
176+ return $ this ;
177+ }
178+
179+
180+ public function generateGridField ($ fields = [] )
181+ {
182+ $ fields = array_filter ($ fields , function ($ field ) {
183+ return isset ($ field ['name ' ]) && !empty ($ field ['name ' ]);
184+ });
185+
186+ if (empty ($ fields )) {
187+ throw new \Exception ('Table fields can \'t be empty ' );
188+ }
189+ foreach ($ fields as $ field ) {
190+ $ rows [] = "\$grid-> {$ field ['name ' ]}(' {$ field ['name ' ]}'); \n" ;
191+ }
192+
193+ $ this ->DummyGridField = trim (implode (str_repeat (' ' , 8 ), $ rows ), "\n" );
194+
195+ return $ this ;
196+ }
128197}
0 commit comments