@@ -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,20 @@ 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+
5163 $ stub = $ this ->files ->get ($ this ->getStub ());
5264
5365 $ this ->files ->put ($ path , $ this ->replace ($ stub , $ this ->name , $ model ));
@@ -67,8 +79,8 @@ protected function replace($stub, $name, $model)
6779 $ stub = $ this ->replaceClass ($ stub , $ name );
6880
6981 return str_replace (
70- ['DummyModelNamespace ' , 'DummyModel ' ],
71- [$ model , class_basename ($ model )],
82+ ['DummyModelNamespace ' , 'DummyModel ' , ' DummyGridField ' , ' DummyShowField ' , ' DummyFormField ' ],
83+ [$ model , class_basename ($ model ), $ this -> DummyGridField , $ this -> DummyShowField , $ this -> DummyFormField ],
7284 $ stub
7385 );
7486 }
@@ -125,4 +137,59 @@ public function getStub()
125137 {
126138 return __DIR__ .'/stubs/controller.stub ' ;
127139 }
140+
141+ public function generateFormField ($ fields = [])
142+ {
143+ $ fields = array_filter ($ fields , function ($ field ) {
144+ return isset ($ field ['name ' ]) && !empty ($ field ['name ' ]);
145+ });
146+
147+ if (empty ($ fields )) {
148+ throw new \Exception ('Table fields can \'t be empty ' );
149+ }
150+
151+ foreach ($ fields as $ field ) {
152+ $ rows [] = "\$form->text(' {$ field ['name ' ]}', ' {$ field ['name ' ]}'); \n" ;
153+ }
154+
155+ $ this ->DummyFormField = trim (implode (str_repeat (' ' , 8 ), $ rows ), "\n" );
156+
157+ return $ this ;
158+ }
159+
160+ public function generateShowField ($ fields = [])
161+ {
162+ $ fields = array_filter ($ fields , function ($ field ) {
163+ return isset ($ field ['name ' ]) && !empty ($ field ['name ' ]);
164+ });
165+
166+ if (empty ($ fields )) {
167+ throw new \Exception ('Table fields can \'t be empty ' );
168+ }
169+ foreach ($ fields as $ field ) {
170+ $ rows [] = "\$show-> {$ field ['name ' ]}(' {$ field ['name ' ]}'); \n" ;
171+ }
172+
173+ $ this ->DummyShowField = trim (implode (str_repeat (' ' , 8 ), $ rows ), "\n" );
174+
175+ return $ this ;
176+ }
177+
178+ public function generateGridField ($ fields = [])
179+ {
180+ $ fields = array_filter ($ fields , function ($ field ) {
181+ return isset ($ field ['name ' ]) && !empty ($ field ['name ' ]);
182+ });
183+
184+ if (empty ($ fields )) {
185+ throw new \Exception ('Table fields can \'t be empty ' );
186+ }
187+ foreach ($ fields as $ field ) {
188+ $ rows [] = "\$grid-> {$ field ['name ' ]}(' {$ field ['name ' ]}'); \n" ;
189+ }
190+
191+ $ this ->DummyGridField = trim (implode (str_repeat (' ' , 8 ), $ rows ), "\n" );
192+
193+ return $ this ;
194+ }
128195}
0 commit comments