Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | 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\Concerns\CreatesMatchingTest;
6
use Illuminate\Console\GeneratorCommand;
7
use Illuminate\Support\Str;
8
use Symfony\Component\Console\Attribute\AsCommand;
9
use Symfony\Component\Console\Input\InputOption;
10
 
11
#[AsCommand(name: 'make:mail')]
12
class MailMakeCommand extends GeneratorCommand
13
{
14
    use CreatesMatchingTest;
15
 
16
    /**
17
     * The console command name.
18
     *
19
     * @var string
20
     */
21
    protected $name = 'make:mail';
22
 
23
    /**
24
     * The name of the console command.
25
     *
26
     * This name is used to identify the command during lazy loading.
27
     *
28
     * @var string|null
29
     *
30
     * @deprecated
31
     */
32
    protected static $defaultName = 'make:mail';
33
 
34
    /**
35
     * The console command description.
36
     *
37
     * @var string
38
     */
39
    protected $description = 'Create a new email class';
40
 
41
    /**
42
     * The type of class being generated.
43
     *
44
     * @var string
45
     */
399 lars 46
    protected $type = 'Mailable';
148 lars 47
 
48
    /**
49
     * Execute the console command.
50
     *
51
     * @return void
52
     */
53
    public function handle()
54
    {
55
        if (parent::handle() === false && ! $this->option('force')) {
56
            return;
57
        }
58
 
59
        if ($this->option('markdown') !== false) {
60
            $this->writeMarkdownTemplate();
61
        }
62
    }
63
 
64
    /**
65
     * Write the Markdown template for the mailable.
66
     *
67
     * @return void
68
     */
69
    protected function writeMarkdownTemplate()
70
    {
71
        $path = $this->viewPath(
72
            str_replace('.', '/', $this->getView()).'.blade.php'
73
        );
74
 
75
        if (! $this->files->isDirectory(dirname($path))) {
76
            $this->files->makeDirectory(dirname($path), 0755, true);
77
        }
78
 
79
        $this->files->put($path, file_get_contents(__DIR__.'/stubs/markdown.stub'));
80
    }
81
 
82
    /**
83
     * Build the class with the given name.
84
     *
85
     * @param  string  $name
86
     * @return string
87
     */
88
    protected function buildClass($name)
89
    {
90
        $class = str_replace(
91
            '{{ subject }}',
92
            Str::headline(str_replace($this->getNamespace($name).'\\', '', $name)),
93
            parent::buildClass($name)
94
        );
95
 
96
        if ($this->option('markdown') !== false) {
97
            $class = str_replace(['DummyView', '{{ view }}'], $this->getView(), $class);
98
        }
99
 
100
        return $class;
101
    }
102
 
103
    /**
104
     * Get the view name.
105
     *
106
     * @return string
107
     */
108
    protected function getView()
109
    {
110
        $view = $this->option('markdown');
111
 
112
        if (! $view) {
113
            $name = str_replace('\\', '/', $this->argument('name'));
114
 
115
            $view = 'mail.'.collect(explode('/', $name))
116
                ->map(fn ($part) => Str::kebab($part))
117
                ->implode('.');
118
        }
119
 
120
        return $view;
121
    }
122
 
123
    /**
124
     * Get the stub file for the generator.
125
     *
126
     * @return string
127
     */
128
    protected function getStub()
129
    {
130
        return $this->resolveStubPath(
131
            $this->option('markdown') !== false
132
                ? '/stubs/markdown-mail.stub'
133
                : '/stubs/mail.stub');
134
    }
135
 
136
    /**
137
     * Resolve the fully-qualified path to the stub.
138
     *
139
     * @param  string  $stub
140
     * @return string
141
     */
142
    protected function resolveStubPath($stub)
143
    {
144
        return file_exists($customPath = $this->laravel->basePath(trim($stub, '/')))
145
            ? $customPath
146
            : __DIR__.$stub;
147
    }
148
 
149
    /**
150
     * Get the default namespace for the class.
151
     *
152
     * @param  string  $rootNamespace
153
     * @return string
154
     */
155
    protected function getDefaultNamespace($rootNamespace)
156
    {
157
        return $rootNamespace.'\Mail';
158
    }
159
 
160
    /**
161
     * Get the console command options.
162
     *
163
     * @return array
164
     */
165
    protected function getOptions()
166
    {
167
        return [
168
            ['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the mailable already exists'],
169
            ['markdown', 'm', InputOption::VALUE_OPTIONAL, 'Create a new Markdown template for the mailable', false],
170
        ];
171
    }
172
}