1 | <?php |
||
2 | |||
3 | namespace Maestriam\FileSystem\Foundation\Template; |
||
4 | |||
5 | use Exception; |
||
6 | use Maestriam\FileSystem\Foundation\Drive\PathSanitizer; |
||
7 | use Maestriam\FileSystem\Foundation\Drive\StructureDirectory; |
||
8 | |||
9 | class StubFile |
||
10 | { |
||
11 | /** |
||
12 | * Nome do arquivo stub |
||
13 | */ |
||
14 | private string $name; |
||
15 | |||
16 | /** |
||
17 | * Conteúdo do arquivo .stub |
||
18 | */ |
||
19 | private string $content = ''; |
||
20 | |||
21 | /** |
||
22 | * Caminho para o diretório onde está os arquivos .stubs |
||
23 | */ |
||
24 | private string $source = ''; |
||
25 | |||
26 | /** |
||
27 | * Extensão dos arquivos stubs |
||
28 | */ |
||
29 | private string $ext = '.stub'; |
||
30 | |||
31 | /** |
||
32 | * Carrega as informaçõea do arquivo stub de template |
||
33 | * |
||
34 | * @param string $source |
||
35 | * @param string $name |
||
36 | */ |
||
37 | public function __construct(string $source, string $name) |
||
38 | { |
||
39 | $this->setSourcePath($source) |
||
40 | ->setName($name) |
||
41 | ->loadContent(); |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * Define o caminho onde estão os arquivos de templates |
||
46 | * |
||
47 | * @param string $source |
||
48 | * @return void |
||
49 | */ |
||
50 | private function setSourcePath(string $source) : self |
||
51 | { |
||
52 | $source = PathSanitizer::sanitize($source); |
||
53 | |||
54 | if (! is_dir($source)) { |
||
55 | $err = sprintf('Template folder [%s] not found', $source); |
||
56 | throw new \Exception($err); |
||
57 | } |
||
58 | |||
59 | $this->source = $source; |
||
60 | return $this; |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
61 | } |
||
62 | |||
63 | /** |
||
64 | * Define o nome do arquivo stub que será manipulado |
||
65 | * |
||
66 | * @param string $name |
||
67 | * @return Stub |
||
68 | */ |
||
69 | private function setName(string $name) : self |
||
70 | { |
||
71 | $file = $this->getStubFile($name); |
||
72 | |||
73 | if (! is_file($file)) { |
||
74 | throw new \Exception("Stub file called '{$file}' not found in folder template"); |
||
75 | } |
||
76 | |||
77 | $this->name = $name; |
||
78 | return $this; |
||
0 ignored issues
–
show
|
|||
79 | } |
||
80 | |||
81 | /** |
||
82 | * Carrega o conteúdo do arquivo stub |
||
83 | * |
||
84 | * @return Stub |
||
85 | */ |
||
86 | private function loadContent() : self |
||
87 | { |
||
88 | $file = $this->getStubFile(); |
||
89 | |||
90 | $this->content = file_get_contents($file); |
||
91 | |||
92 | return $this; |
||
0 ignored issues
–
show
|
|||
93 | } |
||
94 | |||
95 | /** |
||
96 | * Forma a string do caminho completo do arquivo de stub |
||
97 | * |
||
98 | * @param string $name |
||
99 | * @return string |
||
100 | */ |
||
101 | private function getStubFile(string $name = null) : string |
||
102 | { |
||
103 | $name = $name ?? $this->name; |
||
104 | |||
105 | if (! $name) { |
||
106 | throw new \Exception('Stub name bad formatted'); |
||
107 | } |
||
108 | |||
109 | return $this->source . $name . $this->ext; |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Retorna o conteúdo, sem tratamentos, dentro do arquivo stub |
||
114 | * |
||
115 | * @return string |
||
116 | */ |
||
117 | public function raw() : string |
||
118 | { |
||
119 | return $this->content; |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * Retorna o conteúdo do stub com as devidas conversões, |
||
124 | * de acordo com os itens enviados pelo usuário |
||
125 | * |
||
126 | * @return void |
||
127 | */ |
||
128 | public function parse(array $placeholders = []) : string |
||
129 | { |
||
130 | $content = $this->raw(); |
||
131 | |||
132 | return BraceParser::parse($content, $placeholders); |
||
0 ignored issues
–
show
|
|||
133 | } |
||
134 | } |