-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathControlMakeCommand.php
More file actions
191 lines (167 loc) · 5.03 KB
/
ControlMakeCommand.php
File metadata and controls
191 lines (167 loc) · 5.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
namespace Lomkit\Access\Console;
use Illuminate\Console\GeneratorCommand;
use Illuminate\Support\Collection;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Finder\Finder;
use function Laravel\Prompts\multiselect;
#[AsCommand(name: 'make:control')]
class ControlMakeCommand extends GeneratorCommand
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'make:control';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new control class';
/**
* The type of class being generated.
*
* @var string
*/
protected $type = 'Control';
/**
* Get the stub file for the generator.
*
* @return string
*/
protected function getStub()
{
return $this->resolveStubPath('/stubs/control.stub');
}
/**
* Resolve the fully-qualified path to the stub.
*
* @param string $stub
*
* @return string
*/
protected function resolveStubPath($stub)
{
return file_exists($customPath = $this->laravel->basePath(trim($stub, '/')))
? $customPath
: __DIR__.$stub;
}
/**
* Get the default namespace for the class.
*
* @param string $rootNamespace
*
* @return string
*/
protected function getDefaultNamespace($rootNamespace)
{
return $rootNamespace.'\Access\Controls';
}
/**
* Build the class with the given name.
*
* @param string $name
*
* @return string
*/
protected function buildClass($name)
{
$rootNamespace = $this->rootNamespace();
$controlNamespace = $this->getNamespace($name);
$replace = [];
$baseControlExists = file_exists($this->getPath("{$rootNamespace}Access\Controls\Control"));
$replace = $this->buildPerimetersReplacements($replace, $this->option('perimeters'));
if ($baseControlExists) {
$replace['use Lomkit\Access\Controls\Control;'] = '';
} else {
$replace["use {$controlNamespace}\Control;\n"] = '';
}
return str_replace(
array_keys($replace),
array_values($replace),
parent::buildClass($name)
);
}
/**
* Build the model replacement values.
*
* @param array $replace
* @param array $perimeters
*
* @return array
*/
protected function buildPerimetersReplacements(array $replace, array $perimeters)
{
$perimetersImplementation = '';
foreach ($perimeters as $perimeter) {
$perimeterClass = $this->rootNamespace().'Access\\Perimeters\\'.$perimeter;
$perimetersImplementation .= <<<PERIMETER
$perimeterClass::new()
->allowed(function (Model \$user, string \$method) {
return true;
})
->should(function (Model \$user, Model \$model) {
return true;
})
->query(function (Builder \$query, Model \$user) {
return \$query;
}),
PERIMETER;
}
return array_merge($replace, [
'{{ perimeters }}' => $perimetersImplementation,
'{{perimeters}}' => $perimetersImplementation,
]);
}
/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return [
['perimeters', 'p', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'The perimeters that the control relies on'],
];
}
/**
* Interact further with the user if they were prompted for missing arguments.
*
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
*
* @return void
*/
protected function afterPromptingForMissingArguments(InputInterface $input, OutputInterface $output)
{
if ($this->didReceiveOptions($input)) {
return;
}
$perimeters = multiselect(
'What perimeters should this control apply to? (Optional)',
$this->possiblePerimeters(),
);
if ($perimeters) {
$input->setOption('perimeters', $perimeters);
}
}
/**
* Get a list of possible model names.
*
* @return array<int, string>
*/
protected function possiblePerimeters()
{
$perimetersPath = is_dir(app_path('Access/Perimeters')) ? app_path('Access/Perimeters') : app_path();
return (new Collection(Finder::create()->files()->depth(0)->in($perimetersPath)))
->map(fn ($file) => $file->getBasename('.php'))
->sort()
->values()
->all();
}
}