1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Maestriam\Samurai\Entities; |
4
|
|
|
|
5
|
|
|
use Maestriam\FileSystem\Foundation\Drive; |
6
|
|
|
use Maestriam\FileSystem\Foundation\File\FileInfo; |
7
|
|
|
use Maestriam\FileSystem\Support\FileSystem; |
8
|
|
|
|
9
|
|
|
abstract class Source extends Foundation |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Tema qual o arquivo pertence |
13
|
|
|
*/ |
14
|
|
|
protected Theme $themeInstance; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Nome do template |
18
|
|
|
*/ |
19
|
|
|
protected string $template; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Retorna os dados para a geração do arquivo composer.json |
23
|
|
|
* |
24
|
|
|
* @return array |
25
|
|
|
*/ |
26
|
|
|
abstract protected function placeholders() : array; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Retorna o nome do arquivo |
30
|
|
|
* |
31
|
|
|
* @return string |
32
|
|
|
*/ |
33
|
|
|
abstract protected function filename() : string; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Define o tema que será |
37
|
|
|
* |
38
|
|
|
* @param Theme $theme |
39
|
|
|
* @return self |
40
|
|
|
*/ |
41
|
|
|
protected function setTheme(Theme $theme) : self |
42
|
|
|
{ |
43
|
|
|
$this->themeInstance = $theme; |
44
|
|
|
return $this; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Retorna a instância do tema |
49
|
|
|
* |
50
|
|
|
* @return Theme |
51
|
|
|
*/ |
52
|
|
|
protected function theme() : Theme |
53
|
|
|
{ |
54
|
|
|
return $this->themeInstance; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Retorna o drive para a manipulação de arquivos dentro do tema. |
59
|
|
|
* Se não houver drive criado para o tema, configura um novo drive. |
60
|
|
|
* |
61
|
|
|
* @return Drive |
62
|
|
|
*/ |
63
|
|
|
private function getDrive() : Drive |
64
|
|
|
{ |
65
|
|
|
$name = $this->theme()->vendor()->package(); |
66
|
|
|
|
67
|
|
|
$drive = FileSystem::drive($name); |
68
|
|
|
|
69
|
|
|
return ($drive->exists()) ? $drive : $this->initDrive($drive); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Configura um novo drive para criação de tema |
74
|
|
|
* |
75
|
|
|
* @param Drive $drive |
76
|
|
|
* @return Drive |
77
|
|
|
*/ |
78
|
|
|
private function initDrive(Drive $drive) : Drive |
79
|
|
|
{ |
80
|
|
|
$root = $this->theme()->paths()->root(); |
81
|
|
|
$stub = $this->config()->template(); |
82
|
|
|
$path = $this->config()->structure(); |
83
|
|
|
|
84
|
|
|
$drive->structure()->root($root); |
85
|
|
|
$drive->structure()->template($stub); |
86
|
|
|
$drive->structure()->paths($path); |
87
|
|
|
|
88
|
|
|
$drive->save(); |
89
|
|
|
|
90
|
|
|
return $drive; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Executa a criação do arquivo, baseado em um template |
95
|
|
|
* |
96
|
|
|
* @return void |
97
|
|
|
*/ |
98
|
|
|
protected function createFile() : FileInfo |
99
|
|
|
{ |
100
|
|
|
$drive = $this->getDrive(); |
101
|
|
|
$filename = $this->filename(); |
102
|
|
|
$holders = $this->placeholders(); |
103
|
|
|
|
104
|
|
|
return $drive->template($this->template)->create($filename, $holders); |
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Retorna a previa do conteúdo do template já interpretado |
109
|
|
|
* |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
|
|
protected function previewContent() : string |
113
|
|
|
{ |
114
|
|
|
$drive = $this->getDrive(); |
115
|
|
|
$holders = $this->placeholders(); |
116
|
|
|
|
117
|
|
|
return $drive->template($this->template)->preview($holders); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Verifica se o arquivo existe, de acordo com o template |
122
|
|
|
* |
123
|
|
|
* @return boolean |
124
|
|
|
*/ |
125
|
|
|
protected function fileExists() : bool |
126
|
|
|
{ |
127
|
|
|
$drive = $this->getDrive(); |
128
|
|
|
$template = $this->template; |
129
|
|
|
$filename = $this->filename(); |
130
|
|
|
|
131
|
|
|
return $drive->template($template)->exists($filename); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
protected function loadContent() : string |
135
|
|
|
{ |
136
|
|
|
$drive = $this->getDrive(); |
137
|
|
|
$template = $this->template; |
138
|
|
|
$filename = $this->filename(); |
139
|
|
|
|
140
|
|
|
return $drive->template($template)->load($filename); |
|
|
|
|
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
protected function filePath() : string |
144
|
|
|
{ |
145
|
|
|
$drive = $this->getDrive(); |
146
|
|
|
$template = $this->template; |
147
|
|
|
$filename = $this->filename(); |
148
|
|
|
|
149
|
|
|
return $drive->template($template)->path($filename); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|