Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 399 | 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\GeneratorCommand;
6
use InvalidArgumentException;
7
use Symfony\Component\Console\Attribute\AsCommand;
399 lars 8
use Symfony\Component\Console\Input\InputInterface;
148 lars 9
use Symfony\Component\Console\Input\InputOption;
399 lars 10
use Symfony\Component\Console\Output\OutputInterface;
148 lars 11
 
12
#[AsCommand(name: 'make:observer')]
13
class ObserverMakeCommand extends GeneratorCommand
14
{
15
    /**
16
     * The console command name.
17
     *
18
     * @var string
19
     */
20
    protected $name = 'make:observer';
21
 
22
    /**
23
     * The name of the console command.
24
     *
25
     * This name is used to identify the command during lazy loading.
26
     *
27
     * @var string|null
28
     *
29
     * @deprecated
30
     */
31
    protected static $defaultName = 'make:observer';
32
 
33
    /**
34
     * The console command description.
35
     *
36
     * @var string
37
     */
38
    protected $description = 'Create a new observer class';
39
 
40
    /**
41
     * The type of class being generated.
42
     *
43
     * @var string
44
     */
45
    protected $type = 'Observer';
46
 
47
    /**
48
     * Build the class with the given name.
49
     *
50
     * @param  string  $name
51
     * @return string
52
     */
53
    protected function buildClass($name)
54
    {
55
        $stub = parent::buildClass($name);
56
 
57
        $model = $this->option('model');
58
 
59
        return $model ? $this->replaceModel($stub, $model) : $stub;
60
    }
61
 
62
    /**
63
     * Replace the model for the given stub.
64
     *
65
     * @param  string  $stub
66
     * @param  string  $model
67
     * @return string
68
     */
69
    protected function replaceModel($stub, $model)
70
    {
71
        $modelClass = $this->parseModel($model);
72
 
73
        $replace = [
74
            'DummyFullModelClass' => $modelClass,
75
            '{{ namespacedModel }}' => $modelClass,
76
            '{{namespacedModel}}' => $modelClass,
77
            'DummyModelClass' => class_basename($modelClass),
78
            '{{ model }}' => class_basename($modelClass),
79
            '{{model}}' => class_basename($modelClass),
80
            'DummyModelVariable' => lcfirst(class_basename($modelClass)),
81
            '{{ modelVariable }}' => lcfirst(class_basename($modelClass)),
82
            '{{modelVariable}}' => lcfirst(class_basename($modelClass)),
83
        ];
84
 
85
        return str_replace(
86
            array_keys($replace), array_values($replace), $stub
87
        );
88
    }
89
 
90
    /**
91
     * Get the fully-qualified model class name.
92
     *
93
     * @param  string  $model
94
     * @return string
95
     *
96
     * @throws \InvalidArgumentException
97
     */
98
    protected function parseModel($model)
99
    {
100
        if (preg_match('([^A-Za-z0-9_/\\\\])', $model)) {
101
            throw new InvalidArgumentException('Model name contains invalid characters.');
102
        }
103
 
104
        return $this->qualifyModel($model);
105
    }
106
 
107
    /**
108
     * Get the stub file for the generator.
109
     *
110
     * @return string
111
     */
112
    protected function getStub()
113
    {
114
        return $this->option('model')
115
            ? $this->resolveStubPath('/stubs/observer.stub')
116
            : $this->resolveStubPath('/stubs/observer.plain.stub');
117
    }
118
 
119
    /**
120
     * Resolve the fully-qualified path to the stub.
121
     *
122
     * @param  string  $stub
123
     * @return string
124
     */
125
    protected function resolveStubPath($stub)
126
    {
127
        return file_exists($customPath = $this->laravel->basePath(trim($stub, '/')))
128
            ? $customPath
129
            : __DIR__.$stub;
130
    }
131
 
132
    /**
133
     * Get the default namespace for the class.
134
     *
135
     * @param  string  $rootNamespace
136
     * @return string
137
     */
138
    protected function getDefaultNamespace($rootNamespace)
139
    {
140
        return $rootNamespace.'\Observers';
141
    }
142
 
143
    /**
144
     * Get the console command arguments.
145
     *
146
     * @return array
147
     */
148
    protected function getOptions()
149
    {
150
        return [
151
            ['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the observer already exists'],
152
            ['model', 'm', InputOption::VALUE_OPTIONAL, 'The model that the observer applies to'],
153
        ];
154
    }
399 lars 155
 
156
    /**
157
     * Interact further with the user if they were prompted for missing arguments.
158
     *
159
     * @param  \Symfony\Component\Console\Input\InputInterface  $input
160
     * @param  \Symfony\Component\Console\Output\OutputInterface  $output
161
     * @return void
162
     */
163
    protected function afterPromptingForMissingArguments(InputInterface $input, OutputInterface $output)
164
    {
621 lars 165
        if ($this->isReservedName($this->getNameInput()) || $this->didReceiveOptions($input)) {
399 lars 166
            return;
167
        }
168
 
169
        $model = $this->components->askWithCompletion(
170
            'What model should this observer apply to?',
171
            $this->possibleModels(),
172
            'none'
173
        );
174
 
175
        if ($model && $model !== 'none') {
176
            $input->setOption('model', $model);
177
        }
178
    }
148 lars 179
}