Completed
Push — master ( 105fde...b25eac )
by Sam
01:40
created

ClassGenerator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php namespace Savannabits\JetstreamInertiaGenerator\Generators;
2
3
use Savannabits\JetstreamInertiaGenerator\Generators\Traits\Helpers;
4
use Savannabits\JetstreamInertiaGenerator\Generators\Traits\Names;
5
use Savannabits\JetstreamInertiaGenerator\Generators\Traits\Columns;
6
use Illuminate\Console\Command;
7
use Illuminate\Support\Facades\Schema;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Illuminate\Support\Str;
10
use Illuminate\Filesystem\Filesystem;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Output\OutputInterface;
13
14
abstract class ClassGenerator extends Command {
15
16
    use Helpers, Columns, Names;
17
18
    public string $classBaseName;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
19
    public string $classFullName;
20
    public string $classNamespace;
21
22
    /**
23
     * @var Filesystem
24
     */
25
    protected Filesystem $files;
26
27
    /**
28
     * Relations
29
     *
30
     * @var string
31
     */
32
    protected $relations = [];
33
34
    /**
35
     * Create a new controller creator command instance.
36
     *
37
     * @param Filesystem $files
38
     */
39
    public function __construct(Filesystem $files)
40
    {
41
        parent::__construct();
42
43
        $this->files = $files;
44
    }
45
46
    protected function getArguments(): array
47
    {
48
        return [
49
            ['table_name', InputArgument::REQUIRED, 'Name of the existing table'],
50
            ['class_name', InputArgument::OPTIONAL, 'Name of the generated class'],
51
        ];
52
    }
53
54
    /**
55
     * Generate default class name (for case if not passed as argument) from table name
56
     *
57
     * @param $tableName
58
     * @return mixed
59
     */
60
    abstract public function generateClassNameFromTable($tableName);
61
62
    /**
63
     * Build the class with the given name.
64
     *
65
     * @return string
66
     */
67
    abstract protected function buildClass(): string;
68
69
    public function getPathFromClassName($name) {
70
        $path = str_replace('\\', '/', $name).".php";
71
72
        return preg_replace('|^App/|', 'app/', $path);
73
    }
74
75
    /**
76
     * Get the full namespace for a given class, without the class name.
77
     *
78
     * @param string $name
79
     * @return string
80
     */
81
    protected function getNamespace(string $name): string
82
    {
83
        return trim(implode('\\', array_slice(explode('\\', $name), 0, -1)), '\\');
84
    }
85
86
    /**
87
     * Get the root namespace for the class.
88
     *
89
     * @return string
90
     */
91
    public function rootNamespace(): string
92
    {
93
        return $this->laravel->getNamespace();
94
    }
95
96
    /**
97
     * Parse the class name and format according to the root namespace.
98
     *
99
     * @param string $name
100
     * @return string
101
     */
102
    public function qualifyClass(string $name): string
103
    {
104
        $name = str_replace('/', '\\', $name);
105
106
        $rootNamespace = $this->rootNamespace();
107
108
        if (Str::startsWith($name, $rootNamespace)) {
109
            return $name;
110
        }
111
112
        return $this->qualifyClass(
113
            $this->getDefaultNamespace(trim($rootNamespace, '\\')).'\\'.$name
114
        );
115
    }
116
117
    /**
118
     * Get the default namespace for the class.
119
     *
120
     * @param string $rootNamespace
121
     * @return string
122
     */
123
    protected function getDefaultNamespace(string $rootNamespace): string
124
    {
125
        return $rootNamespace;
126
    }
127
128
    protected function generateClass($force = false): bool
129
    {
130
        $path = base_path($this->getPathFromClassName($this->classFullName));
131
        if ($this->alreadyExists($path)) {
132
            if($force) {
133
                $this->warn('File '.$path.' already exists! File will be deleted.');
134
                $this->files->delete($path);
135
            } else {
136
                //TODO: Backup the class then generate a new one... for recovery purposes.
137
                $this->error('File '.$path.' already exists!');
138
                return false;
139
            }
140
        }
141
142
        $this->makeDirectory($path);
143
        $this->files->put($path, $this->buildClass());
144
        return true;
145
    }
146
147
    /**
148
     * Execute the console command.
149
     *
150
     * @param InputInterface $input
151
     * @param OutputInterface $output
152
     * @return int
153
     */
154
    protected function execute(InputInterface $input, OutputInterface $output): int
155
    {
156
        if ($this instanceof Model) {
157
            $this->initCommonNames($this->argument('table_name'), $this->argument('class_name'), null, $this->option('model-with-full-namespace'));
158
        } else {
159
            $this->initCommonNames($this->argument('table_name'), $this->option('model-name'), null, $this->option('model-with-full-namespace'));
160
        }
161
162
        $this->initClassNames($this->argument('class_name'));
163
164
        return parent::execute($input, $output);
165
    }
166
167
    protected function initClassNames($className = null) {
168
        if (empty($className)) {
169
            $className = $this->generateClassNameFromTable($this->tableName);
170
        }
171
172
        $this->classFullName = $this->qualifyClass($className);
173
        $this->classBaseName = class_basename($this->classFullName);
174
        $this->classNamespace = Str::replaceLast("\\".$this->classBaseName, '', $this->classFullName);
175
    }
176
177
}
178