1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ngtfkx\Laradeck\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
|
8
|
|
|
class MakeViewCommand extends Command |
9
|
|
|
{ |
10
|
|
|
protected $signature = 'laradeck:view {name} {--force} {--extends=} {--section=*} {--stack=*} {--component=*}'; |
11
|
|
|
|
12
|
|
|
protected $description = 'Make view'; |
13
|
|
|
|
14
|
|
|
public function __construct() |
15
|
|
|
{ |
16
|
|
|
parent::__construct(); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function handle() |
20
|
|
|
{ |
21
|
|
|
$view = $this->argument('name'); |
22
|
|
|
|
23
|
|
|
$force = $this->option('force'); |
24
|
|
|
|
25
|
|
|
$extends = $this->option('extends'); |
26
|
|
|
|
27
|
|
|
$sections = $this->parse($this->option('section')); |
28
|
|
|
|
29
|
|
|
$stacks = $this->parse($this->option('stack')); |
30
|
|
|
|
31
|
|
|
$components = $this->parse($this->option('component')); |
32
|
|
|
|
33
|
|
|
$path = $this->path($view); |
34
|
|
|
|
35
|
|
|
$content = $this->content($extends, $sections, $stacks, $components); |
36
|
|
|
|
37
|
|
|
$this->create($path, $content, $force); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
protected function path(string $view): string |
41
|
|
|
{ |
42
|
|
|
$path = resource_path('views' . DIRECTORY_SEPARATOR . str_replace('.', DIRECTORY_SEPARATOR, $view) . '.blade.php'); |
43
|
|
|
|
44
|
|
|
$directory = dirname($path); |
45
|
|
|
|
46
|
|
|
if (!is_dir($directory)) { |
47
|
|
|
\File::makeDirectory($directory, 0755, true); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return $path; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
protected function parse($value): array |
54
|
|
|
{ |
55
|
|
|
$values = []; |
56
|
|
|
|
57
|
|
|
foreach ($value as $item) { |
58
|
|
|
if (Str::contains($item, ',')) { |
59
|
|
|
$values = array_merge($values, explode(',', $item)); |
60
|
|
|
} else { |
61
|
|
|
$values[] = $item; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $values; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
protected function block($name, $type): string |
69
|
|
|
{ |
70
|
|
|
$content = PHP_EOL . PHP_EOL . "@" . $type . "('" . $name . "')" . PHP_EOL; |
71
|
|
|
$content .= "@end" . $type; |
72
|
|
|
|
73
|
|
|
return $content; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
protected function content($extends, $sections, $stacks, $components): string |
77
|
|
|
{ |
78
|
|
|
$content = ''; |
79
|
|
|
|
80
|
|
|
if ($extends) { |
81
|
|
|
$content .= "@extends('" . $extends . "')"; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$types = [ |
85
|
|
|
'section' => $sections, |
86
|
|
|
'component' => $components, |
87
|
|
|
'push' => $stacks, |
88
|
|
|
]; |
89
|
|
|
|
90
|
|
|
foreach ($types as $key => $values) { |
91
|
|
|
foreach ($values as $item) { |
92
|
|
|
$content .= $this->block($item, $key); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $content; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
protected function create(string $path, string $content, bool $force) |
100
|
|
|
{ |
101
|
|
|
if ($force || !file_exists($path)) { |
102
|
|
|
\File::put($path, $content); |
103
|
|
|
$this->info('View created successfully.'); |
104
|
|
|
} else { |
105
|
|
|
$this->error('View already exists!'); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|