1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kris\LaravelFormBuilder\Console; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\GeneratorCommand; |
6
|
|
|
use Illuminate\Filesystem\Filesystem; |
7
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
9
|
|
|
|
10
|
|
|
class FormMakeCommand extends GeneratorCommand |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* The console command name. |
15
|
|
|
* |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $name = 'make:form'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The console command description. |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $description = 'Create a form builder class'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Type of the file generated. |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $type = 'Form'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var FormGenerator |
36
|
|
|
*/ |
37
|
|
|
private $formGenerator; |
38
|
|
|
|
39
|
|
|
public function __construct(Filesystem $files, FormGenerator $formGenerator) |
40
|
|
|
{ |
41
|
|
|
parent::__construct($files); |
42
|
|
|
$this->formGenerator = $formGenerator; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Get the console command arguments. |
47
|
|
|
* |
48
|
|
|
* @return array |
49
|
|
|
*/ |
50
|
|
|
protected function getArguments() |
51
|
|
|
{ |
52
|
|
|
return array( |
53
|
|
|
array('name', InputArgument::REQUIRED, 'Full class name of the desired form class.'), |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get the console command options. |
59
|
|
|
* |
60
|
|
|
* @return array |
61
|
|
|
*/ |
62
|
|
|
protected function getOptions() |
63
|
|
|
{ |
64
|
|
|
return array( |
65
|
|
|
array('fields', null, InputOption::VALUE_OPTIONAL, 'Fields for the form'), |
66
|
|
|
array('namespace', null, InputOption::VALUE_OPTIONAL, 'Class namespace'), |
67
|
|
|
array('path', null, InputOption::VALUE_OPTIONAL, 'File path') |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Replace the class name for the given stub. |
73
|
|
|
* |
74
|
|
|
* @param string $stub |
75
|
|
|
* @param string $name |
76
|
|
|
* @return string |
77
|
|
|
*/ |
78
|
|
|
protected function replaceClass($stub, $name) |
79
|
|
|
{ |
80
|
|
|
$formGenerator = $this->formGenerator; |
81
|
|
|
|
82
|
|
|
$stub = str_replace( |
83
|
|
|
'{{class}}', |
84
|
|
|
$formGenerator->getClassInfo($name)->className, |
85
|
|
|
$stub |
86
|
|
|
); |
87
|
|
|
|
88
|
|
|
return str_replace( |
89
|
|
|
'{{fields}}', |
90
|
|
|
$formGenerator->getFieldsVariable($this->option('fields')), |
|
|
|
|
91
|
|
|
$stub |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Replace the namespace for the given stub. |
97
|
|
|
* |
98
|
|
|
* @param string $stub |
99
|
|
|
* @param string $name |
100
|
|
|
* @return $this |
101
|
|
|
*/ |
102
|
|
|
protected function replaceNamespace(&$stub, $name) |
103
|
|
|
{ |
104
|
|
|
$path = $this->option('path'); |
105
|
|
|
$namespace = $this->option('namespace'); |
106
|
|
|
|
107
|
|
|
if (!$namespace) { |
108
|
|
|
$namespace = $this->formGenerator->getClassInfo($name)->namespace; |
109
|
|
|
|
110
|
|
|
if ($path) { |
111
|
|
|
$namespace = str_replace('/', '\\', trim($path, '/')); |
112
|
|
|
foreach ($this->getAutoload() as $autoloadNamespace => $autoloadPath) { |
113
|
|
|
if (preg_match('|'.$autoloadPath.'|', $path)) { |
114
|
|
|
$namespace = str_replace([$autoloadPath, '/'], [$autoloadNamespace, '\\'], trim($path, '/')); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$stub = str_replace('{{namespace}}', $namespace, $stub); |
121
|
|
|
|
122
|
|
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Get the stub file for the generator. |
127
|
|
|
* |
128
|
|
|
* @return string |
129
|
|
|
*/ |
130
|
|
|
protected function getStub() |
131
|
|
|
{ |
132
|
|
|
return __DIR__ . '/stubs/form-class-template.stub'; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Get psr-4 namespace. |
137
|
|
|
* |
138
|
|
|
* @return array |
139
|
|
|
*/ |
140
|
|
|
protected function getAutoload() |
141
|
|
|
{ |
142
|
|
|
$composerPath = base_path('/composer.json'); |
143
|
|
|
if (! file_exists($composerPath)) { |
144
|
|
|
return []; |
145
|
|
|
} |
146
|
|
|
$composer = json_decode(file_get_contents( |
147
|
|
|
$composerPath |
148
|
|
|
), true); |
149
|
|
|
|
150
|
|
|
return $composer['autoload']['psr-4'] ?? []; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Get the desired class name from the input. |
155
|
|
|
* |
156
|
|
|
* @return string |
157
|
|
|
*/ |
158
|
|
|
protected function getNameInput() |
159
|
|
|
{ |
160
|
|
|
return str_replace('/', '\\', $this->argument('name')); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @inheritdoc |
165
|
|
|
*/ |
166
|
|
|
protected function getPath($name) |
167
|
|
|
{ |
168
|
|
|
$optionsPath = $this->option('path'); |
169
|
|
|
|
170
|
|
|
if ($optionsPath !== null) { |
171
|
|
|
return join('/', [ |
172
|
|
|
$this->laravel->basePath(), |
173
|
|
|
trim($optionsPath, '/'), |
174
|
|
|
$this->getNameInput().'.php' |
175
|
|
|
]); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
return parent::getPath($name); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.