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
|
|
|
return $this->scan($item, $pattern); |
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if ($pattern == null || preg_match($pattern, $item)) { |
|
|
|
|
57
|
|
|
$this->add($item); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Adiciona o caminho de um arquivo para a lista de resultado |
64
|
|
|
* |
65
|
|
|
* @param string $file |
66
|
|
|
* @return int |
67
|
|
|
*/ |
68
|
|
|
private function add(string $file) : int |
69
|
|
|
{ |
70
|
|
|
return array_push($this->result, $file); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Retorna a lista de arquivos encontrados |
75
|
|
|
* |
76
|
|
|
* @return array |
77
|
|
|
*/ |
78
|
|
|
public function result() : array |
79
|
|
|
{ |
80
|
|
|
return $this->result; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Retorna o caminho utilizado para a pesquisa |
85
|
|
|
* |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
|
|
private function path() : string |
89
|
|
|
{ |
90
|
|
|
return $this->path; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Define o caminho para a pesquisa |
95
|
|
|
* |
96
|
|
|
* @param string $path |
97
|
|
|
* @return FileSearch |
98
|
|
|
*/ |
99
|
|
|
private function setPath(string $path) : FileSearch |
100
|
|
|
{ |
101
|
|
|
if (! is_dir($path)) { |
102
|
|
|
throw new Exception('Path not found.'); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$this->path = $path; |
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
} |
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.