1: <?php
2:
3: namespace Yajra\Datatables\Generators;
4:
5: use Illuminate\Console\GeneratorCommand;
6: use Illuminate\Support\Str;
7: use Symfony\Component\Console\Input\InputOption;
8:
9: 10: 11: 12: 13: 14:
15: class DataTablesMakeCommand extends GeneratorCommand
16: {
17: 18: 19: 20: 21:
22: protected $name = 'datatables:make';
23:
24: 25: 26: 27: 28:
29: protected $description = 'Create a new DataTable service class.';
30:
31: 32: 33: 34: 35:
36: protected $type = 'DataTable';
37:
38: 39: 40: 41: 42:
43: protected $model;
44:
45: 46: 47: 48: 49:
50: protected $filename;
51:
52: 53: 54: 55: 56: 57:
58: protected function buildClass($name)
59: {
60: $stub = $this->files->get($this->getStub());
61: $stub = $this->replaceNamespace($stub, $name)->replaceClass($stub, $name);
62:
63: return $this->replaceModelImport($stub)->replaceModel($stub)->replaceFilename($stub);
64: }
65:
66: 67: 68: 69: 70:
71: protected function getStub()
72: {
73: return __DIR__ . '/stubs/datatables.stub';
74: }
75:
76: 77: 78: 79: 80: 81:
82: protected function replaceModel(&$stub)
83: {
84: $model = explode('\\', $this->model);
85: $model = array_pop($model);
86: $stub = str_replace('ModelName', $model, $stub);
87:
88: return $this;
89: }
90:
91: 92: 93: 94: 95: 96:
97: protected function replaceModelImport(&$stub)
98: {
99: $stub = str_replace(
100: 'DummyModel', str_replace('\\\\', '\\', $this->model), $stub
101: );
102:
103: return $this;
104: }
105:
106: 107: 108: 109: 110: 111:
112: protected function replaceFilename(&$stub)
113: {
114: $stub = str_replace(
115: 'DummyFilename', $this->filename, $stub
116: );
117:
118: return $stub;
119: }
120:
121: 122: 123: 124: 125:
126: protected function getOptions()
127: {
128: return [
129: ['model', null, InputOption::VALUE_NONE, 'Use the provided name as the model.', null],
130: ];
131: }
132:
133: 134: 135: 136: 137: 138:
139: protected function alreadyExists($rawName)
140: {
141: $name = $this->parseName($rawName);
142:
143: $this->setModel($rawName);
144: $this->setFilename($rawName);
145:
146: return $this->files->exists($this->getPath($name));
147: }
148:
149: 150: 151: 152: 153: 154:
155: protected function parseName($name)
156: {
157: $rootNamespace = $this->laravel->getNamespace();
158:
159: if (Str::startsWith($name, $rootNamespace)) {
160: return $name;
161: }
162:
163: if (Str::contains($name, '/')) {
164: $name = str_replace('/', '\\', $name);
165: }
166:
167: if (! Str::contains(Str::lower($name), 'datatable')) {
168: $name .= 'DataTable';
169: }
170:
171: return $this->parseName($this->getDefaultNamespace(trim($rootNamespace, '\\')) . '\\' . $name);
172: }
173:
174: 175: 176: 177: 178: 179:
180: protected function getDefaultNamespace($rootNamespace)
181: {
182: return $rootNamespace . "\\" . $this->laravel['config']->get('datatables.namespace.base', 'DataTables');
183: }
184:
185: 186: 187: 188: 189:
190: protected function setModel($name)
191: {
192: $rootNamespace = $this->laravel->getNamespace();
193: $modelNamespace = $this->laravel['config']->get('datatables.namespace.model');
194: $this->model = $this->option('model')
195: ? $rootNamespace . "\\" . ($modelNamespace ? $modelNamespace . "\\" : "") . $name
196: : $rootNamespace . "\\User";
197: }
198:
199: 200: 201: 202: 203:
204: protected function setFilename($name)
205: {
206: $this->filename = Str::lower(Str::plural($name));
207: }
208: }
209: