Skip to content

Commit de71db6

Browse files
authored
Merge pull request #99 from yajra/htmlBuilder
[4.0] Extract html builder to own class.
2 parents 732c1cc + 3a1351d commit de71db6

File tree

10 files changed

+562
-90
lines changed

10 files changed

+562
-90
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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Yajra\DataTables\Contracts;
4+
5+
interface DataTableHtmlBuilder
6+
{
7+
/**
8+
* Handle building of dataTables html.
9+
*
10+
* @return \Yajra\DataTables\Html\Builder
11+
* @throws \Exception
12+
*/
13+
public function handle();
14+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
namespace Yajra\DataTables\Generators;
4+
5+
use Illuminate\Support\Str;
6+
7+
class DataTablesHtmlCommand extends DataTablesMakeCommand
8+
{
9+
/**
10+
* The name and signature of the console command.
11+
*
12+
* @var string
13+
*/
14+
protected $signature = 'datatables:html
15+
{name : The name of the DataTable html.}
16+
{--dom= : The dom of the DataTable.}
17+
{--buttons= : The buttons of the DataTable.}
18+
{--table= : Scaffold columns from the table.}
19+
{--builder : Ignore, added to work with parent generator.}
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+
*/
42+
protected function buildClass($name)
43+
{
44+
$stub = $this->files->get($this->getStub());
45+
46+
$stub = $this->replaceNamespace($stub, $name)->replaceClass($stub, $name);
47+
48+
$this->replaceBuilder($stub)
49+
->replaceColumns($stub)
50+
->replaceButtons($stub)
51+
->replaceDOM($stub)
52+
->replaceTableId($stub);
53+
54+
return $stub;
55+
}
56+
57+
/**
58+
* Get the stub file for the generator.
59+
*
60+
* @return string
61+
*/
62+
protected function getStub()
63+
{
64+
$config = $this->laravel['config'];
65+
66+
return $config->get('datatables-buttons.stub')
67+
? base_path() . $config->get('datatables-buttons.stub') . '/html.stub'
68+
: __DIR__ . '/stubs/html.stub';
69+
}
70+
71+
/**
72+
* Parse the name and format according to the root namespace.
73+
*
74+
* @param string $name
75+
* @return string
76+
*/
77+
protected function qualifyClass($name)
78+
{
79+
$rootNamespace = $this->laravel->getNamespace();
80+
81+
if (Str::startsWith($name, $rootNamespace)) {
82+
return $name;
83+
}
84+
85+
if (Str::contains($name, '/')) {
86+
$name = str_replace('/', '\\', $name);
87+
}
88+
89+
if (! Str::contains(Str::lower($name), 'datatable')) {
90+
$name .= 'DataTableHtml';
91+
}
92+
93+
return $this->getDefaultNamespace(trim($rootNamespace, '\\')) . '\\' . $name;
94+
}
95+
}

0 commit comments

Comments
 (0)