1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Distilleries\DatatableBuilder\Console; |
4
|
|
|
|
5
|
|
|
use Illuminate\Filesystem\Filesystem; |
6
|
|
|
use Illuminate\Console\GeneratorCommand; |
7
|
|
|
use Symfony\Component\Console\Input\InputOption; |
8
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
9
|
|
|
use Distilleries\DatatableBuilder\Console\Lib\Generators\DatatableGenerator; |
10
|
|
|
|
11
|
|
|
class DatatableMakeCommand extends GeneratorCommand |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* The console command name. |
15
|
|
|
* |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $name = 'datatable:make'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The console command description. |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $description = 'Creates a datable builder class.'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The filesystem instance. |
29
|
|
|
* |
30
|
|
|
* @var \Illuminate\Filesystem\Filesystem |
31
|
|
|
*/ |
32
|
|
|
protected $files; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Datatable generator instance. |
36
|
|
|
* |
37
|
|
|
* @var \Distilleries\DatatableBuilder\Console\Lib\Generators\DatatableGenerator |
38
|
|
|
*/ |
39
|
|
|
protected $formGenerator; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* DatatableMakeCommand constructor. |
43
|
|
|
* |
44
|
|
|
* @param \Illuminate\Filesystem\Filesystem $files |
45
|
|
|
* @param \Distilleries\DatatableBuilder\Console\Lib\Generators\DatatableGenerator $formGenerator |
46
|
|
|
*/ |
47
|
8 |
|
public function __construct(Filesystem $files, DatatableGenerator $formGenerator) |
48
|
|
|
{ |
49
|
8 |
|
parent::__construct($files); |
50
|
|
|
|
51
|
8 |
|
$this->formGenerator = $formGenerator; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get the console command arguments. |
56
|
|
|
* |
57
|
|
|
* @return array |
58
|
|
|
*/ |
59
|
8 |
|
protected function getArguments() |
60
|
|
|
{ |
61
|
|
|
return [ |
62
|
8 |
|
['name', InputArgument::REQUIRED, 'Full path for datatable class.'], |
63
|
|
|
]; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get the console command options. |
68
|
|
|
* |
69
|
|
|
* @return array |
70
|
|
|
*/ |
71
|
8 |
|
protected function getOptions() |
72
|
|
|
{ |
73
|
|
|
return [ |
74
|
8 |
|
['fields', null, InputOption::VALUE_OPTIONAL, 'Fields for the datatable'], |
75
|
|
|
]; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Replace the class name for the given stub. |
80
|
|
|
* |
81
|
|
|
* @param string $stub |
82
|
|
|
* @param string $name |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
4 |
|
protected function replaceClass($stub, $name) |
86
|
|
|
{ |
87
|
4 |
|
$formGenerator = $this->formGenerator; |
88
|
|
|
|
89
|
4 |
|
$stub = str_replace( |
90
|
4 |
|
'{{class}}', |
91
|
4 |
|
$formGenerator->getClassInfo($name)->className, |
92
|
2 |
|
$stub |
93
|
|
|
); |
94
|
|
|
|
95
|
4 |
|
return str_replace( |
96
|
4 |
|
'{{fields}}', |
97
|
4 |
|
$formGenerator->getFieldsVariable($this->option('fields')), |
98
|
2 |
|
$stub |
99
|
|
|
); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Replace the namespace for the given stub. |
104
|
|
|
* |
105
|
|
|
* @param string $stub |
106
|
|
|
* @param string $name |
107
|
|
|
* @return $this |
108
|
|
|
*/ |
109
|
4 |
|
protected function replaceNamespace(&$stub, $name) |
110
|
|
|
{ |
111
|
4 |
|
$stub = str_replace( |
112
|
4 |
|
'{{namespace}}', |
113
|
4 |
|
$this->formGenerator->getClassInfo($name)->namespace, |
114
|
2 |
|
$stub |
115
|
|
|
); |
116
|
|
|
|
117
|
4 |
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Get the desired class name from the input. |
122
|
|
|
* |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
6 |
|
protected function getNameInput() |
126
|
|
|
{ |
127
|
6 |
|
return str_replace('/', '\\', $this->argument('name')); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Get the stub file for the generator. |
132
|
|
|
* |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
4 |
|
protected function getStub() |
136
|
|
|
{ |
137
|
4 |
|
return __DIR__ . '/Lib/stubs/datatable-class-template.stub'; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|