1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Maestriam\FileSystem\Foundation; |
4
|
|
|
|
5
|
|
|
use Maestriam\FileSystem\Foundation\Drive\PathSanitizer; |
6
|
|
|
use Maestriam\FileSystem\Foundation\Template\StubFile; |
7
|
|
|
use Maestriam\FileSystem\Foundation\Drive\StructureDirectory; |
8
|
|
|
use Maestriam\FileSystem\Foundation\File\FileInfo; |
9
|
|
|
use Maestriam\FileSystem\Support\FileSystem; |
|
|
|
|
10
|
|
|
|
11
|
|
|
class Template |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Nome do temmplate utilizado |
15
|
|
|
*/ |
16
|
|
|
private string $name; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Nome do arquivo stub |
20
|
|
|
*/ |
21
|
|
|
private StubFile $stub; |
22
|
|
|
|
23
|
|
|
private string $filename; |
24
|
|
|
|
25
|
|
|
private array $placeholders = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Regras de negócio de diretório |
29
|
|
|
*/ |
30
|
|
|
private StructureDirectory $structure; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Regras de negócio para manipulação de arquivos baseado em template |
34
|
|
|
* |
35
|
|
|
* @param string $name |
36
|
|
|
* @param StructureDirectory $structure |
37
|
|
|
*/ |
38
|
|
|
public function __construct(string $name, StructureDirectory $structure) |
39
|
|
|
{ |
40
|
|
|
$this->setStructure($structure)->loadStub($name)->setName($name); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Define a estrutura de diretórios utilizadas no template |
45
|
|
|
* |
46
|
|
|
* @param StructureDirectory $structure |
47
|
|
|
* @return Template |
48
|
|
|
*/ |
49
|
|
|
private function setStructure(StructureDirectory $structure) : Template |
50
|
|
|
{ |
51
|
|
|
$this->structure = $structure; |
52
|
|
|
|
53
|
|
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Carrega as informações de um arquivo stub |
58
|
|
|
* |
59
|
|
|
* @param string $name |
60
|
|
|
* @return Template |
61
|
|
|
*/ |
62
|
|
|
private function loadStub(string $name) : Template |
63
|
|
|
{ |
64
|
|
|
$source = $this->structure->template; |
|
|
|
|
65
|
|
|
|
66
|
|
|
$this->stub = new StubFile($source, $name); |
67
|
|
|
|
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Define o nome do arquivo de template |
73
|
|
|
* |
74
|
|
|
* @param string $name |
75
|
|
|
* @return Template |
76
|
|
|
*/ |
77
|
|
|
private function setName(string $name) : Template |
78
|
|
|
{ |
79
|
|
|
$this->name = $name; |
80
|
|
|
|
81
|
|
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function filename(string $filename) : Template |
85
|
|
|
{ |
86
|
|
|
$this->filename = $filename; |
87
|
|
|
|
88
|
|
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function placeholders(array $placeholders = []) : Template |
92
|
|
|
{ |
93
|
|
|
$this->placeholders = $placeholders; |
94
|
|
|
|
95
|
|
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Cria um arquivo baseado em um template |
101
|
|
|
* |
102
|
|
|
* @param string $filename |
103
|
|
|
* @param array $placeholders |
104
|
|
|
* @return void |
105
|
|
|
*/ |
106
|
|
|
public function create(string $filename = null, array $placeholders = null) : FileInfo |
107
|
|
|
{ |
108
|
|
|
$filename = $filename ?? $this->filename; |
109
|
|
|
|
110
|
|
|
$placeholders = $placeholders ?? $this->placeholders; |
111
|
|
|
|
112
|
|
|
$content = $this->stub->parse($placeholders); |
113
|
|
|
|
114
|
|
|
$folder = $this->structure->findByTemplate($this->name); |
115
|
|
|
|
116
|
|
|
$file = new File($filename); |
117
|
|
|
|
118
|
|
|
return $file->setFolder($folder)->create($content); |
|
|
|
|
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Retorna o conteúdo de um template já interpretado |
123
|
|
|
* |
124
|
|
|
* @param array $placeholders |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
|
|
public function preview(array $placeholders) : string |
128
|
|
|
{ |
129
|
|
|
return $this->stub->parse($placeholders); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Retorna se o arquivo, baseado em um modelo de template, |
134
|
|
|
* já existe no diretório pré-definido. |
135
|
|
|
* |
136
|
|
|
* @param array $placeholders |
137
|
|
|
* @return string |
138
|
|
|
*/ |
139
|
|
|
public function exists(string $filename) : bool |
140
|
|
|
{ |
141
|
|
|
$file = $this->generateFilename($filename); |
142
|
|
|
|
143
|
|
|
return (is_file($file)) ? true : false; |
|
|
|
|
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Retorna o contéudo |
148
|
|
|
* |
149
|
|
|
* @param string $filename |
150
|
|
|
* @return string|null |
151
|
|
|
*/ |
152
|
|
|
public function load(string $filename) : ?string |
153
|
|
|
{ |
154
|
|
|
if (! $this->exists($filename)) { |
155
|
|
|
return null; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
$file = $this->generateFilename($filename); |
159
|
|
|
|
160
|
|
|
return file_get_contents($file); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Retorna o caminho absoluto do arquivo, baseado nas premissas |
165
|
|
|
* definidas do template. |
166
|
|
|
* |
167
|
|
|
* @return string |
168
|
|
|
*/ |
169
|
|
|
public function path(string $filename) : string |
170
|
|
|
{ |
171
|
|
|
return $this->generateFilename($filename); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
private function generateFilename(string $filename) : string |
175
|
|
|
{ |
176
|
|
|
$folder = $this->structure->findByTemplate($this->name); |
177
|
|
|
|
178
|
|
|
$path = $folder . DS . $filename; |
179
|
|
|
|
180
|
|
|
return PathSanitizer::sanitize($path); |
181
|
|
|
} |
182
|
|
|
} |
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: