Subversion-Projekte lars-tiefland.laravel_shop

Revision

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