Completed
Pull Request — master (#1150)
by
unknown
01:58
created

LivewireCommand::viewContents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Nwidart\Modules\Commands;
4
5
use Illuminate\Support\Str;
6
use Illuminate\Console\Command;
7
use Illuminate\Support\Facades\File;
8
use Nwidart\Modules\Support\Stub;
9
use Nwidart\Modules\Traits\ModuleCommandTrait;
10
use Nwidart\Modules\Support\Config\GenerateConfigReader;
11
12
class LivewireCommand extends Command
13
{
14
    use ModuleCommandTrait;
15
16
    public $component;
17
18
    public $module;
19
20
    public $directories;
21
22
23
    protected $signature = 'module:make-livewire {module} {name} {--view=} {--force} {--inline}';
24
25
    /**
26
     * The console command description.
27
     *
28
     * @var string
29
     */
30
    protected $description = 'Generate Livewire Component.';
31
32
    /**
33
     * Execute the console command.
34
     *
35
     * @return int
36
     */
37
    public function handle()
38
    {
39
        if (! class_exists('Livewire')) {
40
            $this->line("\n<options=bold,reverse;fg=red> WHOOPS! </> 😳 \n");
41
            $this->line("<fg=red;options=bold>Livewire not found!</> \n");
42
            $this->line("<fg=green;options=bold>Install The Livewire Package - composer require livewire/livewire</> \n");
43
            return 0;
44
        }
45
46
        $this->component = $this->getComponent();
47
48
        if ($this->isReservedClassName($name = $this->component->class->name)) {
49
            $this->line("\n<options=bold,reverse;fg=red> WHOOPS! </> 😳 \n");
50
            $this->line("<fg=red;options=bold>Class is reserved:</> {$name} \n");
51
            return 0;
52
        }
53
54
        $force = $this->option('force');
55
        $inline = $this->option('inline');
56
57
        $class = $this->createClass($force, $inline);
58
        $view = $this->createView($force, $inline);
59
60
        if ($class || $view) {
61
            $this->line("\n <options=bold,reverse;fg=green> COMPONENT CREATED </> 🤙\n");
62
            $class && $this->line("<options=bold;fg=green>CLASS:</> {$this->component->class->path}");
63
64
            if (! $inline) {
65
                $view && $this->line("<options=bold;fg=green>VIEW:</>  {$this->component->view->path}");
66
            }
67
        }
68
69
        return 0;
70
    }
71
72
    protected function getComponent()
73
    {
74
        $module = $this->laravel['modules']->findOrFail($this->getModuleName());
75
        
76
        $this->module = $module;
77
        
78
        $this->directories = preg_split('/[.\/(\\\\)]+/', $this->argument('name'));
79
        
80
        $classInfo = $this->getClassInfo();
81
        
82
        $viewInfo = $this->getViewInfo();
83
84
        return (object) [
85
            'class' => $classInfo,
86
            'view' => $viewInfo
87
        ];
88
    }
89
90
    public function getClassInfo()
91
    {
92
        $modulePath = $this->module->getPath().'/';
93
94
        $defaultNamespace = GenerateConfigReader::read('livewire-namespace')->getPath();
95
96
        $classDir = $modulePath . strtr($defaultNamespace, ['\\' => '/']);
97
98
        $path = collect($this->directories)
99
            ->map([\Str::class, 'studly'])
100
            ->implode('/');
101
102
        $beforeLast = (\Str::contains($path, '/')) ? '/' . \Str::beforeLast($path, '/') : '';
103
104
        $namespace = config('modules.namespace') . '\\' . $this->module->getName() . '\\' . $defaultNamespace . strtr($beforeLast, ['/' => '\\']);
105
106
        $name = \Str::studly( \Arr::last($this->directories) );
107
108
        return (object) [
109
            'dir' => $classDir,
110
            'path' => $path,
111
            'file' => $classDir . '/' . $path . '.php',
112
            'namespace' => $namespace,
113
            'name' => $name,
114
        ];
115
    }
116
117
    public function getViewInfo()
118
    {
119
        $modulePath = $this->module->getPath().'/';
120
121
        $viewDir = $modulePath . GenerateConfigReader::read('livewire-view')->getPath();
122
123
        $path = collect($this->directories)
124
            ->map([\Str::class, 'kebab'])
125
            ->implode('/');
126
127
        if ($this->option('view')) $path = strtr($this->option('view'), ['.' => '/']);
128
        
129
        $name = strtr($path, ['/' => '.']);
130
131
        return (object) [
132
            'dir' => $viewDir,
133
            'path' => $path,
134
            'folder' => \Str::after($viewDir, 'views/'),
135
            'file' => $viewDir . '/' . $path . '.blade.php',
136
            'name' => $name,
137
        ];
138
    }
139
140 View Code Duplication
    protected function createClass($force = false, $inline = false)
141
    {
142
        $classPath = $this->component->class->file;
143
144
        if (File::exists($classPath) && ! $force) {
145
            $this->line("<options=bold,reverse;fg=red> WHOOPS </> 😳 \n");
146
            $this->line("<fg=red;options=bold>Class already exists:</> {$this->component->class->path}");
147
            return false;
148
        }
149
150
        $this->ensureDirectoryExists($classPath);
151
152
        File::put($classPath, $this->classContents($inline));
153
154
        return $classPath;
155
    }
156
157
    public function classContents($inline = false)
158
    {
159
        $stubName = $inline ? '/livewire/livewire.inline.stub' : '/livewire/livewire.stub';
160
161
        return (new Stub($stubName, [
162
            'NAMESPACE'  => $this->component->class->namespace,
163
            'CLASS'      => $this->component->class->name,
164
            'LOWER_NAME' => $this->module->getLowerName(),
165
            'VIEW_NAME'  => $this->component->view->folder . '.' . $this->component->view->name,
166
        ]))->render();
167
    }
168
169 View Code Duplication
    protected function createView($force = false, $inline = false)
170
    {
171
        if ($inline) return false;
172
173
        $viewPath = $this->component->view->file;
174
175
        if (File::exists($viewPath) && ! $force) {
176
            $this->line("<fg=red;options=bold>View already exists:</> {$this->component->view->path}");
177
            return false;
178
        }
179
180
        $this->ensureDirectoryExists($viewPath);
181
182
        File::put($viewPath, $this->viewContents());
183
184
        return $viewPath;
185
    }
186
187
    public function viewContents($inline = false)
0 ignored issues
show
Unused Code introduced by
The parameter $inline is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
188
    {
189
        return (new Stub('/livewire/livewire.view.stub'))->render();
190
    }
191
192
    protected function ensureDirectoryExists($path)
193
    {
194
        if (! File::isDirectory(dirname($path))) {
195
            File::makeDirectory(dirname($path), 0777, $recursive = true, $force = true);
196
        }
197
    }
198
199
    public function isReservedClassName($name)
200
    {
201
        return array_search($name, ['Parent', 'Component', 'Interface']) !== false;
202
    }
203
}
204