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

orchestral / canvas / 5911156259

19 Aug 2023 11:35AM UTC coverage: 95.793% (-0.9%) from 96.723%
5911156259

Pull #22

github

web-flow
Merge d074609f4 into ee912994c
Pull Request #22: Workbench code generators

12 of 12 new or added lines in 2 files covered. (100.0%)

797 of 832 relevant lines covered (95.79%)

21.55 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 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\InputOption;
10

11
/**
12
 * @see https://github.com/laravel/framework/blob/10.x/src/Illuminate/Routing/Console/ControllerMakeCommand.php
13
 */
14
#[AsCommand(name: 'make:controller', description: 'Create a new controller class')]
15
class Controller extends Generator
16
{
17
    use CreatesMatchingTest;
18

19
    /**
20
     * The console command name.
21
     *
22
     * @var string
23
     */
24
    protected $name = 'make:controller';
25

26
    /**
27
     * The console command description.
28
     *
29
     * @var string
30
     */
31
    protected $description = 'Create a new controller class';
32

33
    /**
34
     * The type of class being generated.
35
     *
36
     * @var string
37
     */
38
    protected string $type = 'Controller';
39

40
    /**
41
     * Generator processor.
42
     *
43
     * @var string
44
     */
45
    protected $processor = GeneratesControllerCode::class;
46

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

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

63
    /**
64
     * Get the stub file name for the generator.
65
     */
66
    public function getStubFileName(): string
67
    {
68
        $stub = null;
16✔
69

70
        /** @var string $type */
71
        $type = $this->option('type');
16✔
72

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

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

95
        return $stub ?? 'controller.plain.stub';
16✔
96
    }
97

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

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

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

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

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