33namespace Backpack \Generators \Console \Commands ;
44
55use Illuminate \Console \GeneratorCommand ;
6+ use Illuminate \Support \Arr ;
67use Illuminate \Support \Str ;
78
89class CrudControllerBackpackCommand extends GeneratorCommand
@@ -89,6 +90,84 @@ protected function replaceNameStrings(&$stub, $name)
8990 return $ this ;
9091 }
9192
93+ protected function getAttributes ($ model )
94+ {
95+ $ attributes = [];
96+ $ model = new $ model ;
97+
98+ // if fillable was defined, use that as the attributes
99+ if (! count ($ model ->getFillable ())) {
100+ $ attributes = $ model ->getFillable ();
101+ } else {
102+ // otherwise, if guarded is used, just pick up the columns straight from the bd table
103+ $ attributes = \Schema::getColumnListing ($ model ->getTable ());
104+ }
105+
106+ return $ attributes ;
107+ }
108+
109+ /**
110+ * Replace the table name for the given stub.
111+ *
112+ * @param string $stub
113+ * @param string $name
114+ *
115+ * @return string
116+ */
117+ protected function replaceSetFromDb (&$ stub , $ name )
118+ {
119+ $ class = str_replace ($ this ->getNamespace ($ name ).'\\' , '' , $ name );
120+ $ model = 'App\Models \\' .$ class ;
121+
122+ if (! class_exists ($ model )) {
123+ return $ this ;
124+ }
125+
126+ $ attributes = $ this ->getAttributes ($ model );
127+
128+ // create an array with the needed code for defining fields
129+ $ fields = Arr::where ($ attributes , function ($ value , $ key ) {
130+ return ! in_array ($ value , ['id ' , 'created_at ' , 'updated_at ' , 'deleted_at ' ]);
131+ });
132+ if (count ($ fields )) {
133+ foreach ($ fields as $ key => $ field ) {
134+ $ fields [$ key ] = "CRUD::field(' " .$ field ."'); " ;
135+ }
136+ }
137+
138+ // create an array with the needed code for defining columns
139+ $ columns = Arr::where ($ attributes , function ($ value , $ key ) {
140+ return ! in_array ($ value , ['id ' ]);
141+ });
142+ if (count ($ columns )) {
143+ foreach ($ columns as $ key => $ column ) {
144+ $ columns [$ key ] = "CRUD::column(' " .$ column ."'); " ;
145+ }
146+ }
147+
148+ // replace setFromDb with actual fields and columns
149+ $ stub = str_replace ('CRUD::setFromDb(); // fields ' , implode (PHP_EOL .' ' , $ fields ), $ stub );
150+ $ stub = str_replace ('CRUD::setFromDb(); // columns ' , implode (PHP_EOL .' ' , $ columns ), $ stub );
151+
152+ return $ this ;
153+ }
154+
155+ /**
156+ * Replace the class name for the given stub.
157+ *
158+ * @param string $stub
159+ * @param string $name
160+ *
161+ * @return string
162+ */
163+ protected function replaceModel (&$ stub , $ name )
164+ {
165+ $ class = str_replace ($ this ->getNamespace ($ name ).'\\' , '' , $ name );
166+ $ stub = str_replace (['DummyClass ' , '{{ class }} ' , '{{class}} ' ], $ class , $ stub );
167+
168+ return $ this ;
169+ }
170+
92171 /**
93172 * Build the class with the given name.
94173 *
@@ -100,7 +179,12 @@ protected function buildClass($name)
100179 {
101180 $ stub = $ this ->files ->get ($ this ->getStub ());
102181
103- return $ this ->replaceNamespace ($ stub , $ name )->replaceNameStrings ($ stub , $ name )->replaceClass ($ stub , $ name );
182+ $ this ->replaceNamespace ($ stub , $ name )
183+ ->replaceNameStrings ($ stub , $ name )
184+ ->replaceModel ($ stub , $ name )
185+ ->replaceSetFromDb ($ stub , $ name );
186+
187+ return $ stub ;
104188 }
105189
106190 /**
0 commit comments