1 | <?php |
||
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 = []) |
|
56 | |||
57 | /* ----------------------------------------------------------------- |
||
58 | | Getters & Setters |
||
59 | | ----------------------------------------------------------------- |
||
60 | */ |
||
61 | |||
62 | /** |
||
63 | * Get stub path. |
||
64 | * |
||
65 | * @return string |
||
66 | */ |
||
67 | 18 | public function getPath(): string |
|
77 | |||
78 | /** |
||
79 | * Set stub path. |
||
80 | * |
||
81 | * @param string $path |
||
82 | * |
||
83 | * @return $this |
||
84 | */ |
||
85 | 18 | public function setPath(string $path): self |
|
91 | |||
92 | /** |
||
93 | * Get base path. |
||
94 | * |
||
95 | * @return string|null |
||
96 | */ |
||
97 | 18 | public static function getBasePath(): ?string |
|
101 | |||
102 | /** |
||
103 | * Set base path. |
||
104 | * |
||
105 | * @param string $path |
||
106 | */ |
||
107 | 18 | public static function setBasePath(string $path) |
|
111 | |||
112 | /** |
||
113 | * Get replacements. |
||
114 | * |
||
115 | * @return array |
||
116 | */ |
||
117 | 18 | public function getReplaces(): array |
|
121 | |||
122 | /** |
||
123 | * Set replacements array. |
||
124 | * |
||
125 | * @param array $replaces |
||
126 | * |
||
127 | * @return $this |
||
128 | */ |
||
129 | 18 | public function setReplaces(array $replaces = []): self |
|
135 | |||
136 | /** |
||
137 | * Set replacements array. |
||
138 | * |
||
139 | * @param array $replaces |
||
140 | * |
||
141 | * @return $this |
||
142 | */ |
||
143 | 6 | public function replaces(array $replaces = []): self |
|
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 |
|
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 |
|
180 | |||
181 | /** |
||
182 | * Get stub contents. |
||
183 | * |
||
184 | * @return string |
||
185 | */ |
||
186 | 12 | public function render(): string |
|
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 |
|
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 |
|
215 | |||
216 | /** |
||
217 | * Get stub contents. |
||
218 | * |
||
219 | * @return string|mixed |
||
220 | */ |
||
221 | 12 | public function getContents() |
|
231 | |||
232 | /** |
||
233 | * Handle magic method __toString. |
||
234 | * |
||
235 | * @return string |
||
236 | */ |
||
237 | 6 | public function __toString(): string |
|
241 | } |
||
242 |