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

orchestral / canvas / 4665386932

pending completion
4665386932

push

github

Mior Muhammad Zaki
wip

807 of 834 relevant lines covered (96.76%)

21.96 hits per line

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

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

3
namespace Orchestra\Canvas\Commands\Routing;
4

5
use Orchestra\Canvas\Commands\Generator;
6
use Orchestra\Canvas\Processors\GeneratesControllerCode;
7
use Symfony\Component\Console\Input\InputOption;
8

9
/**
10
 * @see https://github.com/laravel/framework/blob/10.x/src/Illuminate/Routing/Console/ControllerMakeCommand.php
11
 */
12
#[\Symfony\Component\Console\Attribute\AsCommand(name: 'make:controller')]
13
class Controller extends Generator
14
{
15
    /**
16
     * The console command name.
17
     *
18
     * @var string
19
     */
20
    protected $name = 'make:controller';
21

22
    /**
23
     * The console command description.
24
     *
25
     * @var string
26
     */
27
    protected $description = 'Create a new controller class';
28

29
    /**
30
     * The type of class being generated.
31
     *
32
     * @var string
33
     */
34
    protected string $type = 'Controller';
35

36
    /**
37
     * Generator processor.
38
     *
39
     * @var string
40
     */
41
    protected $processor = GeneratesControllerCode::class;
42

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

51
    /**
52
     * Get the stub file for the generator.
53
     */
54
    public function getStubFile(): string
55
    {
56
        return $this->getStubFileFromPresetStorage($this->preset, $this->getStubFileName());
16✔
57
    }
58

59
    /**
60
     * Get the stub file name for the generator.
61
     */
62
    public function getStubFileName(): string
63
    {
64
        $stub = null;
16✔
65

66
        /** @var string $type */
67
        $type = $this->option('type');
16✔
68

69
        if ($type) {
16✔
70
            $stub = "controller.{$type}.stub";
1✔
71
        } elseif ($this->option('parent')) {
15✔
72
            $stub = $this->option('singleton')
2✔
73
                        ? 'controller.nested.singleton.stub'
×
74
                        : 'controller.nested.stub';
2✔
75
        } elseif ($this->option('model')) {
13✔
76
            $stub = 'controller.model.stub';
7✔
77
        } elseif ($this->option('invokable')) {
6✔
78
            $stub = 'controller.invokable.stub';
2✔
79
        } elseif ($this->option('singleton')) {
4✔
80
            $stub = 'controller.singleton.stub';
×
81
        } elseif ($this->option('resource')) {
4✔
82
            $stub = 'controller.stub';
×
83
        }
84

85
        if ($this->option('api') && \is_null($stub)) {
16✔
86
            $stub = 'controller.api.stub';
1✔
87
        } elseif ($this->option('api') && ! \is_null($stub) && ! $this->option('invokable')) {
15✔
88
            $stub = str_replace('.stub', '.api.stub', $stub);
2✔
89
        }
90

91
        return $stub ?? 'controller.plain.stub';
16✔
92
    }
93

94
    /**
95
     * Get the default namespace for the class.
96
     */
97
    public function getDefaultNamespace(string $rootNamespace): string
98
    {
99
        return $rootNamespace.'\Http\Controllers';
16✔
100
    }
101

102
    /**
103
     * Generator options.
104
     *
105
     * @return array<string, mixed>
106
     */
107
    public function generatorOptions(): array
108
    {
109
        return array_merge(parent::generatorOptions(), [
16✔
110
            'model' => $this->option('model'),
16✔
111
            'parent' => $this->option('parent'),
16✔
112
            'requests' => $this->option('requests'),
16✔
113
        ]);
16✔
114
    }
115

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

126
    /**
127
     * Create request.
128
     */
129
    public function createRequest(string $className): void
130
    {
131
        if ($this->confirm("A {$className} request does not exist. Do you want to generate it?", true)) {
1✔
132
            $this->call('make:request', ['name' => $className]);
1✔
133
        }
134
    }
135

136
    /**
137
     * Get the console command options.
138
     *
139
     * @return array<int, array>
140
     */
141
    protected function getOptions()
142
    {
143
        return [
90✔
144
            ['api', null, InputOption::VALUE_NONE, 'Exclude the create and edit methods from the controller.'],
90✔
145
            ['force', null, InputOption::VALUE_NONE, 'Create the class even if the controller already exists'],
90✔
146
            ['invokable', 'i', InputOption::VALUE_NONE, 'Generate a single method, invokable controller class.'],
90✔
147
            ['model', 'm', InputOption::VALUE_OPTIONAL, 'Generate a resource controller for the given model.'],
90✔
148
            ['parent', 'p', InputOption::VALUE_OPTIONAL, 'Generate a nested resource controller class.'],
90✔
149
            ['resource', 'r', InputOption::VALUE_NONE, 'Generate a resource controller class.'],
90✔
150
            ['singleton', 's', InputOption::VALUE_NONE, 'Generate a singleton resource controller class'],
90✔
151
            ['type', null, InputOption::VALUE_REQUIRED, 'Manually specify the controller stub file to use.'],
90✔
152
            ['requests', 'R', InputOption::VALUE_NONE, 'Create new form request classes and use them in the resource controller'],
90✔
153
        ];
90✔
154
    }
155
}
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