1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Storeman\IndexBuilder; |
4
|
|
|
|
5
|
|
|
use Storeman\Exception; |
6
|
|
|
use Storeman\Hash\HashContainer; |
7
|
|
|
use Storeman\Index\Index; |
8
|
|
|
use Storeman\Index\IndexObject; |
9
|
|
|
use Symfony\Component\Finder\Finder; |
10
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
11
|
|
|
|
12
|
|
|
class StandardIndexBuilder implements IndexBuilderInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* {@inheritdoc} |
16
|
|
|
*/ |
17
|
|
|
public function buildIndex(string $path, array $excludedPathsRegexp = []): Index |
18
|
|
|
{ |
19
|
|
|
if (!file_exists($path)) |
20
|
|
|
{ |
21
|
|
|
throw new \InvalidArgumentException("Given path '{$path}' does not exist."); |
22
|
|
|
} |
23
|
|
|
elseif (!is_dir($path)) |
24
|
|
|
{ |
25
|
|
|
throw new \InvalidArgumentException("Given path '{$path}' is not a directory."); |
26
|
|
|
} |
27
|
|
|
elseif (!is_readable($path)) |
28
|
|
|
{ |
29
|
|
|
throw new \InvalidArgumentException("Given directory '{$path}' is not readable.'"); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
$finder = new Finder(); |
33
|
|
|
$finder->in($path); |
34
|
|
|
$finder->ignoreDotFiles(false); |
35
|
|
|
|
36
|
|
|
foreach ($excludedPathsRegexp as $excludedPathRegexp) |
37
|
|
|
{ |
38
|
|
|
$finder->notPath($excludedPathRegexp); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$index = new Index(); |
42
|
|
|
|
43
|
|
|
foreach ($finder->directories() as $fileInfo) |
44
|
|
|
{ |
45
|
|
|
/** @var SplFileInfo $fileInfo */ |
46
|
|
|
|
47
|
|
|
if ($indexObject = $this->buildIndexObject($path, $fileInfo->getRelativePathname())) |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
$index->addObject($indexObject); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
foreach ($finder->files() as $fileInfo) |
54
|
|
|
{ |
55
|
|
|
/** @var SplFileInfo $fileInfo */ |
56
|
|
|
|
57
|
|
|
if ($indexObject = $this->buildIndexObject($path, $fileInfo->getRelativePathname())) |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
$index->addObject($indexObject); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $index; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
|
|
public function buildIndexObject(string $basePath, string $relativePath): ?IndexObject |
70
|
|
|
{ |
71
|
|
|
$absolutePath = rtrim($basePath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $relativePath; |
72
|
|
|
|
73
|
|
|
clearstatcache(null, $absolutePath); |
74
|
|
|
|
75
|
|
|
if (!($stat = @lstat($absolutePath))) |
76
|
|
|
{ |
77
|
|
|
throw new Exception("lstat() failed for {$absolutePath}"); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$size = $linkTarget = $hashContainer = null; |
81
|
|
|
|
82
|
|
|
switch ($stat['mode'] & 0xF000) |
83
|
|
|
{ |
84
|
|
|
case 0x4000: |
|
|
|
|
85
|
|
|
|
86
|
|
|
$type = IndexObject::TYPE_DIR; |
87
|
|
|
|
88
|
|
|
break; |
89
|
|
|
|
90
|
|
|
case 0x8000: |
|
|
|
|
91
|
|
|
|
92
|
|
|
$type = IndexObject::TYPE_FILE; |
93
|
|
|
$size = $stat['size']; |
94
|
|
|
$hashContainer = new HashContainer(); |
95
|
|
|
|
96
|
|
|
break; |
97
|
|
|
|
98
|
|
|
case 0xA000: |
|
|
|
|
99
|
|
|
|
100
|
|
|
$type = IndexObject::TYPE_LINK; |
101
|
|
|
$linkTarget = readlink($absolutePath); |
102
|
|
|
|
103
|
|
|
if ($linkTarget === false) |
104
|
|
|
{ |
105
|
|
|
// todo: log |
106
|
|
|
|
107
|
|
|
// silently ignore broken links |
108
|
|
|
return null; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
break; |
112
|
|
|
|
113
|
|
|
default: |
|
|
|
|
114
|
|
|
|
115
|
|
|
// sockets, pipes, etc. |
116
|
|
|
return null; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return new IndexObject($relativePath, $type, $stat['mtime'], $stat['ctime'], $stat['mode'], $size, $stat['ino'], $linkTarget, null, $hashContainer); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.