1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Storeman\IndexBuilder; |
4
|
|
|
|
5
|
|
|
use Psr\Log\LoggerAwareInterface; |
6
|
|
|
use Psr\Log\LoggerInterface; |
7
|
|
|
use Psr\Log\NullLogger; |
8
|
|
|
use Storeman\Exception; |
9
|
|
|
use Storeman\Hash\HashContainer; |
10
|
|
|
use Storeman\Index\Index; |
11
|
|
|
use Storeman\Index\IndexObject; |
12
|
|
|
use Symfony\Component\Finder\Finder; |
13
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
14
|
|
|
|
15
|
|
|
class StandardIndexBuilder implements IndexBuilderInterface, LoggerAwareInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var LoggerInterface |
19
|
|
|
*/ |
20
|
|
|
protected $logger; |
21
|
|
|
|
22
|
|
|
public function __construct() |
23
|
|
|
{ |
24
|
|
|
$this->logger = new NullLogger(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* {@inheritdoc} |
29
|
|
|
*/ |
30
|
|
|
public function setLogger(LoggerInterface $logger): void |
31
|
|
|
{ |
32
|
|
|
$this->logger = $logger; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
*/ |
38
|
|
|
public function buildIndex(string $path, array $excludedPathsRegexp = []): Index |
39
|
|
|
{ |
40
|
|
|
$this->logger->info(sprintf("Building index using %s for path '%s' (excluded: %s)...", static::class, $path, implode(',', $excludedPathsRegexp) ?: '-')); |
41
|
|
|
|
42
|
|
|
if (!file_exists($path)) |
43
|
|
|
{ |
44
|
|
|
throw new \InvalidArgumentException("Given path '{$path}' does not exist."); |
45
|
|
|
} |
46
|
|
|
elseif (!is_dir($path)) |
47
|
|
|
{ |
48
|
|
|
throw new \InvalidArgumentException("Given path '{$path}' is not a directory."); |
49
|
|
|
} |
50
|
|
|
elseif (!is_readable($path)) |
51
|
|
|
{ |
52
|
|
|
throw new \InvalidArgumentException("Given directory '{$path}' is not readable.'"); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$finder = new Finder(); |
56
|
|
|
$finder->in($path); |
57
|
|
|
$finder->ignoreDotFiles(false); |
58
|
|
|
|
59
|
|
|
foreach ($excludedPathsRegexp as $excludedPathRegexp) |
60
|
|
|
{ |
61
|
|
|
$finder->notPath($excludedPathRegexp); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$index = new Index(); |
65
|
|
|
|
66
|
|
|
foreach ($finder->directories() as $fileInfo) |
67
|
|
|
{ |
68
|
|
|
/** @var SplFileInfo $fileInfo */ |
69
|
|
|
|
70
|
|
|
if ($indexObject = $this->buildIndexObject($path, $fileInfo->getRelativePathname())) |
|
|
|
|
71
|
|
|
{ |
72
|
|
|
$index->addObject($indexObject); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
foreach ($finder->files() as $fileInfo) |
77
|
|
|
{ |
78
|
|
|
/** @var SplFileInfo $fileInfo */ |
79
|
|
|
|
80
|
|
|
if ($indexObject = $this->buildIndexObject($path, $fileInfo->getRelativePathname())) |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
$index->addObject($indexObject); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $index; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritdoc} |
91
|
|
|
*/ |
92
|
|
|
public function buildIndexObject(string $basePath, string $relativePath): ?IndexObject |
93
|
|
|
{ |
94
|
|
|
$absolutePath = rtrim($basePath, '/') . '/' . $relativePath; |
95
|
|
|
|
96
|
|
|
clearstatcache(null, $absolutePath); |
97
|
|
|
|
98
|
|
|
if (!($stat = @lstat($absolutePath))) |
99
|
|
|
{ |
100
|
|
|
throw new Exception("lstat() failed for {$absolutePath}"); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$size = $linkTarget = $hashContainer = null; |
104
|
|
|
|
105
|
|
|
switch ($stat['mode'] & 0xF000) |
106
|
|
|
{ |
107
|
|
|
case 0x4000: |
|
|
|
|
108
|
|
|
|
109
|
|
|
$type = IndexObject::TYPE_DIR; |
110
|
|
|
|
111
|
|
|
break; |
112
|
|
|
|
113
|
|
|
case 0x8000: |
|
|
|
|
114
|
|
|
|
115
|
|
|
$type = IndexObject::TYPE_FILE; |
116
|
|
|
$size = $stat['size']; |
117
|
|
|
$hashContainer = new HashContainer(); |
118
|
|
|
|
119
|
|
|
break; |
120
|
|
|
|
121
|
|
|
case 0xA000: |
|
|
|
|
122
|
|
|
|
123
|
|
|
$type = IndexObject::TYPE_LINK; |
124
|
|
|
$linkTarget = readlink($absolutePath); |
125
|
|
|
|
126
|
|
|
if ($linkTarget === false) |
127
|
|
|
{ |
128
|
|
|
$this->logger->notice("Found broken link: {$absolutePath}"); |
129
|
|
|
|
130
|
|
|
// silently ignore broken links |
131
|
|
|
return null; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
break; |
135
|
|
|
|
136
|
|
|
default: |
|
|
|
|
137
|
|
|
|
138
|
|
|
// sockets, pipes, etc. |
139
|
|
|
return null; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return new IndexObject($relativePath, $type, $stat['mtime'], $stat['ctime'], $stat['mode'] & 0777, $size, $stat['ino'], $linkTarget, null, $hashContainer); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
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.