Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Foundation\Console;
4
 
5
use Illuminate\Console\GeneratorCommand;
6
use Illuminate\Foundation\Inspiring;
7
use Illuminate\Support\Str;
8
use Symfony\Component\Console\Attribute\AsCommand;
9
use Symfony\Component\Console\Input\InputOption;
10
 
11
#[AsCommand(name: 'make:component')]
12
class ComponentMakeCommand extends GeneratorCommand
13
{
14
    /**
15
     * The console command name.
16
     *
17
     * @var string
18
     */
19
    protected $name = 'make:component';
20
 
21
    /**
22
     * The name of the console command.
23
     *
24
     * This name is used to identify the command during lazy loading.
25
     *
26
     * @var string|null
27
     *
28
     * @deprecated
29
     */
30
    protected static $defaultName = 'make:component';
31
 
32
    /**
33
     * The console command description.
34
     *
35
     * @var string
36
     */
37
    protected $description = 'Create a new view component class';
38
 
39
    /**
40
     * The type of class being generated.
41
     *
42
     * @var string
43
     */
44
    protected $type = 'Component';
45
 
46
    /**
47
     * Execute the console command.
48
     *
49
     * @return void
50
     */
51
    public function handle()
52
    {
53
        if ($this->option('view')) {
54
            $this->writeView(function () {
55
                $this->components->info($this->type.' created successfully.');
56
            });
57
 
58
            return;
59
        }
60
 
61
        if (parent::handle() === false && ! $this->option('force')) {
62
            return false;
63
        }
64
 
65
        if (! $this->option('inline')) {
66
            $this->writeView();
67
        }
68
    }
69
 
70
    /**
71
     * Write the view for the component.
72
     *
73
     * @param  callable|null  $onSuccess
74
     * @return void
75
     */
76
    protected function writeView($onSuccess = null)
77
    {
78
        $path = $this->viewPath(
79
            str_replace('.', '/', 'components.'.$this->getView()).'.blade.php'
80
        );
81
 
82
        if (! $this->files->isDirectory(dirname($path))) {
83
            $this->files->makeDirectory(dirname($path), 0777, true, true);
84
        }
85
 
86
        if ($this->files->exists($path) && ! $this->option('force')) {
87
            $this->components->error('View already exists.');
88
 
89
            return;
90
        }
91
 
92
        file_put_contents(
93
            $path,
94
            '<div>
95
    <!-- '.Inspiring::quotes()->random().' -->
96
</div>'
97
        );
98
 
99
        if ($onSuccess) {
100
            $onSuccess();
101
        }
102
    }
103
 
104
    /**
105
     * Build the class with the given name.
106
     *
107
     * @param  string  $name
108
     * @return string
109
     */
110
    protected function buildClass($name)
111
    {
112
        if ($this->option('inline')) {
113
            return str_replace(
114
                ['DummyView', '{{ view }}'],
115
                "<<<'blade'\n<div>\n    <!-- ".Inspiring::quotes()->random()." -->\n</div>\nblade",
116
                parent::buildClass($name)
117
            );
118
        }
119
 
120
        return str_replace(
121
            ['DummyView', '{{ view }}'],
122
            'view(\'components.'.$this->getView().'\')',
123
            parent::buildClass($name)
124
        );
125
    }
126
 
127
    /**
128
     * Get the view name relative to the components directory.
129
     *
130
     * @return string view
131
     */
132
    protected function getView()
133
    {
134
        $name = str_replace('\\', '/', $this->argument('name'));
135
 
136
        return collect(explode('/', $name))
137
            ->map(function ($part) {
138
                return Str::kebab($part);
139
            })
140
            ->implode('.');
141
    }
142
 
143
    /**
144
     * Get the stub file for the generator.
145
     *
146
     * @return string
147
     */
148
    protected function getStub()
149
    {
150
        return $this->resolveStubPath('/stubs/view-component.stub');
151
    }
152
 
153
    /**
154
     * Resolve the fully-qualified path to the stub.
155
     *
156
     * @param  string  $stub
157
     * @return string
158
     */
159
    protected function resolveStubPath($stub)
160
    {
161
        return file_exists($customPath = $this->laravel->basePath(trim($stub, '/')))
162
                        ? $customPath
163
                        : __DIR__.$stub;
164
    }
165
 
166
    /**
167
     * Get the default namespace for the class.
168
     *
169
     * @param  string  $rootNamespace
170
     * @return string
171
     */
172
    protected function getDefaultNamespace($rootNamespace)
173
    {
174
        return $rootNamespace.'\View\Components';
175
    }
176
 
177
    /**
178
     * Get the console command options.
179
     *
180
     * @return array
181
     */
182
    protected function getOptions()
183
    {
184
        return [
185
            ['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the component already exists'],
186
            ['inline', null, InputOption::VALUE_NONE, 'Create a component that renders an inline view'],
187
            ['view', null, InputOption::VALUE_NONE, 'Create an anonymous component with only a view'],
188
        ];
189
    }
190
}