1515class  DataTablesMakeCommand extends  GeneratorCommand
1616{
1717 /** 
18-  * The console command name . 
18+  * The name and signature of the  console command. 
1919 * 
2020 * @var string 
2121 */ 
22-  protected  $ name'datatables:make ' ;
22+  protected  $ signature'datatables:make  
23+  {name : The name of the datatable.} 
24+  {--model : The name of the model to be used.} 
25+  {--model-namespace= : The namespace of the model to be used.} 
26+  {--action= : The path of the action view.} 
27+  {--columns= : The columns of the datatable.} '
2328
2429 /** 
2530 * The console command description. 
@@ -47,14 +52,16 @@ protected function buildClass($name)
4752
4853 return  $ this replaceModelImport ($ stub
4954 ->replaceModel ($ stub
55+  ->replaceColumns ($ stub
56+  ->replaceAction ($ stub
5057 ->replaceFilename ($ stub
5158 }
5259
5360 /** 
5461 * Replace model name. 
5562 * 
5663 * @param string $stub 
57-  * @return mixed  
64+  * @return this  
5865 */ 
5966 protected  function  replaceModel (&$ stub
6067 {
@@ -72,13 +79,82 @@ protected function getModel()
7279 {
7380 $ name$ this getNameInput ();
7481 $ rootNamespace$ this laravel ->getNamespace ();
75-  $ modelNamespace$ this laravel ['config ' ]->get ('datatables-buttons.namespace.model ' );
82+  $ model$ this option ('model ' ) || $ this option ('model-namespace ' );
83+  $ modelNamespace$ this option ('model-namespace ' ) ? $ this option ('model-namespace ' ) : $ this laravel ['config ' ]->get ('datatables-buttons.namespace.model ' );
7684
77-  return  $ this -> option ( ' model ' ) 
78-  ? $ rootNamespace"\\"  . ($ modelNamespace$ modelNamespace"\\"  : "" ) . str_singular ($ name
85+  return  $ model
86+  ? $ rootNamespace"\\"  . ($ modelNamespace$ modelNamespace"\\"  : "" ) . str_singular ($ name
7987 : $ rootNamespace"\\User " ;
8088 }
8189
90+  /** 
91+  * Replace columns. 
92+  * 
93+  * @param string $stub 
94+  * @return $this 
95+  */ 
96+  protected  function  replaceColumns (&$ stub
97+  {
98+  $ stubstr_replace (
99+  'DummyColumns ' , $ this getColumns (), $ stub
100+  );
101+ 102+  return  $ this 
103+  }
104+ 105+  /** 
106+  * Get the columns to be used. 
107+  * 
108+  * @return string 
109+  */ 
110+  protected  function  getColumns ()
111+  {
112+  if ($ this option ('columns ' ) != '' ){
113+  return  $ this parseArray ($ this option ('columns ' ));
114+  }else {
115+  return  $ this parseArray ('id,add your columns,created_at,updated_at ' );
116+  }
117+  }
118+ 119+  /** 
120+  * Parse array from definition 
121+  * 
122+  * @param string $definition 
123+  * @param string $delimiter 
124+  * @param int $indentation 
125+  * @return string 
126+  */ 
127+  protected  function  parseArray ($ definition$ delimiter', ' , $ indentation12 )
128+  {
129+  return  str_replace ($ delimiter"', \n"  . str_repeat ('' , $ indentation"' " , $ definition
130+  }
131+ 132+  /** 
133+  * Replace the action. 
134+  * 
135+  * @param string $stub 
136+  * @return this 
137+  */ 
138+  protected  function  replaceAction (&$ stub
139+  {
140+  $ stubstr_replace (
141+  'DummyAction ' , $ this getAction (), $ stub
142+  );
143+ 144+  return  $ this 
145+  }
146+ 147+  /** 
148+  * Set the action view to be used. 
149+  * 
150+  * @return string 
151+  */ 
152+  protected  function  getAction ()
153+  {
154+  return  $ this option ('action ' ) ? $ this option ('action ' ) : Str::lower ($ this getNameInput ()) . '.action ' ;
155+ 156+  }
157+ 82158 /** 
83159 * Replace model import. 
84160 * 
@@ -101,7 +177,10 @@ protected function replaceModelImport(&$stub)
101177 */ 
102178 protected  function  getStub ()
103179 {
104-  return  __DIR__  . '/stubs/datatables.stub ' ;
180+  $ config$ this laravel ['config ' ];
181+  return  $ configget ('datatables-buttons.stub ' )
182+  ? base_path () . $ configget ('datatables-buttons.stub ' ) . '/datatables.stub ' 
183+  : __DIR__  . '/stubs/datatables.stub ' ;
105184 }
106185
107186 /** 
@@ -128,6 +207,8 @@ protected function getOptions()
128207 {
129208 return  [
130209 ['model ' , null , InputOption::VALUE_NONE , 'Use the provided name as the model. ' , null ],
210+  ['action ' , null , InputOption::VALUE_OPTIONAL , 'Path to action column template. ' , null ],
211+  ['columns ' , null , InputOption::VALUE_OPTIONAL , 'Use the provided columns. ' , null ],
131212 ];
132213 }
133214
0 commit comments