Skip to content

Commit a9db1e7

Browse files
committed
Add DataTableHtml generator command.
Create HTML when making a new DataTable.
1 parent 5c029d7 commit a9db1e7

File tree

6 files changed

+454
-123
lines changed

6 files changed

+454
-123
lines changed

src/ButtonsServiceProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Support\ServiceProvider;
66
use Maatwebsite\Excel\ExcelServiceProvider;
7+
use Yajra\DataTables\Generators\DataTablesHtmlCommand;
78
use Yajra\DataTables\Generators\DataTablesMakeCommand;
89
use Yajra\DataTables\Generators\DataTablesScopeCommand;
910

@@ -48,6 +49,7 @@ protected function registerCommands()
4849
{
4950
$this->commands(DataTablesMakeCommand::class);
5051
$this->commands(DataTablesScopeCommand::class);
52+
$this->commands(DataTablesHtmlCommand::class);
5153
}
5254

5355
/**
Lines changed: 284 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,284 @@
1+
<?php
2+
3+
namespace Yajra\DataTables\Generators;
4+
5+
use Illuminate\Support\Str;
6+
use Illuminate\Console\GeneratorCommand;
7+
use Symfony\Component\Console\Input\InputOption;
8+
9+
class DataTablesHtmlCommand extends GeneratorCommand
10+
{
11+
/**
12+
* The name and signature of the console command.
13+
*
14+
* @var string
15+
*/
16+
protected $signature = 'datatables:html
17+
{name : The name of the datatable html.}
18+
{--dom= : The dom of the datatable.}
19+
{--buttons= : The buttons of the datatable.}
20+
{--columns= : The columns of the datatable.}';
21+
22+
/**
23+
* The console command description.
24+
*
25+
* @var string
26+
*/
27+
protected $description = 'Create a new dataTable html class.';
28+
29+
/**
30+
* The type of class being generated.
31+
*
32+
* @var string
33+
*/
34+
protected $type = 'DataTableHtml';
35+
36+
/**
37+
* Build the class with the given name.
38+
*
39+
* @param string $name
40+
* @return string
41+
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
42+
*/
43+
protected function buildClass($name)
44+
{
45+
$stub = parent::buildClass($name);
46+
47+
return $this->replaceBuilder($stub)
48+
->replaceColumns($stub)
49+
->replaceButtons($stub)
50+
->replaceDOM($stub)
51+
->replaceTableId($stub);
52+
}
53+
54+
/**
55+
* Replace columns.
56+
*
57+
* @param string $stub
58+
* @return string
59+
*/
60+
protected function replaceTableId(&$stub)
61+
{
62+
$stub = str_replace(
63+
'DummyTableId', Str::lower($this->getNameInput()) . '-table', $stub
64+
);
65+
66+
return $stub;
67+
}
68+
69+
/**
70+
* Replace dom.
71+
*
72+
* @param string $stub
73+
* @return $this
74+
*/
75+
protected function replaceDOM(&$stub)
76+
{
77+
$stub = str_replace(
78+
'DummyDOM',
79+
$this->option('dom') ?: $this->laravel['config']->get('datatables-buttons.generator.dom', 'Bfrtip'),
80+
$stub
81+
);
82+
83+
return $this;
84+
}
85+
86+
/**
87+
* Replace buttons.
88+
*
89+
* @param string $stub
90+
* @return $this
91+
*/
92+
protected function replaceButtons(&$stub)
93+
{
94+
$stub = str_replace(
95+
'DummyButtons', $this->getButtons(), $stub
96+
);
97+
98+
return $this;
99+
}
100+
101+
/**
102+
* Get the columns to be used.
103+
*
104+
* @return string
105+
*/
106+
protected function getButtons()
107+
{
108+
if ($this->option('buttons') != '') {
109+
return $this->parseButtons($this->option('buttons'));
110+
} else {
111+
return $this->parseButtons(
112+
$this->laravel['config']->get(
113+
'datatables-buttons.generator.buttons',
114+
'create,export,print,reset,reload'
115+
)
116+
);
117+
}
118+
}
119+
120+
/**
121+
* Parse array from definition.
122+
*
123+
* @param string $definition
124+
* @param int $indentation
125+
* @return string
126+
*/
127+
protected function parseButtons($definition, $indentation = 24)
128+
{
129+
$columns = explode(',', $definition);
130+
$stub = '';
131+
foreach ($columns as $key => $column) {
132+
$indent = '';
133+
$separator = ',';
134+
135+
if ($key < count($columns) - 1) {
136+
$indent = PHP_EOL . str_repeat(' ', $indentation);
137+
}
138+
139+
if ($key == count($columns) - 1) {
140+
$separator = '';
141+
}
142+
143+
$stub .= "Button::make('{$column}')" . $separator . $indent;
144+
}
145+
146+
return $stub;
147+
}
148+
149+
/**
150+
* Replace columns.
151+
*
152+
* @param string $stub
153+
* @return $this
154+
*/
155+
protected function replaceColumns(&$stub)
156+
{
157+
$stub = str_replace(
158+
'DummyColumns', $this->getColumns(), $stub
159+
);
160+
161+
return $this;
162+
}
163+
164+
/**
165+
* Get the columns to be used.
166+
*
167+
* @return string
168+
*/
169+
protected function getColumns()
170+
{
171+
if ($this->option('columns') != '') {
172+
return $this->parseColumns($this->option('columns'));
173+
} else {
174+
return $this->parseColumns(
175+
$this->laravel['config']->get(
176+
'datatables-buttons.generator.columns',
177+
'id,add your columns,created_at,updated_at'
178+
)
179+
);
180+
}
181+
}
182+
183+
/**
184+
* Parse array from definition.
185+
*
186+
* @param string $definition
187+
* @param int $indentation
188+
* @return string
189+
*/
190+
protected function parseColumns($definition, $indentation = 12)
191+
{
192+
$columns = explode(',', $definition);
193+
$stub = '';
194+
foreach ($columns as $key => $column) {
195+
$stub .= "Column::make('{$column}'),";
196+
197+
if ($key < count($columns) - 1) {
198+
$stub .= PHP_EOL . str_repeat(' ', $indentation);
199+
}
200+
}
201+
202+
return $stub;
203+
}
204+
205+
/**
206+
* Replace builder name.
207+
*
208+
* @param string $stub
209+
* @return self
210+
*/
211+
protected function replaceBuilder(&$stub)
212+
{
213+
$name = $this->qualifyClass($this->getNameInput());
214+
$class = str_replace($this->getNamespace($name) . '\\', '', $name);
215+
216+
$stub = str_replace('DummyBuilder', $class . 'Html', $stub);
217+
218+
return $this;
219+
}
220+
221+
/**
222+
* Parse the name and format according to the root namespace.
223+
*
224+
* @param string $name
225+
* @return string
226+
*/
227+
protected function qualifyClass($name)
228+
{
229+
$rootNamespace = $this->laravel->getNamespace();
230+
231+
if (Str::startsWith($name, $rootNamespace)) {
232+
return $name;
233+
}
234+
235+
if (Str::contains($name, '/')) {
236+
$name = str_replace('/', '\\', $name);
237+
}
238+
239+
if (! Str::contains(Str::lower($name), 'datatable')) {
240+
$name .= 'DataTableHtml';
241+
}
242+
243+
return $this->getDefaultNamespace(trim($rootNamespace, '\\')) . '\\' . $name;
244+
}
245+
246+
/**
247+
* Get the default namespace for the class.
248+
*
249+
* @param string $rootNamespace
250+
* @return string
251+
*/
252+
protected function getDefaultNamespace($rootNamespace)
253+
{
254+
return $rootNamespace . '\\' . $this->laravel['config']->get('datatables-buttons.namespace.base', 'DataTables');
255+
}
256+
257+
/**
258+
* Get the stub file for the generator.
259+
*
260+
* @return string
261+
*/
262+
protected function getStub()
263+
{
264+
$config = $this->laravel['config'];
265+
266+
return $config->get('datatables-buttons.stub')
267+
? base_path() . $config->get('datatables-buttons.stub') . '/html.stub'
268+
: __DIR__ . '/stubs/html.stub';
269+
}
270+
271+
/**
272+
* Get the console command options.
273+
*
274+
* @return array
275+
*/
276+
protected function getOptions()
277+
{
278+
return [
279+
['columns', null, InputOption::VALUE_OPTIONAL, 'Use the provided columns.', null],
280+
['buttons', null, InputOption::VALUE_OPTIONAL, 'Use the provided buttons.', null],
281+
['dom', null, InputOption::VALUE_OPTIONAL, 'Use the provided DOM.', null],
282+
];
283+
}
284+
}

0 commit comments

Comments
 (0)