-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathGenerator.php
More file actions
165 lines (139 loc) · 3.46 KB
/
Generator.php
File metadata and controls
165 lines (139 loc) · 3.46 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
<?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;
/**
* 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
* @return bool
*/
public function write(string $type, 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 = $this->makeStubContent(
$type,
array_merge([
'namespace' => $namespace,
'className' => $classname
], $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 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;
}
}