|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Maestriam\FileSystem\Foundation\Drive; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use Maestriam\FileSystem\Foundation\Drive\StructureDirectory; |
|
7
|
|
|
|
|
8
|
|
|
class PathFinder |
|
9
|
|
|
{ |
|
10
|
|
|
private array $structureDirectory; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Pesquisa um |
|
14
|
|
|
* |
|
15
|
|
|
* @param array $structure |
|
16
|
|
|
*/ |
|
17
|
|
|
public function __construct(array $structure) |
|
18
|
|
|
{ |
|
19
|
|
|
$this->setStructureDirectory($structure); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Define a estrutura de diretórios baseada em templates |
|
24
|
|
|
* |
|
25
|
|
|
* @param array $structure |
|
26
|
|
|
* @return void |
|
27
|
|
|
*/ |
|
28
|
|
|
private function setStructureDirectory(array $structure) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->structureDirectory = $structure; |
|
31
|
|
|
|
|
32
|
|
|
return $this; |
|
|
|
|
|
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Retorna a estrutura de diretórios, baseada em templates |
|
37
|
|
|
* |
|
38
|
|
|
* @return array |
|
39
|
|
|
*/ |
|
40
|
|
|
private function getStructureDirectory() : array |
|
41
|
|
|
{ |
|
42
|
|
|
return $this->structureDirectory; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Retorna o sub-diretório que o arquivo deverá ser armazenado, |
|
47
|
|
|
* de acordo com o tipo de template especificado |
|
48
|
|
|
* |
|
49
|
|
|
* @param string $template |
|
50
|
|
|
* @return string |
|
51
|
|
|
*/ |
|
52
|
|
|
public function findByTemplate(string $template) : string |
|
53
|
|
|
{ |
|
54
|
|
|
$subfolder = $this->getSpecificSubFolder($template); |
|
55
|
|
|
|
|
56
|
|
|
if ($subfolder) { |
|
57
|
|
|
return $subfolder; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$subfolder = $this->getDefaultSubFolder($template); |
|
61
|
|
|
|
|
62
|
|
|
if ($subfolder) { |
|
63
|
|
|
return $subfolder; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
throw new \Exception($template); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Verifica se o template tem algum diretório específico definido |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $template |
|
73
|
|
|
* @return string|null |
|
74
|
|
|
*/ |
|
75
|
|
|
private function getSpecificSubFolder(string $template) : ?string |
|
76
|
|
|
{ |
|
77
|
|
|
$structure = $this->getStructureDirectory(); |
|
78
|
|
|
|
|
79
|
|
|
return $structure[$template] ?? null; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Verifica se o template tem algum diretório padrão definido |
|
84
|
|
|
* |
|
85
|
|
|
* @param string $requested |
|
86
|
|
|
* @return string|null |
|
87
|
|
|
*/ |
|
88
|
|
|
private function getDefaultSubFolder(string $requested) : ?string |
|
89
|
|
|
{ |
|
90
|
|
|
$structure = $this->getStructureDirectory(); |
|
91
|
|
|
|
|
92
|
|
|
foreach($structure as $key => $path) |
|
93
|
|
|
{ |
|
94
|
|
|
if ($key == '*') { |
|
95
|
|
|
return $path; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
$pattern = "/{$key}/"; |
|
99
|
|
|
|
|
100
|
|
|
if (preg_match($pattern, $requested)) { |
|
101
|
|
|
return $path; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return null; |
|
106
|
|
|
} |
|
107
|
|
|
} |