• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

orchestral / canvas / 9563332959

18 Jun 2024 10:23AM UTC coverage: 94.815%. Remained the same
9563332959

push

github

crynobone
wip

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

640 of 675 relevant lines covered (94.81%)

24.78 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

74.58
/src/Console/ModelMakeCommand.php
1
<?php
2

3
namespace Orchestra\Canvas\Console;
4

5
use Illuminate\Filesystem\Filesystem;
6
use Illuminate\Support\Str;
7
use Orchestra\Canvas\Core\Concerns\CodeGenerator;
8
use Orchestra\Canvas\Core\Concerns\TestGenerator;
9
use Orchestra\Canvas\Core\Concerns\UsesGeneratorOverrides;
10
use Symfony\Component\Console\Attribute\AsCommand;
11

12
/**
13
 * @see https://github.com/laravel/framework/blob/10.x/src/Illuminate/Foundation/Console/ModelMakeCommand.php
14
 */
15
#[AsCommand(name: 'make:model', description: 'Create a new Eloquent model class')]
16
class ModelMakeCommand extends \Illuminate\Foundation\Console\ModelMakeCommand
17
{
18
    use CodeGenerator;
19
    use TestGenerator;
20
    use UsesGeneratorOverrides;
21

22
    /**
23
     * Create a new creator command instance.
24
     *
25
     * @return void
26
     */
27
    public function __construct(Filesystem $files)
28
    {
29
        parent::__construct($files);
9✔
30

31
        $this->addGeneratorPresetOptions();
9✔
32
    }
33

34
    /**
35
     * Execute the console command.
36
     *
37
     * @return bool|null
38
     *
39
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
40
     */
41
    #[\Override]
42
    public function handle()
43
    {
44
        /** @phpstan-ignore return.type */
45
        return $this->generateCode() ? self::SUCCESS : self::FAILURE;
9✔
46
    }
47

48
    /**
49
     * Run after code successfully generated.
50
     */
51
    protected function afterCodeHasBeenGenerated(): void
52
    {
53
        if ($this->option('all')) {
9✔
54
            $this->input->setOption('factory', true);
×
55
            $this->input->setOption('seed', true);
×
56
            $this->input->setOption('migration', true);
×
57
            $this->input->setOption('controller', true);
×
58
            $this->input->setOption('policy', true);
×
59
            $this->input->setOption('resource', true);
×
60
        }
61

62
        if ($this->option('factory')) {
9✔
63
            $this->createFactory();
1✔
64
        }
65

66
        if ($this->option('migration')) {
9✔
67
            $this->createMigration();
1✔
68
        }
69

70
        if ($this->option('seed')) {
9✔
71
            $this->createSeeder();
1✔
72
        }
73

74
        if ($this->option('controller') || $this->option('resource') || $this->option('api')) {
9✔
75
            $this->createController();
2✔
76
        }
77

78
        if ($this->option('policy')) {
9✔
79
            $this->createPolicy();
×
80
        }
81
    }
82

83
    /**
84
     * Create a model factory for the model.
85
     *
86
     * @return void
87
     */
88
    #[\Override]
89
    protected function createFactory()
90
    {
91
        $factory = Str::studly($this->getNameInput());
1✔
92

93
        $this->call('make:factory', [
1✔
94
            'name' => "{$factory}Factory",
1✔
95
            '--model' => $this->qualifyClass($this->getNameInput()),
1✔
96
            '--preset' => $this->option('preset'),
1✔
97
        ]);
1✔
98
    }
99

100
    /**
101
     * Create a migration file for the model.
102
     *
103
     * @return void
104
     */
105
    #[\Override]
106
    protected function createMigration()
107
    {
108
        $table = Str::snake(Str::pluralStudly(class_basename($this->getNameInput())));
1✔
109

110
        if ($this->option('pivot')) {
1✔
111
            $table = Str::singular($table);
×
112
        }
113

114
        $this->call('make:migration', [
1✔
115
            'name' => "create_{$table}_table",
1✔
116
            '--create' => $table,
1✔
117
            '--fullpath' => true,
1✔
118
            '--preset' => $this->option('preset'),
1✔
119
        ]);
1✔
120
    }
121

122
    /**
123
     * Create a seeder file for the model.
124
     *
125
     * @return void
126
     */
127
    #[\Override]
128
    protected function createSeeder()
129
    {
130
        $seeder = Str::studly(class_basename($this->getNameInput()));
1✔
131

132
        $this->call('make:seeder', [
1✔
133
            'name' => "{$seeder}Seeder",
1✔
134
            '--preset' => $this->option('preset'),
1✔
135
        ]);
1✔
136
    }
137

138
    /**
139
     * Create a controller for the model.
140
     *
141
     * @return void
142
     */
143
    #[\Override]
144
    protected function createController()
145
    {
146
        $controller = Str::studly(class_basename($this->getNameInput()));
2✔
147

148
        $modelName = $this->qualifyClass($this->getNameInput());
2✔
149

150
        $this->call('make:controller', array_filter([
2✔
151
            'name' => "{$controller}Controller",
2✔
152
            '--model' => $this->option('resource') || $this->option('api') ? $modelName : null,
2✔
153
            '--api' => $this->option('api'),
2✔
154
            '--requests' => $this->option('requests') || $this->option('all'),
2✔
155
            '--preset' => $this->option('preset'),
2✔
156
        ]));
2✔
157
    }
158

159
    /**
160
     * Create a policy file for the model.
161
     *
162
     * @return void
163
     */
164
    #[\Override]
165
    protected function createPolicy()
166
    {
167
        $policy = Str::studly(class_basename($this->getNameInput()));
×
168

169
        $this->call('make:policy', [
×
170
            'name' => "{$policy}Policy",
×
171
            '--model' => $this->qualifyClass($this->getNameInput()),
×
172
            '--preset' => $this->option('preset'),
×
173
        ]);
×
174
    }
175

176
    /**
177
     * Get the default namespace for the class.
178
     *
179
     * @param  string  $rootNamespace
180
     * @return string
181
     */
182
    #[\Override]
183
    protected function getDefaultNamespace($rootNamespace)
184
    {
185
        return rtrim($this->generatorPreset()->modelNamespace(), '\\');
9✔
186
    }
187

188
    /**
189
     * Qualify the given model class base name.
190
     *
191
     * @param  string  $model
192
     * @return string
193
     */
194
    #[\Override]
195
    protected function qualifyModel(string $model)
196
    {
197
        return $this->qualifyModelUsingCanvas($model);
×
198
    }
199

200
    /**
201
     * Get the destination class path.
202
     *
203
     * @param  string  $name
204
     * @return string
205
     */
206
    #[\Override]
207
    protected function getPath($name)
208
    {
209
        return $this->getPathUsingCanvas($name);
9✔
210
    }
211

212
    /**
213
     * Get the root namespace for the class.
214
     *
215
     * @return string
216
     */
217
    #[\Override]
218
    protected function rootNamespace()
219
    {
220
        return $this->rootNamespaceUsingCanvas();
9✔
221
    }
222
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc