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 content($extends, $sections, $stacks, $components): string |
69
|
|
|
{ |
70
|
|
|
$content = ''; |
71
|
|
|
|
72
|
|
|
if ($extends) { |
73
|
|
|
$content .= "@extends('" . $extends . "')"; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
foreach ($sections as $section) { |
77
|
|
|
$content .= PHP_EOL . PHP_EOL . "@section('" . $section . "')" . PHP_EOL; |
78
|
|
|
$content .= "@endsection"; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
foreach ($components as $component) { |
82
|
|
|
$content .= PHP_EOL . PHP_EOL . "@component('" . $component . "')" . PHP_EOL; |
83
|
|
|
$content .= "@endcomponent"; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
foreach ($stacks as $stack) { |
87
|
|
|
$content .= PHP_EOL . PHP_EOL . "@push('" . $stack . "')" . PHP_EOL; |
88
|
|
|
$content .= "@endpush"; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $content; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
protected function create(string $path, string $content, bool $force) |
95
|
|
|
{ |
96
|
|
|
if ($force || !file_exists($path)) { |
97
|
|
|
\File::put($path, $content); |
98
|
|
|
$this->info('View created successfully.'); |
99
|
|
|
} else { |
100
|
|
|
$this->error('View already exists!'); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|