Issues (197)

src/Commands/ControllerMakeCommand.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace Salah3id\Domains\Commands;
4
5
use Illuminate\Support\Str;
6
use Salah3id\Domains\Support\Config\GenerateConfigReader;
7
use Salah3id\Domains\Support\Stub;
8
use Salah3id\Domains\Traits\DomainCommandTrait;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputOption;
11
12
class ControllerMakeCommand extends GeneratorCommand
13
{
14
    use DomainCommandTrait;
15
16
    /**
17
     * The name of argument being used.
18
     *
19
     * @var string
20
     */
21
    protected $argumentName = 'controller';
22
23
    /**
24
     * The console command name.
25
     *
26
     * @var string
27
     */
28
    protected $name = 'domain:make-controller';
29
30
    /**
31
     * The console command description.
32
     *
33
     * @var string
34
     */
35
    protected $description = 'Generate new restful controller for the specified domain.';
36
37
    /**
38
     * Get controller name.
39
     *
40
     * @return string
41
     */
42
    public function getDestinationFilePath()
43
    {
44
        $path = $this->laravel['domains']->getDomainPath($this->getDomainName());
45
46
        $controllerPath = GenerateConfigReader::read('controller');
47
48
        return $path . $controllerPath->getPath() . '/' . $this->getControllerName() . '.php';
49
    }
50
51
    /**
52
     * @return string
53
     */
54
    protected function getTemplateContents()
55
    {
56
        $domain = $this->laravel['domains']->findOrFail($this->getDomainName());
57
58
        return (new Stub($this->getStubName(), [
59
            'DOMAINNAME'        => $domain->getStudlyName(),
60
            'CONTROLLERNAME'    => $this->getControllerName(),
61
            'NAMESPACE'         => $domain->getStudlyName(),
62
            'CLASS_NAMESPACE'   => $this->getClassNamespace($domain),
63
            'CLASS'             => $this->getControllerNameWithoutNamespace(),
64
            'LOWER_NAME'        => $domain->getLowerName(),
65
            'DOMAIN'            => $this->getDomainName(),
66
            'NAME'              => $this->getDomainName(),
67
            'STUDLY_NAME'       => $domain->getStudlyName(),
68
            'DOMAIN_NAMESPACE'  => $this->laravel['domains']->config('namespace'),
69
        ]))->render();
70
    }
71
72
    /**
73
     * Get the console command arguments.
74
     *
75
     * @return array
76
     */
77
    protected function getArguments()
78
    {
79
        return [
80
            ['controller', InputArgument::REQUIRED, 'The name of the controller class.'],
81
            ['domain', InputArgument::OPTIONAL, 'The name of domain will be used.'],
82
        ];
83
    }
84
85
    /**
86
     * @return array
87
     */
88
    protected function getOptions()
89
    {
90
        return [
91
            ['plain', 'p', InputOption::VALUE_NONE, 'Generate a plain controller', null],
92
            ['api', null, InputOption::VALUE_NONE, 'Exclude the create and edit methods from the controller.'],
93
        ];
94
    }
95
96
    /**
97
     * @return array|string
98
     */
99
    protected function getControllerName()
100
    {
101
        $controller = Str::studly($this->argument('controller'));
0 ignored issues
show
It seems like $this->argument('controller') can also be of type array; however, parameter $value of Illuminate\Support\Str::studly() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

101
        $controller = Str::studly(/** @scrutinizer ignore-type */ $this->argument('controller'));
Loading history...
102
103
        if (Str::contains(strtolower($controller), 'controller') === false) {
104
            $controller .= 'Controller';
105
        }
106
107
        return $controller;
108
    }
109
110
    /**
111
     * @return array|string
112
     */
113
    private function getControllerNameWithoutNamespace()
114
    {
115
        return class_basename($this->getControllerName());
116
    }
117
118
    public function getDefaultNamespace(): string
119
    {
120
        $domain = $this->laravel['domains'];
121
122
        return $domain->config('paths.generator.controller.namespace') ?: $domain->config('paths.generator.controller.path', 'Http/Controllers');
123
    }
124
125
    /**
126
     * Get the stub file name based on the options
127
     * @return string
128
     */
129
    protected function getStubName()
130
    {
131
        if ($this->option('plain') === true) {
132
            $stub = '/controller-plain.stub';
133
        } elseif ($this->option('api') === true) {
134
            $stub = '/controller-api.stub';
135
        } else {
136
            $stub = '/controller.stub';
137
        }
138
139
        return $stub;
140
    }
141
}
142