Subversion-Projekte lars-tiefland.laravel_shop

Revision

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