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 Illuminate\Support\Str;
7
use LogicException;
8
use Symfony\Component\Console\Attribute\AsCommand;
9
use Symfony\Component\Console\Input\InputOption;
10
 
11
#[AsCommand(name: 'make:policy')]
12
class PolicyMakeCommand extends GeneratorCommand
13
{
14
    /**
15
     * The console command name.
16
     *
17
     * @var string
18
     */
19
    protected $name = 'make:policy';
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:policy';
31
 
32
    /**
33
     * The console command description.
34
     *
35
     * @var string
36
     */
37
    protected $description = 'Create a new policy class';
38
 
39
    /**
40
     * The type of class being generated.
41
     *
42
     * @var string
43
     */
44
    protected $type = 'Policy';
45
 
46
    /**
47
     * Build the class with the given name.
48
     *
49
     * @param  string  $name
50
     * @return string
51
     */
52
    protected function buildClass($name)
53
    {
54
        $stub = $this->replaceUserNamespace(
55
            parent::buildClass($name)
56
        );
57
 
58
        $model = $this->option('model');
59
 
60
        return $model ? $this->replaceModel($stub, $model) : $stub;
61
    }
62
 
63
    /**
64
     * Replace the User model namespace.
65
     *
66
     * @param  string  $stub
67
     * @return string
68
     */
69
    protected function replaceUserNamespace($stub)
70
    {
71
        $model = $this->userProviderModel();
72
 
73
        if (! $model) {
74
            return $stub;
75
        }
76
 
77
        return str_replace(
78
            $this->rootNamespace().'User',
79
            $model,
80
            $stub
81
        );
82
    }
83
 
84
    /**
85
     * Get the model for the guard's user provider.
86
     *
87
     * @return string|null
88
     *
89
     * @throws \LogicException
90
     */
91
    protected function userProviderModel()
92
    {
93
        $config = $this->laravel['config'];
94
 
95
        $guard = $this->option('guard') ?: $config->get('auth.defaults.guard');
96
 
97
        if (is_null($guardProvider = $config->get('auth.guards.'.$guard.'.provider'))) {
98
            throw new LogicException('The ['.$guard.'] guard is not defined in your "auth" configuration file.');
99
        }
100
 
101
        if (! $config->get('auth.providers.'.$guardProvider.'.model')) {
102
            return 'App\\Models\\User';
103
        }
104
 
105
        return $config->get(
106
            'auth.providers.'.$guardProvider.'.model'
107
        );
108
    }
109
 
110
    /**
111
     * Replace the model for the given stub.
112
     *
113
     * @param  string  $stub
114
     * @param  string  $model
115
     * @return string
116
     */
117
    protected function replaceModel($stub, $model)
118
    {
119
        $model = str_replace('/', '\\', $model);
120
 
121
        if (str_starts_with($model, '\\')) {
122
            $namespacedModel = trim($model, '\\');
123
        } else {
124
            $namespacedModel = $this->qualifyModel($model);
125
        }
126
 
127
        $model = class_basename(trim($model, '\\'));
128
 
129
        $dummyUser = class_basename($this->userProviderModel());
130
 
131
        $dummyModel = Str::camel($model) === 'user' ? 'model' : $model;
132
 
133
        $replace = [
134
            'NamespacedDummyModel' => $namespacedModel,
135
            '{{ namespacedModel }}' => $namespacedModel,
136
            '{{namespacedModel}}' => $namespacedModel,
137
            'DummyModel' => $model,
138
            '{{ model }}' => $model,
139
            '{{model}}' => $model,
140
            'dummyModel' => Str::camel($dummyModel),
141
            '{{ modelVariable }}' => Str::camel($dummyModel),
142
            '{{modelVariable}}' => Str::camel($dummyModel),
143
            'DummyUser' => $dummyUser,
144
            '{{ user }}' => $dummyUser,
145
            '{{user}}' => $dummyUser,
146
            '$user' => '$'.Str::camel($dummyUser),
147
        ];
148
 
149
        $stub = str_replace(
150
            array_keys($replace), array_values($replace), $stub
151
        );
152
 
153
        return preg_replace(
154
            vsprintf('/use %s;[\r\n]+use %s;/', [
155
                preg_quote($namespacedModel, '/'),
156
                preg_quote($namespacedModel, '/'),
157
            ]),
158
            "use {$namespacedModel};",
159
            $stub
160
        );
161
    }
162
 
163
    /**
164
     * Get the stub file for the generator.
165
     *
166
     * @return string
167
     */
168
    protected function getStub()
169
    {
170
        return $this->option('model')
171
                    ? $this->resolveStubPath('/stubs/policy.stub')
172
                    : $this->resolveStubPath('/stubs/policy.plain.stub');
173
    }
174
 
175
    /**
176
     * Resolve the fully-qualified path to the stub.
177
     *
178
     * @param  string  $stub
179
     * @return string
180
     */
181
    protected function resolveStubPath($stub)
182
    {
183
        return file_exists($customPath = $this->laravel->basePath(trim($stub, '/')))
184
                        ? $customPath
185
                        : __DIR__.$stub;
186
    }
187
 
188
    /**
189
     * Get the default namespace for the class.
190
     *
191
     * @param  string  $rootNamespace
192
     * @return string
193
     */
194
    protected function getDefaultNamespace($rootNamespace)
195
    {
196
        return $rootNamespace.'\Policies';
197
    }
198
 
199
    /**
200
     * Get the console command arguments.
201
     *
202
     * @return array
203
     */
204
    protected function getOptions()
205
    {
206
        return [
207
            ['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the policy already exists'],
208
            ['model', 'm', InputOption::VALUE_OPTIONAL, 'The model that the policy applies to'],
209
            ['guard', 'g', InputOption::VALUE_OPTIONAL, 'The guard that the policy relies on'],
210
        ];
211
    }
212
}