|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Maestriam\FileSystem\Foundation\Drive; |
|
4
|
|
|
|
|
5
|
|
|
use Maestriam\FileSystem\Concerns\FluentGetter; |
|
6
|
|
|
|
|
7
|
|
|
class StructureDirectory |
|
8
|
|
|
{ |
|
9
|
|
|
use FluentGetter; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Caminho-raíz do projeto |
|
13
|
|
|
*/ |
|
14
|
|
|
private string $rootPath = '.'; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Caminho onde está os arquivos de template |
|
18
|
|
|
*/ |
|
19
|
|
|
private string $templatePath = '.'; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Lista de caminhos de template |
|
23
|
|
|
*/ |
|
24
|
|
|
private array $pathList = []; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Busc |
|
28
|
|
|
*/ |
|
29
|
|
|
private PathFinder $finder; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Define/Retorna o caminho-raíz do projeto, |
|
33
|
|
|
* de acordo com o tipo de parâmetro |
|
34
|
|
|
* |
|
35
|
|
|
* @param string $path |
|
36
|
|
|
* @return mixed |
|
37
|
|
|
*/ |
|
38
|
|
|
public function root(string $path = null) |
|
39
|
|
|
{ |
|
40
|
|
|
return (! $path) ? $this->getRoot() : $this->setRoot($path); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Define o caminho-raiz do projeto |
|
45
|
|
|
* |
|
46
|
|
|
* @param string $path |
|
47
|
|
|
* @return StructureDirectory |
|
48
|
|
|
*/ |
|
49
|
|
|
public function setRoot(string $path) : StructureDirectory |
|
50
|
|
|
{ |
|
51
|
|
|
$this->rootPath = $path; |
|
52
|
|
|
|
|
53
|
|
|
return $this; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Retorna o caminho-raiz do projeto |
|
58
|
|
|
* |
|
59
|
|
|
* @param string $path |
|
60
|
|
|
* @return StructureDirectory |
|
61
|
|
|
*/ |
|
62
|
|
|
private function getRoot() : string |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->rootPath; |
|
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Define/Retorna o caminho de diretório de templates |
|
69
|
|
|
* |
|
70
|
|
|
* @param string $path |
|
71
|
|
|
* @return mixed |
|
72
|
|
|
*/ |
|
73
|
|
|
public function template(string $path = null) |
|
74
|
|
|
{ |
|
75
|
|
|
return (! $path) ? $this->getTemplate() : $this->setTemplate($path); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Define o caminho de templates |
|
80
|
|
|
* |
|
81
|
|
|
* @param string $path |
|
82
|
|
|
* @return mixed |
|
83
|
|
|
*/ |
|
84
|
|
|
public function setTemplate(string $path) : StructureDirectory |
|
85
|
|
|
{ |
|
86
|
|
|
$this->templatePath = $path; |
|
87
|
|
|
|
|
88
|
|
|
return $this; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Retorna o caminho de templates |
|
93
|
|
|
* |
|
94
|
|
|
* @param string $path |
|
95
|
|
|
* @return mixed |
|
96
|
|
|
*/ |
|
97
|
|
|
private function getTemplate() : string |
|
98
|
|
|
{ |
|
99
|
|
|
return $this->templatePath; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Retorna caminho baseado nas configurações de template |
|
104
|
|
|
* |
|
105
|
|
|
* @param string $file |
|
106
|
|
|
* @return string |
|
107
|
|
|
*/ |
|
108
|
|
|
public function findByTemplate(string $template) : string |
|
109
|
|
|
{ |
|
110
|
|
|
$folder = $this->finder->findByTemplate($template); |
|
111
|
|
|
|
|
112
|
|
|
$path = $this->root() . DS . $folder . DS; |
|
113
|
|
|
|
|
114
|
|
|
return PathSanitizer::sanitize($path); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* |
|
119
|
|
|
* |
|
120
|
|
|
* @param array $paths |
|
121
|
|
|
* @return mixed |
|
122
|
|
|
*/ |
|
123
|
|
|
public function paths(array $paths = null) |
|
124
|
|
|
{ |
|
125
|
|
|
if (! $paths) { |
|
126
|
|
|
return $this->getPaths(); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
return $this->setPaths($paths)->initFinder(); |
|
|
|
|
|
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Inicia a instância com as regras de negócio para pesquisar |
|
134
|
|
|
* o diretório apropriado de acordo com o template |
|
135
|
|
|
* |
|
136
|
|
|
* @return void |
|
137
|
|
|
*/ |
|
138
|
|
|
private function initFinder() |
|
139
|
|
|
{ |
|
140
|
|
|
$structure = $this->getPaths(); |
|
141
|
|
|
|
|
142
|
|
|
$this->finder = new PathFinder($structure); |
|
143
|
|
|
|
|
144
|
|
|
return $this; |
|
|
|
|
|
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Define a lista de caminho de templates |
|
149
|
|
|
* |
|
150
|
|
|
* @param string $path |
|
151
|
|
|
* @return mixed |
|
152
|
|
|
*/ |
|
153
|
|
|
public function setPaths(array $list) : StructureDirectory |
|
154
|
|
|
{ |
|
155
|
|
|
$this->pathList = $list; |
|
156
|
|
|
|
|
157
|
|
|
return $this; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Retorna a lista de caminhos definidos dentro do projeto |
|
162
|
|
|
* |
|
163
|
|
|
* @return void |
|
164
|
|
|
*/ |
|
165
|
|
|
public function getPaths() : array |
|
166
|
|
|
{ |
|
167
|
|
|
return $this->pathList; |
|
|
|
|
|
|
168
|
|
|
} |
|
169
|
|
|
} |