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

orchestral / canvas / 6224137920

18 Sep 2023 02:42PM UTC coverage: 90.984% (+1.0%) from 89.945%
6224137920

push

github

crynobone
wip

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

603 of 603 new or added lines in 34 files covered. (100.0%)

555 of 610 relevant lines covered (90.98%)

24.54 hits per line

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

74.14
/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/9.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 controller 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
    public function handle()
42
    {
43
        return $this->generateCode() ? self::SUCCESS : self::FAILURE;
9✔
44
    }
45

46
    protected function afterCodeHasBeenGenerated(): void
47
    {
48
        if ($this->option('all')) {
9✔
49
            $this->input->setOption('factory', true);
×
50
            $this->input->setOption('seed', true);
×
51
            $this->input->setOption('migration', true);
×
52
            $this->input->setOption('controller', true);
×
53
            $this->input->setOption('policy', true);
×
54
            $this->input->setOption('resource', true);
×
55
        }
56

57
        if ($this->option('factory')) {
9✔
58
            $this->createFactory();
1✔
59
        }
60

61
        if ($this->option('migration')) {
9✔
62
            $this->createMigration();
1✔
63
        }
64

65
        if ($this->option('seed')) {
9✔
66
            $this->createSeeder();
1✔
67
        }
68

69
        if ($this->option('controller') || $this->option('resource') || $this->option('api')) {
9✔
70
            $this->createController();
2✔
71
        }
72

73
        if ($this->option('policy')) {
9✔
74
            $this->createPolicy();
×
75
        }
76
    }
77

78
    /**
79
     * Create a model factory for the model.
80
     *
81
     * @return void
82
     */
83
    protected function createFactory()
84
    {
85
        $factory = Str::studly($this->getNameInput());
1✔
86

87
        $this->call('make:factory', [
1✔
88
            'name' => "{$factory}Factory",
1✔
89
            '--model' => $this->qualifyClass($this->getNameInput()),
1✔
90
        ]);
1✔
91
    }
92

93
    /**
94
     * Create a migration file for the model.
95
     *
96
     * @return void
97
     */
98
    protected function createMigration()
99
    {
100
        $table = Str::snake(Str::pluralStudly(class_basename($this->getNameInput())));
1✔
101

102
        if ($this->option('pivot')) {
1✔
103
            $table = Str::singular($table);
×
104
        }
105

106
        $this->call('make:migration', [
1✔
107
            'name' => "create_{$table}_table",
1✔
108
            '--create' => $table,
1✔
109
            '--fullpath' => true,
1✔
110
            '--preset' => $this->option('preset'),
1✔
111
        ]);
1✔
112
    }
113

114
    /**
115
     * Create a seeder file for the model.
116
     *
117
     * @return void
118
     */
119
    protected function createSeeder()
120
    {
121
        $seeder = Str::studly(class_basename($this->getNameInput()));
1✔
122

123
        $this->call('make:seeder', [
1✔
124
            'name' => "{$seeder}Seeder",
1✔
125
            '--preset' => $this->option('preset'),
1✔
126
        ]);
1✔
127
    }
128

129
    /**
130
     * Create a controller for the model.
131
     *
132
     * @return void
133
     */
134
    protected function createController()
135
    {
136
        $controller = Str::studly(class_basename($this->getNameInput()));
2✔
137

138
        $modelName = $this->qualifyClass($this->getNameInput());
2✔
139

140
        $this->call('make:controller', array_filter([
2✔
141
            'name' => "{$controller}Controller",
2✔
142
            '--model' => $this->option('resource') || $this->option('api') ? $modelName : null,
2✔
143
            '--api' => $this->option('api'),
2✔
144
            '--requests' => $this->option('requests') || $this->option('all'),
2✔
145
            '--preset' => $this->option('preset'),
2✔
146
        ]));
2✔
147
    }
148

149
    /**
150
     * Create a policy file for the model.
151
     *
152
     * @return void
153
     */
154
    protected function createPolicy()
155
    {
156
        $policy = Str::studly(class_basename($this->getNameInput()));
×
157

158
        $this->call('make:policy', [
×
159
            'name' => "{$policy}Policy",
×
160
            '--model' => $this->qualifyClass($this->getNameInput()),
×
161
            '--preset' => $this->option('preset'),
×
162
        ]);
×
163
    }
164

165
    /**
166
     * Get the default namespace for the class.
167
     *
168
     * @param  string  $rootNamespace
169
     * @return string
170
     */
171
    protected function getDefaultNamespace($rootNamespace)
172
    {
173
        return rtrim($this->generatorPreset()->modelNamespace(), '\\');
9✔
174
    }
175

176
    /**
177
     * Qualify the given model class base name.
178
     *
179
     * @param  string  $model
180
     * @return string
181
     */
182
    protected function qualifyModel(string $model)
183
    {
184
        return $this->qualifyModelUsingCanvas($model);
×
185
    }
186

187
    /**
188
     * Get the destination class path.
189
     *
190
     * @param  string  $name
191
     * @return string
192
     */
193
    protected function getPath($name)
194
    {
195
        return $this->getPathUsingCanvas($name);
9✔
196
    }
197

198
    /**
199
     * Get the root namespace for the class.
200
     *
201
     * @return string
202
     */
203
    protected function rootNamespace()
204
    {
205
        return $this->rootNamespaceUsingCanvas();
9✔
206
    }
207
}
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

© 2025 Coveralls, Inc