Skip to content

Commit e1c563f

Browse files
committed
Make builder generator optional.
1 parent 4e24600 commit e1c563f

File tree

4 files changed

+288
-248
lines changed

4 files changed

+288
-248
lines changed

src/Generators/DataTablesHtmlCommand.php

Lines changed: 8 additions & 205 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@
33
namespace Yajra\DataTables\Generators;
44

55
use Illuminate\Support\Str;
6-
use Illuminate\Support\Facades\Schema;
7-
use Illuminate\Console\GeneratorCommand;
8-
use Symfony\Component\Console\Input\InputOption;
96

10-
class DataTablesHtmlCommand extends GeneratorCommand
7+
class DataTablesHtmlCommand extends DataTablesMakeCommand
118
{
129
/**
1310
* The name and signature of the console command.
@@ -40,190 +37,21 @@ class DataTablesHtmlCommand extends GeneratorCommand
4037
*
4138
* @param string $name
4239
* @return string
43-
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
4440
*/
4541
protected function buildClass($name)
4642
{
47-
$stub = parent::buildClass($name);
43+
$stub = $this->files->get($this->getStub());
4844

49-
return $this->replaceBuilder($stub)
50-
->replaceColumns($stub)
51-
->replaceButtons($stub)
52-
->replaceDOM($stub)
53-
->replaceTableId($stub);
54-
}
55-
56-
/**
57-
* Replace columns.
58-
*
59-
* @param string $stub
60-
* @return string
61-
*/
62-
protected function replaceTableId(&$stub)
63-
{
64-
$stub = str_replace(
65-
'DummyTableId', Str::lower($this->getNameInput()) . '-table', $stub
66-
);
67-
68-
return $stub;
69-
}
70-
71-
/**
72-
* Replace dom.
73-
*
74-
* @param string $stub
75-
* @return $this
76-
*/
77-
protected function replaceDOM(&$stub)
78-
{
79-
$stub = str_replace(
80-
'DummyDOM',
81-
$this->option('dom') ?: $this->laravel['config']->get('datatables-buttons.generator.dom', 'Bfrtip'),
82-
$stub
83-
);
84-
85-
return $this;
86-
}
87-
88-
/**
89-
* Replace buttons.
90-
*
91-
* @param string $stub
92-
* @return $this
93-
*/
94-
protected function replaceButtons(&$stub)
95-
{
96-
$stub = str_replace(
97-
'DummyButtons', $this->getButtons(), $stub
98-
);
99-
100-
return $this;
101-
}
45+
$stub = $this->replaceNamespace($stub, $name)->replaceClass($stub, $name);
10246

103-
/**
104-
* Get the columns to be used.
105-
*
106-
* @return string
107-
*/
108-
protected function getButtons()
109-
{
110-
if ($this->option('buttons') != '') {
111-
return $this->parseButtons($this->option('buttons'));
112-
} else {
113-
return $this->parseButtons(
114-
$this->laravel['config']->get(
115-
'datatables-buttons.generator.buttons',
116-
'create,export,print,reset,reload'
117-
)
118-
);
119-
}
120-
}
121-
122-
/**
123-
* Parse array from definition.
124-
*
125-
* @param string $definition
126-
* @param int $indentation
127-
* @return string
128-
*/
129-
protected function parseButtons($definition, $indentation = 24)
130-
{
131-
$columns = explode(',', $definition);
132-
$stub = '';
133-
foreach ($columns as $key => $column) {
134-
$indent = '';
135-
$separator = ',';
136-
137-
if ($key < count($columns) - 1) {
138-
$indent = PHP_EOL . str_repeat(' ', $indentation);
139-
}
140-
141-
if ($key == count($columns) - 1) {
142-
$separator = '';
143-
}
144-
145-
$stub .= "Button::make('{$column}')" . $separator . $indent;
146-
}
47+
$this->replaceBuilder($stub)
48+
->replaceColumns($stub)
49+
->replaceButtons($stub)
50+
->replaceDOM($stub)
51+
->replaceTableId($stub);
14752

14853
return $stub;
14954
}
150-
151-
/**
152-
* Replace columns.
153-
*
154-
* @param string $stub
155-
* @return $this
156-
*/
157-
protected function replaceColumns(&$stub)
158-
{
159-
$stub = str_replace(
160-
'DummyColumns', $this->getColumns(), $stub
161-
);
162-
163-
return $this;
164-
}
165-
166-
/**
167-
* Get the columns to be used.
168-
*
169-
* @return string
170-
*/
171-
protected function getColumns()
172-
{
173-
if ($this->option('table')) {
174-
return $this->parseColumns(Schema::getColumnListing($this->option('table')));
175-
}
176-
177-
if ($this->option('columns') != '') {
178-
return $this->parseColumns($this->option('columns'));
179-
} else {
180-
return $this->parseColumns(
181-
$this->laravel['config']->get(
182-
'datatables-buttons.generator.columns',
183-
'id,add your columns,created_at,updated_at'
184-
)
185-
);
186-
}
187-
}
188-
189-
/**
190-
* Parse array from definition.
191-
*
192-
* @param string $definition
193-
* @param int $indentation
194-
* @return string
195-
*/
196-
protected function parseColumns($definition, $indentation = 12)
197-
{
198-
$columns = is_array($definition) ? $definition : explode(',', $definition);
199-
$stub = '';
200-
foreach ($columns as $key => $column) {
201-
$stub .= "Column::make('{$column}'),";
202-
203-
if ($key < count($columns) - 1) {
204-
$stub .= PHP_EOL . str_repeat(' ', $indentation);
205-
}
206-
}
207-
208-
return $stub;
209-
}
210-
211-
/**
212-
* Replace builder name.
213-
*
214-
* @param string $stub
215-
* @return self
216-
*/
217-
protected function replaceBuilder(&$stub)
218-
{
219-
$name = $this->qualifyClass($this->getNameInput());
220-
$class = str_replace($this->getNamespace($name) . '\\', '', $name);
221-
222-
$stub = str_replace('DummyBuilder', $class . 'Html', $stub);
223-
224-
return $this;
225-
}
226-
22755
/**
22856
* Parse the name and format according to the root namespace.
22957
*
@@ -249,17 +77,6 @@ protected function qualifyClass($name)
24977
return $this->getDefaultNamespace(trim($rootNamespace, '\\')) . '\\' . $name;
25078
}
25179

252-
/**
253-
* Get the default namespace for the class.
254-
*
255-
* @param string $rootNamespace
256-
* @return string
257-
*/
258-
protected function getDefaultNamespace($rootNamespace)
259-
{
260-
return $rootNamespace . '\\' . $this->laravel['config']->get('datatables-buttons.namespace.base', 'DataTables');
261-
}
262-
26380
/**
26481
* Get the stub file for the generator.
26582
*
@@ -273,18 +90,4 @@ protected function getStub()
27390
? base_path() . $config->get('datatables-buttons.stub') . '/html.stub'
27491
: __DIR__ . '/stubs/html.stub';
27592
}
276-
277-
/**
278-
* Get the console command options.
279-
*
280-
* @return array
281-
*/
282-
protected function getOptions()
283-
{
284-
return [
285-
['columns', null, InputOption::VALUE_OPTIONAL, 'Use the provided columns.', null],
286-
['buttons', null, InputOption::VALUE_OPTIONAL, 'Use the provided buttons.', null],
287-
['dom', null, InputOption::VALUE_OPTIONAL, 'Use the provided DOM.', null],
288-
];
289-
}
29093
}

0 commit comments

Comments
 (0)