Completed
Push — master ( c18692...2bec79 )
by Denis
02:51
created

MakeViewCommand::path()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
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 blocks($key, $values): string {
77
        $content = '';
78
79
        foreach ($values as $item) {
80
            $content .= $this->block($item, $key);
81
        }
82
83
        return $content;
84
    }
85
86
    protected function content(string $extends, array $sections, array $stacks, array $components): string
87
    {
88
        $content = '';
89
90
        if ($extends) {
91
            $content .= "@extends('" . $extends . "')";
92
        }
93
94
        $types = [
95
            'section' => $sections,
96
            'component' => $components,
97
            'push' => $stacks,
98
        ];
99
100
        foreach ($types as $key => $values) {
101
            $content .= $this->blocks($key, $values);
102
        }
103
104
        return $content;
105
    }
106
107
    protected function create(string $path, string $content, bool $force)
108
    {
109
        if ($force || !file_exists($path)) {
110
            \File::put($path, $content);
111
            $this->info('View created successfully.');
112
        } else {
113
            $this->error('View already exists!');
114
        }
115
    }
116
}
117