|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Helldar\Support\Tools; |
|
4
|
|
|
|
|
5
|
|
|
use Helldar\Support\Exceptions\UnknownStubFileException; |
|
6
|
|
|
use Helldar\Support\Facades\Helpers\Str; |
|
7
|
|
|
|
|
8
|
|
|
final class Stub |
|
9
|
|
|
{ |
|
10
|
|
|
public const PHP_ARRAY = 'php_array.stub'; |
|
11
|
|
|
|
|
12
|
|
|
public const JSON = 'json.stub'; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Replace the contents of the template file. |
|
16
|
|
|
* |
|
17
|
|
|
* @param string $stub_file |
|
18
|
|
|
* @param array $replace |
|
19
|
|
|
* |
|
20
|
|
|
* @throws \Helldar\Support\Exceptions\UnknownStubFileException |
|
21
|
|
|
* |
|
22
|
|
|
* @return string |
|
23
|
|
|
*/ |
|
24
|
16 |
|
public function replace(string $stub_file, array $replace): string |
|
25
|
|
|
{ |
|
26
|
16 |
|
$content = $this->get($stub_file); |
|
27
|
|
|
|
|
28
|
16 |
|
return Str::replace($content, $replace); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Receive the contents of the template file. |
|
33
|
|
|
* |
|
34
|
|
|
* @param string $filename |
|
35
|
|
|
* |
|
36
|
|
|
* @throws \Helldar\Support\Exceptions\UnknownStubFileException |
|
37
|
|
|
* |
|
38
|
|
|
* @return string |
|
39
|
|
|
*/ |
|
40
|
24 |
|
public function get(string $filename): string |
|
41
|
|
|
{ |
|
42
|
24 |
|
if ($path = $this->path($filename)) { |
|
43
|
22 |
|
return file_get_contents($path); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
2 |
|
throw new UnknownStubFileException($filename); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Receive the path to the template file. |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $filename |
|
53
|
|
|
* |
|
54
|
|
|
* @return string|null |
|
55
|
|
|
*/ |
|
56
|
24 |
|
protected function path(string $filename): ?string |
|
57
|
|
|
{ |
|
58
|
24 |
|
$path = $this->isCustom($filename) ? $filename : __DIR__ . '/../../resources/stubs/' . $filename; |
|
59
|
|
|
|
|
60
|
24 |
|
return realpath($path); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Returns a link to the template file. |
|
65
|
|
|
* |
|
66
|
|
|
* If the file exists under the specified link, it will return it, |
|
67
|
|
|
* otherwise it will search in the default folder. |
|
68
|
|
|
* |
|
69
|
|
|
* @param string $path |
|
70
|
|
|
* |
|
71
|
|
|
* @return bool |
|
72
|
|
|
*/ |
|
73
|
24 |
|
protected function isCustom(string $path): bool |
|
74
|
|
|
{ |
|
75
|
24 |
|
return file_exists($path); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|