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