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

orchestral / canvas / 6130114868

09 Sep 2023 09:20AM UTC coverage: 89.286%. Remained the same
6130114868

push

github

crynobone
wip

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

1 of 1 new or added line in 1 file covered. (100.0%)

750 of 840 relevant lines covered (89.29%)

21.68 hits per line

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

65.57
/src/Commands/Routing/Controller.php
1
<?php
2

3
namespace Orchestra\Canvas\Commands\Routing;
4

5
use Illuminate\Console\Concerns\CreatesMatchingTest;
6
use Orchestra\Canvas\Commands\Generator;
7
use Orchestra\Canvas\Processors\GeneratesControllerCode;
8
use Symfony\Component\Console\Attribute\AsCommand;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Output\OutputInterface;
12

13
use function Laravel\Prompts\confirm;
14
use function Laravel\Prompts\select;
15
use function Laravel\Prompts\suggest;
16

17
/**
18
 * @see https://github.com/laravel/framework/blob/10.x/src/Illuminate/Routing/Console/ControllerMakeCommand.php
19
 */
20
#[AsCommand(name: 'make:controller', description: 'Create a new controller class')]
21
class Controller extends Generator
22
{
23
    use CreatesMatchingTest;
24

25
    /**
26
     * The type of class being generated.
27
     *
28
     * @var string
29
     */
30
    protected string $type = 'Controller';
31

32
    /**
33
     * Generator processor.
34
     *
35
     * @var class-string<\Orchestra\Canvas\Core\GeneratesCode>
36
     */
37
    protected string $processor = GeneratesControllerCode::class;
38

39
    /**
40
     * Get the stub file for the generator.
41
     */
42
    public function getPublishedStubFileName(): ?string
43
    {
44
        return $this->getStubFileName();
16✔
45
    }
46

47
    /**
48
     * Get the stub file name for the generator.
49
     */
50
    public function getStubFileName(): string
51
    {
52
        $stub = null;
16✔
53

54
        /** @var string $type */
55
        $type = $this->option('type');
16✔
56

57
        if ($type) {
16✔
58
            $stub = "controller.{$type}.stub";
1✔
59
        } elseif ($this->option('parent')) {
15✔
60
            $stub = $this->option('singleton')
2✔
61
                        ? 'controller.nested.singleton.stub'
×
62
                        : 'controller.nested.stub';
2✔
63
        } elseif ($this->option('model')) {
13✔
64
            $stub = 'controller.model.stub';
7✔
65
        } elseif ($this->option('invokable')) {
6✔
66
            $stub = 'controller.invokable.stub';
2✔
67
        } elseif ($this->option('singleton')) {
4✔
68
            $stub = 'controller.singleton.stub';
×
69
        } elseif ($this->option('resource')) {
4✔
70
            $stub = 'controller.stub';
×
71
        }
72

73
        if ($this->option('api') && \is_null($stub)) {
16✔
74
            $stub = 'controller.api.stub';
1✔
75
        } elseif ($this->option('api') && ! \is_null($stub) && ! $this->option('invokable')) {
15✔
76
            $stub = str_replace('.stub', '.api.stub', $stub);
2✔
77
        }
78

79
        return $stub ?? 'controller.plain.stub';
16✔
80
    }
81

82
    /**
83
     * Get the default namespace for the class.
84
     */
85
    public function getDefaultNamespace(string $rootNamespace): string
86
    {
87
        return $rootNamespace.'\Http\Controllers';
16✔
88
    }
89

90
    /**
91
     * Generator options.
92
     *
93
     * @return array<string, mixed>
94
     */
95
    public function generatorOptions(): array
96
    {
97
        return array_merge(parent::generatorOptions(), [
16✔
98
            'model' => $this->option('model'),
16✔
99
            'parent' => $this->option('parent'),
16✔
100
            'requests' => $this->option('requests'),
16✔
101
        ]);
16✔
102
    }
103

104
    /**
105
     * Create model.
106
     */
107
    public function createModel(string $className): void
108
    {
109
        if (confirm("A {$className} model does not exist. Do you want to generate it?", default: true)) {
9✔
110
            $this->call('make:model', ['name' => $className]);
9✔
111
        }
112
    }
113

114
    /**
115
     * Create request.
116
     */
117
    public function createRequest(string $className): void
118
    {
119
        if (confirm("A {$className} request does not exist. Do you want to generate it?", default: true)) {
1✔
120
            $this->call('make:request', ['name' => $className]);
1✔
121
        }
122
    }
123

124
    /**
125
     * Interact further with the user if they were prompted for missing arguments.
126
     *
127
     * @param  \Symfony\Component\Console\Input\InputInterface  $input
128
     * @param  \Symfony\Component\Console\Output\OutputInterface  $output
129
     * @return void
130
     */
131
    protected function afterPromptingForMissingArguments(InputInterface $input, OutputInterface $output)
132
    {
133
        if ($this->didReceiveOptions($input)) {
×
134
            return;
×
135
        }
136

137
        $type = select('Which type of controller would you like?', [
×
138
            'empty' => 'Empty',
×
139
            'resource' => 'Resource',
×
140
            'singleton' => 'Singleton',
×
141
            'api' => 'API',
×
142
            'invokable' => 'Invokable',
×
143
        ]);
×
144

145
        if ($type !== 'empty') {
×
146
            $input->setOption($type, true); // @phpstan-ignore-line
×
147
        }
148

149
        if (\in_array($type, ['api', 'resource', 'singleton'])) {
×
150
            $model = suggest(
×
151
                "What model should this $type controller be for? (Optional)",
×
152
                $this->possibleModels()
×
153
            );
×
154

155
            if ($model) {
×
156
                $input->setOption('model', $model);
×
157
            }
158
        }
159
    }
160

161
    /**
162
     * Get the console command options.
163
     *
164
     * @return array<int, array>
165
     */
166
    protected function getOptions()
167
    {
168
        return [
90✔
169
            ['api', null, InputOption::VALUE_NONE, 'Exclude the create and edit methods from the controller.'],
90✔
170
            ['force', null, InputOption::VALUE_NONE, 'Create the class even if the controller already exists'],
90✔
171
            ['invokable', 'i', InputOption::VALUE_NONE, 'Generate a single method, invokable controller class.'],
90✔
172
            ['model', 'm', InputOption::VALUE_OPTIONAL, 'Generate a resource controller for the given model.'],
90✔
173
            ['parent', 'p', InputOption::VALUE_OPTIONAL, 'Generate a nested resource controller class.'],
90✔
174
            ['resource', 'r', InputOption::VALUE_NONE, 'Generate a resource controller class.'],
90✔
175
            ['singleton', 's', InputOption::VALUE_NONE, 'Generate a singleton resource controller class'],
90✔
176
            ['type', null, InputOption::VALUE_REQUIRED, 'Manually specify the controller stub file to use.'],
90✔
177
            ['requests', 'R', InputOption::VALUE_NONE, 'Create new form request classes and use them in the resource controller'],
90✔
178
        ];
90✔
179
    }
180
}
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