|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Arcanedev\Support; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class Stub |
|
9
|
|
|
* |
|
10
|
|
|
* @author ARCANEDEV <[email protected]> |
|
11
|
|
|
*/ |
|
12
|
|
|
class Stub |
|
13
|
|
|
{ |
|
14
|
|
|
/* ----------------------------------------------------------------- |
|
15
|
|
|
| Properties |
|
16
|
|
|
| ----------------------------------------------------------------- |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* The stub path. |
|
21
|
|
|
* |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $path; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* The base path of stub file. |
|
28
|
|
|
* |
|
29
|
|
|
* @var string|null |
|
30
|
|
|
*/ |
|
31
|
|
|
protected static $basePath = null; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* The replacements array. |
|
35
|
|
|
* |
|
36
|
|
|
* @var array |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $replaces = []; |
|
39
|
|
|
|
|
40
|
|
|
/* ----------------------------------------------------------------- |
|
41
|
|
|
| Constructor |
|
42
|
|
|
| ----------------------------------------------------------------- |
|
43
|
|
|
*/ |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Create a new instance. |
|
47
|
|
|
* |
|
48
|
|
|
* @param string $path |
|
49
|
|
|
* @param array $replaces |
|
50
|
|
|
*/ |
|
51
|
18 |
|
public function __construct($path, array $replaces = []) |
|
52
|
|
|
{ |
|
53
|
18 |
|
$this->setPath($path); |
|
54
|
18 |
|
$this->setReplaces($replaces); |
|
55
|
18 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
/* ----------------------------------------------------------------- |
|
58
|
|
|
| Getters & Setters |
|
59
|
|
|
| ----------------------------------------------------------------- |
|
60
|
|
|
*/ |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Get stub path. |
|
64
|
|
|
* |
|
65
|
|
|
* @return string |
|
66
|
|
|
*/ |
|
67
|
18 |
|
public function getPath(): string |
|
68
|
|
|
{ |
|
69
|
18 |
|
$path = $this->path; |
|
70
|
|
|
|
|
71
|
18 |
|
if ( ! empty(static::$basePath)) { |
|
72
|
6 |
|
$path = static::$basePath.DIRECTORY_SEPARATOR.ltrim($path, DIRECTORY_SEPARATOR); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
18 |
|
return $path; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Set stub path. |
|
80
|
|
|
* |
|
81
|
|
|
* @param string $path |
|
82
|
|
|
* |
|
83
|
|
|
* @return $this |
|
84
|
|
|
*/ |
|
85
|
18 |
|
public function setPath(string $path): self |
|
86
|
|
|
{ |
|
87
|
18 |
|
$this->path = $path; |
|
88
|
|
|
|
|
89
|
18 |
|
return $this; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Get base path. |
|
94
|
|
|
* |
|
95
|
|
|
* @return string|null |
|
96
|
|
|
*/ |
|
97
|
18 |
|
public static function getBasePath(): ?string |
|
98
|
|
|
{ |
|
99
|
18 |
|
return static::$basePath; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Set base path. |
|
104
|
|
|
* |
|
105
|
|
|
* @param string $path |
|
106
|
|
|
*/ |
|
107
|
18 |
|
public static function setBasePath(string $path) |
|
108
|
|
|
{ |
|
109
|
18 |
|
static::$basePath = $path; |
|
110
|
18 |
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Get replacements. |
|
114
|
|
|
* |
|
115
|
|
|
* @return array |
|
116
|
|
|
*/ |
|
117
|
18 |
|
public function getReplaces(): array |
|
118
|
|
|
{ |
|
119
|
18 |
|
return $this->replaces; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Set replacements array. |
|
124
|
|
|
* |
|
125
|
|
|
* @param array $replaces |
|
126
|
|
|
* |
|
127
|
|
|
* @return $this |
|
128
|
|
|
*/ |
|
129
|
18 |
|
public function setReplaces(array $replaces = []): self |
|
130
|
|
|
{ |
|
131
|
18 |
|
$this->replaces = $replaces; |
|
132
|
|
|
|
|
133
|
18 |
|
return $this; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Set replacements array. |
|
138
|
|
|
* |
|
139
|
|
|
* @param array $replaces |
|
140
|
|
|
* |
|
141
|
|
|
* @return $this |
|
142
|
|
|
*/ |
|
143
|
6 |
|
public function replaces(array $replaces = []): self |
|
144
|
|
|
{ |
|
145
|
6 |
|
return $this->setReplaces($replaces); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/* ----------------------------------------------------------------- |
|
149
|
|
|
| Main Methods |
|
150
|
|
|
| ----------------------------------------------------------------- |
|
151
|
|
|
*/ |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Create new self instance. |
|
155
|
|
|
* |
|
156
|
|
|
* @param string $path |
|
157
|
|
|
* @param array $replaces |
|
158
|
|
|
* |
|
159
|
|
|
* @return $this |
|
160
|
|
|
*/ |
|
161
|
6 |
|
public static function create(string $path, array $replaces = []): self |
|
162
|
|
|
{ |
|
163
|
6 |
|
return new static($path, $replaces); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Create new self instance from full path. |
|
168
|
|
|
* |
|
169
|
|
|
* @param string $path |
|
170
|
|
|
* @param array $replaces |
|
171
|
|
|
* |
|
172
|
|
|
* @return $this |
|
173
|
|
|
*/ |
|
174
|
6 |
|
public static function createFromPath(string $path, array $replaces = []): self |
|
175
|
|
|
{ |
|
176
|
6 |
|
return tap(new static($path, $replaces), function (self $stub) { |
|
177
|
6 |
|
$stub->setBasePath(''); |
|
178
|
6 |
|
}); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Get stub contents. |
|
183
|
|
|
* |
|
184
|
|
|
* @return string |
|
185
|
|
|
*/ |
|
186
|
12 |
|
public function render(): string |
|
187
|
|
|
{ |
|
188
|
12 |
|
return $this->getContents(); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Save stub to base path. |
|
193
|
|
|
* |
|
194
|
|
|
* @param string $filename |
|
195
|
|
|
* |
|
196
|
|
|
* @return bool |
|
197
|
|
|
*/ |
|
198
|
6 |
|
public function save(string $filename): bool |
|
199
|
|
|
{ |
|
200
|
6 |
|
return $this->saveTo(self::getBasePath(), $filename); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* Save stub to specific path. |
|
205
|
|
|
* |
|
206
|
|
|
* @param string $path |
|
207
|
|
|
* @param string $filename |
|
208
|
|
|
* |
|
209
|
|
|
* @return bool |
|
210
|
|
|
*/ |
|
211
|
6 |
|
public function saveTo(string $path, string $filename): bool |
|
212
|
|
|
{ |
|
213
|
6 |
|
return file_put_contents($path.DIRECTORY_SEPARATOR.$filename, $this->render()) !== false; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* Get stub contents. |
|
218
|
|
|
* |
|
219
|
|
|
* @return string|mixed |
|
220
|
|
|
*/ |
|
221
|
12 |
|
public function getContents() |
|
222
|
|
|
{ |
|
223
|
12 |
|
$contents = file_get_contents($this->getPath()); |
|
224
|
|
|
|
|
225
|
12 |
|
foreach ($this->getReplaces() as $search => $replace) { |
|
226
|
6 |
|
$contents = str_replace('$'.strtoupper($search).'$', $replace, $contents); |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
12 |
|
return $contents; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* Handle magic method __toString. |
|
234
|
|
|
* |
|
235
|
|
|
* @return string |
|
236
|
|
|
*/ |
|
237
|
6 |
|
public function __toString(): string |
|
238
|
|
|
{ |
|
239
|
6 |
|
return $this->render(); |
|
240
|
|
|
} |
|
241
|
|
|
} |
|
242
|
|
|
|