-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathGenerator.php
More file actions
241 lines (201 loc) · 5.24 KB
/
Copy pathGenerator.php
File metadata and controls
241 lines (201 loc) · 5.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<?php
declare(strict_types=1);
namespace Bow\Console;
use Bow\Console\Traits\ConsoleTrait;
use Bow\Support\Str;
class Generator
{
use ConsoleTrait;
/**
* The base directory where that are going to generate
*
* @var string
*/
private string $base_directory;
/**
* Define the stub path
*
* @var string
*/
private string $stub_path;
/**
* The generate name
*
* @var string
*/
private string $name;
/**
* GeneratorCommand constructor
*
* @param string $base_directory
* @param string $name
*/
public function __construct(string $base_directory, string $name)
{
$this->base_directory = $base_directory;
$this->name = $name;
}
/**
* Check if controller exists
*
* @return bool
*/
public function fileExists(): bool
{
$this->filenameIsValid($this->name);
return file_exists($this->getPath()) || is_dir($this->base_directory . "/" . $this->name);
}
/**
* Check if filename is valid
*
* @param string|null $filename
*/
public function filenameIsValid(?string $filename): void
{
if (is_null($filename)) {
echo Color::red('The file name is invalid.');
exit(1);
}
}
/**
* Get file path
*
* @return string
*/
public function getPath(): string
{
return $this->base_directory . "/" . $this->name . ".php";
}
/**
* Check if controller exists
*
* @return bool
*/
public function exists(): bool
{
$this->filenameIsValid($this->name);
return file_exists($this->getPath());
}
/**
* Write file
*
* @param string $type
* @param array $data
* @param bool $using_path
* @return bool
*/
public function write(string $type, array $data = [], bool $using_path = false): bool
{
$dirname = dirname($this->name);
if (!is_dir($this->base_directory)) {
@mkdir($this->base_directory, 0777, true);
}
if ($dirname != '.') {
@mkdir($this->base_directory . '/' . trim($dirname, '/'), 0777, true);
$namespace = '\\' . str_replace('/', '\\', ucfirst(trim($dirname, '/')));
} else {
$namespace = '';
}
// Transform class to match the PSR-2 standard
$classname = ucfirst(
Str::camel(basename($this->name))
);
// Create the stub parsed content
$template_data = array_merge([
'namespace' => $namespace,
'className' => $classname
], $data);
$template = $this->makeStubContent(
$type,
$template_data
);
return (bool) file_put_contents($this->getPath(), $template);
}
/**
* Write file
*
* @param array $data
* @return bool
*/
public function writeFromDefineStubeFile(array $data = []): bool
{
$dirname = dirname($this->name);
if (!is_dir($this->base_directory)) {
@mkdir($this->base_directory, 0777, true);
}
if ($dirname != '.') {
@mkdir($this->base_directory . '/' . trim($dirname, '/'), 0777, true);
$namespace = '\\' . str_replace('/', '\\', ucfirst(trim($dirname, '/')));
} else {
$namespace = '';
}
// Transform class to match the PSR-2 standard
$classname = ucfirst(
Str::camel(basename($this->name))
);
// Create the stub parsed content
$template_data = array_merge([
'namespace' => $namespace,
'className' => $classname
], $data);
$template = $this->makeUsingStubPathContent($template_data);
return (bool) file_put_contents($this->getPath(), $template);
}
/**
* Stub render
*
* @param string $type
* @param array $data
* @return string
*/
public function makeStubContent(string $type, array $data = []): string
{
$content = file_get_contents(__DIR__ . '/stubs/' . $type . '.stub');
foreach ($data as $key => $value) {
$content = str_replace('{' . $key . '}', (string)$value, $content);
}
return $content;
}
/**
* Set the stub path
*
* @param string $path
* @return void
*/
public function setStubPath(string $path)
{
$this->stub_path = $path;
}
/**
* Make stub using path
*
* @param array $data
* @return string
*/
public function makeUsingStubPathContent(array $data = []): string
{
$content = file_get_contents($this->stub_path);
foreach ($data as $key => $value) {
$content = str_replace('{' . $key . '}', (string)$value, $content);
}
return $content;
}
/**
* Set writing filename
*
* @param string $name
*/
public function setName(string $name): void
{
$this->name = $name;
}
/**
* Get the base directory
*
* @param string $base_directory
*/
public function setBaseDirectory(string $base_directory): void
{
$this->base_directory = $base_directory;
}
}