1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sebastienheyd\BoilerplatePackager\Commands\Crud; |
4
|
|
|
|
5
|
|
|
use Illuminate\Filesystem\Filesystem; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
|
8
|
|
|
class Views extends Command |
9
|
|
|
{ |
10
|
|
|
protected $signature = 'boilerplate:packager:crud:views {package} {tables}'; |
11
|
|
|
protected $description = ''; |
12
|
|
|
|
13
|
|
|
public function handle() |
14
|
|
|
{ |
15
|
|
|
foreach ($this->argument('tables') as $table) { |
16
|
|
|
$this->buildViews($table); |
17
|
|
|
} |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
private function buildViews($table) |
21
|
|
|
{ |
22
|
|
|
$package = $this->argument('package'); |
23
|
|
|
[$vendor, $packageName] = explode('/', $package); |
|
|
|
|
24
|
|
|
$columns = $this->getColumnsFromTable($table); |
25
|
|
|
$relations = $this->getTableRelations($table); |
26
|
|
|
|
27
|
|
|
$fields = []; |
28
|
|
|
foreach ($columns as $column) { |
29
|
|
|
if (in_array($column['name'], ['created_at', 'updated_at', 'deleted_at'])) { |
30
|
|
|
continue; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
if (preg_match('#_id$#', $column['name'])) { |
34
|
|
|
continue; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$rules = [$column['required'] ? 'required' : 'nullable']; |
38
|
|
|
|
39
|
|
|
$fields[] = [ |
40
|
|
|
'name' => $column['name'], |
41
|
|
|
'type' => $column['type'], |
42
|
|
|
'rules' => join('|', $rules), |
43
|
|
|
]; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$data = [ |
47
|
|
|
'namespace' => $this->getNamespace($package), |
48
|
|
|
'resource' => $table, |
49
|
|
|
'vendor' => $vendor, |
50
|
|
|
'packageName' => $packageName, |
51
|
|
|
'fields' => $fields, |
52
|
|
|
'relations' => $relations, |
53
|
|
|
]; |
54
|
|
|
|
55
|
|
|
$path = __DIR__.'/../../resources/views/resource'; |
56
|
|
|
$files = (new Filesystem())->allFiles($path); |
57
|
|
|
|
58
|
|
|
foreach ($files as $file) { |
59
|
|
|
$dest = str_replace($path, '', $file); |
60
|
|
|
$view = str_replace(['.blade.php', '/'], '', $dest); |
61
|
|
|
|
62
|
|
|
$content = (string) view('packager::resource.'.$view, $data); |
63
|
|
|
$this->storage->put($package.'/src/resources/views/'.Str::singular($table).$dest, $content); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|