|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Maestriam\FileSystem\Foundation; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
|
|
7
|
|
|
class FileSearch |
|
8
|
|
|
{ |
|
9
|
|
|
private string $path; |
|
10
|
|
|
|
|
11
|
|
|
private array $result = []; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Regras de negócio para pesquisa de arquivos dentro diretório |
|
15
|
|
|
* |
|
16
|
|
|
* @param string $path |
|
17
|
|
|
*/ |
|
18
|
|
|
public function __construct(string $path) |
|
19
|
|
|
{ |
|
20
|
|
|
$this->setPath($path); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Retorna a lista de todos os arquivos encontrados em diretório e |
|
25
|
|
|
* seus sub-diretórios. |
|
26
|
|
|
* |
|
27
|
|
|
* @return array |
|
28
|
|
|
*/ |
|
29
|
|
|
public function files(string $pattern = null) : array |
|
30
|
|
|
{ |
|
31
|
|
|
$pattern = "/". $pattern ."/"; |
|
32
|
|
|
|
|
33
|
|
|
$this->scan($this->path(), $pattern); |
|
34
|
|
|
|
|
35
|
|
|
return $this->result(); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Faz a busca de arquivos de dentro de diretórios e subdiretórios |
|
40
|
|
|
* |
|
41
|
|
|
* @param string $path |
|
42
|
|
|
* @return void |
|
43
|
|
|
*/ |
|
44
|
|
|
private function scan(string $path, string $pattern = null) |
|
45
|
|
|
{ |
|
46
|
|
|
$items = array_diff(scandir($path), array('.', '..')); |
|
47
|
|
|
|
|
48
|
|
|
foreach ($items as $item) { |
|
49
|
|
|
|
|
50
|
|
|
$item = "$path/$item"; |
|
51
|
|
|
|
|
52
|
|
|
if (is_dir($item)) { |
|
53
|
|
|
$this->scan($item, $pattern); |
|
54
|
|
|
continue; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
if ($pattern == null || preg_match($pattern, $item)) { |
|
|
|
|
|
|
58
|
|
|
$this->add($item); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Adiciona o caminho de um arquivo para a lista de resultado |
|
65
|
|
|
* |
|
66
|
|
|
* @param string $file |
|
67
|
|
|
* @return int |
|
68
|
|
|
*/ |
|
69
|
|
|
private function add(string $file) : int |
|
70
|
|
|
{ |
|
71
|
|
|
return array_push($this->result, $file); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Retorna a lista de arquivos encontrados |
|
76
|
|
|
* |
|
77
|
|
|
* @return array |
|
78
|
|
|
*/ |
|
79
|
|
|
public function result() : array |
|
80
|
|
|
{ |
|
81
|
|
|
return $this->result; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Retorna o caminho utilizado para a pesquisa |
|
86
|
|
|
* |
|
87
|
|
|
* @return string |
|
88
|
|
|
*/ |
|
89
|
|
|
private function path() : string |
|
90
|
|
|
{ |
|
91
|
|
|
return $this->path; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Define o caminho para a pesquisa |
|
96
|
|
|
* |
|
97
|
|
|
* @param string $path |
|
98
|
|
|
* @return FileSearch |
|
99
|
|
|
*/ |
|
100
|
|
|
private function setPath(string $path) : FileSearch |
|
101
|
|
|
{ |
|
102
|
|
|
if (! is_dir($path)) { |
|
103
|
|
|
throw new Exception('Path not found.'); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
$this->path = $path; |
|
107
|
|
|
return $this; |
|
108
|
|
|
} |
|
109
|
|
|
} |