Issues (27)

src/Console/Commands/Fastapi.php (5 issues)

Labels
Severity
1
<?php
2
3
namespace Larafast\Fastapi\Console\Commands;
4
5
use Illuminate\Support\Str;
6
use Larafast\Fastapi\Console\Contracts\GeneratorCommand;
7
8
class Fastapi extends GeneratorCommand
9
{
10
11
    /**
12
     * The console command name.
13
     *
14
     * @var string
15
     */
16
    protected $name = 'fastApi';
17
    /**
18
     * The name and signature of the console command.
19
     *
20
     * @var string
21
     */
22
    protected $signature = 'fastApi
23
    {name : Model name. Controller, factory, migration, views will be based on this name.}
24
    {--views-dir= : Place views in a sub-directory under the views directory. It can be any nested directory structure}
25
    {--controller-dir= : Place controller in a sub-directory under the Http/Controllers directory. It can be any nested directory structure}
26
    {--stubs-dir= : Specify a custom stubs directory}
27
    {--no-views : Do not create view files for the model}
28
    {--no-migration : Do not create a migration for the model}
29
    {--no-factory : Do not create a factory for the model}
30
    ';
31
32
    /**
33
     * The console command description.
34
     *
35
     * @var string
36
     */
37
    protected $description = 'Scaffold a nearly complete boilerplate CRUD pages for the specified Model';
38
39
    /**
40
     * Create a new command instance.
41
     *
42
     */
43
    /*    public function __construct()
44
        {
45
            parent::__construct();
46
        }*/
47
48
    /**
49
     * Execute the console command.
50
     *
51
     * @return mixed
52
     */
53
    public function handle()
54
    {
55
        /**
56
         * Steps
57
         * - Generate Model
58
         * - Generate Factory
59
         * - Generate Migration
60
         * - Generate Controller
61
         * - Generate Requests
62
         * - Generate Views
63
         *
64
         */
65
        $this->type = 'Model';
66
67
        if (!$this->option('no-factory')) {
68
            $this->createFactory();
69
        }
70
71
        if (!$this->option('no-migration')) {
72
            $this->createMigration();
73
        }
74
        $this->createController();
75
76
        $this->type = 'Request';
77
78
        $this->createRequest('Store');
79
80
        $this->createRequest('Update');
81
82
        $this->createResource();
83
    }
84
85
    /**
86
     * Create a model factory for the model.
87
     *
88
     * @return void
89
     */
90
    protected function createFactory()
91
    {
92
        $factory = Str::studly(class_basename($this->argument('name')));
0 ignored issues
show
It seems like $this->argument('name') can also be of type string[]; however, parameter $class of class_basename() does only seem to accept object|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

92
        $factory = Str::studly(class_basename(/** @scrutinizer ignore-type */ $this->argument('name')));
Loading history...
93
94
        $this->call('fastApi:factory', [
95
            'name' => "{$factory}Factory",
96
            '--model' => $this->argument('name'),
97
        ]);
98
    }
99
100
    /**
101
     * Create a migration file for the model.
102
     *
103
     * @return void
104
     */
105
    protected function createMigration()
106
    {
107
        $table = Str::plural(Str::snake(class_basename($this->argument('name'))));
0 ignored issues
show
It seems like $this->argument('name') can also be of type string[]; however, parameter $class of class_basename() does only seem to accept object|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

107
        $table = Str::plural(Str::snake(class_basename(/** @scrutinizer ignore-type */ $this->argument('name'))));
Loading history...
108
109
        $this->call('fastApi:migration', [
110
            'name' => "create_{$table}_table",
111
            '--create' => $table,
112
        ]);
113
    }
114
115
    /**
116
     * Create a controller for the model.
117
     *
118
     * @return void
119
     */
120
    protected function createController()
121
    {
122
123
        $controller = Str::studly(class_basename($this->argument('name')));
0 ignored issues
show
It seems like $this->argument('name') can also be of type string[]; however, parameter $class of class_basename() does only seem to accept object|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

123
        $controller = Str::studly(class_basename(/** @scrutinizer ignore-type */ $this->argument('name')));
Loading history...
124
125
        $modelName = $this->qualifyClass($this->getNameInput());
126
127
        $args = [
128
            'name' => "{$controller}Controller",
129
            '--model' => $modelName,
130
        ];
131
132
        $viewsDir = $this->option('views-dir');
133
        if ($viewsDir) {
134
            $args['--views-dir'] = $viewsDir;
135
        }
136
137
        $controllerDir = $this->option('controller-dir');
138
        if ($controllerDir) {
139
            $args['--controller-dir'] = $controllerDir;
140
        }
141
142
        $this->call('fastApi:controller', $args);
143
    }
144
145
146
    /**
147
     * Create a controller for the model.
148
     *
149
     * @param string $requestType
150
     *
151
     * @return void
152
     */
153
    protected function createRequest($requestType = 'Store')
154
    {
155
        $model = Str::studly(class_basename($this->argument('name')));
0 ignored issues
show
It seems like $this->argument('name') can also be of type string[]; however, parameter $class of class_basename() does only seem to accept object|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

155
        $model = Str::studly(class_basename(/** @scrutinizer ignore-type */ $this->argument('name')));
Loading history...
156
        $name = "{$model}{$requestType}Request";
157
        $this->call('fastApi:request', [
158
            'name' => $name,
159
            '--model' => $model,
160
            '--type' => Str::slug($requestType),
161
        ]);
162
    }
163
164
    /**
165
     * Get the stub file for the generator.
166
     *
167
     * @return string
168
     */
169
    protected function getStub()
170
    {
171
        return config('fastApi.stubs_dir') . '/' . Str::slug($this->type) . '.stub';
172
    }
173
174
    /**
175
     * Get the default namespace for the class.
176
     *
177
     * @param string $rootNamespace
178
     *
179
     * @return string
180
     */
181
    protected function getDefaultNamespace($rootNamespace)
182
    {
183
        switch ($this->type) {
184
            case 'Request':
185
                return $rootNamespace . '\Http\Requests';
186
        }
187
188
        return $rootNamespace;
189
    }
190
191
192
    /**
193
     * Create a model resource for the model.
194
     *
195
     * @return void
196
     */
197
    protected function createResource()
198
    {
199
        $resource = Str::studly(class_basename($this->argument('name')));
0 ignored issues
show
It seems like $this->argument('name') can also be of type string[]; however, parameter $class of class_basename() does only seem to accept object|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

199
        $resource = Str::studly(class_basename(/** @scrutinizer ignore-type */ $this->argument('name')));
Loading history...
200
201
        $this->call('fastApi:resource', [
202
            'name' => "{$resource}Resource",
203
            '--model' => $this->argument('name'),
204
        ]);
205
    }
206
}
207