|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Webfactor\Laravel\Generators\Services; |
|
4
|
|
|
|
|
5
|
|
|
use Webfactor\Laravel\Generators\Contracts\ServiceAbstract; |
|
6
|
|
|
use Webfactor\Laravel\Generators\Contracts\ServiceInterface; |
|
7
|
|
|
use Webfactor\Laravel\Generators\Helper\ShortSyntaxArray; |
|
8
|
|
|
use Webfactor\Laravel\Generators\Schemas\CrudColumn; |
|
9
|
|
|
use Webfactor\Laravel\Generators\Schemas\CrudField; |
|
10
|
|
|
|
|
11
|
|
|
class BackpackCrudControllerService extends ServiceAbstract implements ServiceInterface |
|
12
|
|
|
{ |
|
13
|
|
|
protected $relativeToBasePath = 'app/Http/Controllers/Admin'; |
|
14
|
|
|
|
|
15
|
|
|
protected $columns = []; |
|
16
|
|
|
|
|
17
|
|
|
protected $fields = []; |
|
18
|
|
|
|
|
19
|
|
|
public function call() |
|
20
|
|
|
{ |
|
21
|
|
|
$this->command->call('make:crud-controller', [ |
|
22
|
|
|
'name' => $this->getName($this->command->entity), |
|
23
|
|
|
]); |
|
24
|
|
|
|
|
25
|
|
|
$this->addLatestFileToIdeStack(); |
|
26
|
|
|
|
|
27
|
|
|
$this->setColumnsFromSchema(); |
|
28
|
|
|
$this->setFieldsFromSchema(); |
|
29
|
|
|
|
|
30
|
|
|
$this->fillColumnsAndFieldsInGeneratedControllerFromSchema(); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function getName(string $entity): string |
|
34
|
|
|
{ |
|
35
|
|
|
return ucfirst($entity); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
private function fillColumnsAndFieldsInGeneratedControllerFromSchema(): void |
|
39
|
|
|
{ |
|
40
|
|
|
$controllerFile = end($this->command->filesToBeOpened); |
|
41
|
|
|
|
|
42
|
|
|
$controller = $this->filesystem->get($controllerFile); |
|
43
|
|
|
$controller = str_replace('__columns__', $this->getColumnsAsString(), $controller); |
|
44
|
|
|
$controller = str_replace('__fields__', $this->getfieldsAsString(), $controller); |
|
45
|
|
|
$this->filesystem->put($controllerFile, $controller); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
private function setColumnsFromSchema(): void |
|
49
|
|
|
{ |
|
50
|
|
|
$this->command->schema->getStructure()->each(function ($field) { |
|
51
|
|
|
array_push($this->columns, new CrudColumn($field)); |
|
52
|
|
|
}); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
private function setFieldsFromSchema(): void |
|
56
|
|
|
{ |
|
57
|
|
|
$this->command->schema->getStructure()->each(function ($field) { |
|
58
|
|
|
array_push($this->fields, new CrudField($field)); |
|
59
|
|
|
}); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @return string |
|
64
|
|
|
*/ |
|
65
|
|
|
private function getColumnsAsString(): string |
|
66
|
|
|
{ |
|
67
|
|
|
$columnsArray = []; |
|
68
|
|
|
|
|
69
|
|
|
foreach ($this->columns as $crudColumn) { |
|
70
|
|
|
if ($column = $crudColumn->generateColumn()) { |
|
71
|
|
|
array_push($columnsArray, $column); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return ShortSyntaxArray::parse($columnsArray); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @return string |
|
80
|
|
|
*/ |
|
81
|
|
|
private function getfieldsAsString(): string |
|
82
|
|
|
{ |
|
83
|
|
|
$fieldsArray = []; |
|
84
|
|
|
|
|
85
|
|
|
foreach ($this->fields as $crudfield) { |
|
86
|
|
|
if ($field = $crudfield->generateField()) { |
|
87
|
|
|
array_push($fieldsArray, $field); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return ShortSyntaxArray::parse($fieldsArray); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|