1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Funivan\Cs\Fs\FileFinder; |
4
|
|
|
|
5
|
|
|
use Funivan\Cs\Fs\File; |
6
|
|
|
use Funivan\Cs\Fs\FilesCollection; |
7
|
|
|
use Symfony\Component\Finder\Finder; |
8
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
9
|
|
|
use Symfony\Component\Process\Process; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* |
13
|
|
|
*/ |
14
|
|
|
class StandardFileFinder implements FileFinderInterface { |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @param FinderParameters $finderParameters |
18
|
|
|
* @return FilesCollection |
19
|
|
|
* @internal param FinderParameters $params |
20
|
|
|
*/ |
21
|
|
|
public function findFiles(FinderParameters $finderParameters) { |
22
|
|
|
$filesCollection = new FilesCollection(); |
23
|
|
|
|
24
|
|
|
if ($finderParameters->get('dir')) { |
|
|
|
|
25
|
|
|
$dir = $finderParameters->get('dir'); |
26
|
|
|
|
27
|
|
|
$files = (new Finder())->in($dir)->files(); |
28
|
|
|
|
29
|
|
|
/** @var SplFileInfo $file */ |
30
|
|
|
foreach ($files as $file) { |
31
|
|
|
$filesCollection->add(new File($file->getRealPath(), File::STATUS_UNKNOWN)); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return $filesCollection; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$baseDir = $finderParameters->get('baseDir'); |
38
|
|
|
if (empty($baseDir)) { |
39
|
|
|
throw new \RuntimeException('Can not find files. Invalid baseDir finder parameter.'); |
40
|
|
|
} |
41
|
|
|
$baseDir = rtrim($baseDir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; |
42
|
|
|
if (!empty($commitHash)) { |
|
|
|
|
43
|
|
|
$process = new Process('git diff-tree --no-commit-id --name-status -r ' . $commitHash); |
44
|
|
|
} else { |
45
|
|
|
$process = new Process('git diff --name-status ; git diff --cached --name-status'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
$process->run(); |
50
|
|
|
if (!$process->isSuccessful()) { |
51
|
|
|
return $filesCollection; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
$filesList = array_filter(explode("\n", $process->getOutput())); |
56
|
|
|
|
57
|
|
|
$statusMap = [ |
58
|
|
|
'A' => File::STATUS_ADDED, |
59
|
|
|
'C' => File::STATUS_COPIED, |
60
|
|
|
'M' => File::STATUS_MODIFIED, |
61
|
|
|
'R' => File::STATUS_RENAMED, |
62
|
|
|
'D' => File::STATUS_DELETED, |
63
|
|
|
]; |
64
|
|
|
|
65
|
|
|
foreach ($filesList as $fileInfo) { |
66
|
|
|
|
67
|
|
|
preg_match('!^([^\s]+)\s+(.+)$!', $fileInfo, $matchedFileInfo); |
68
|
|
|
if (empty($matchedFileInfo[2])) { |
69
|
|
|
continue; |
70
|
|
|
} |
71
|
|
|
$gitStatus = trim($matchedFileInfo[1]); |
72
|
|
|
|
73
|
|
|
$status = !empty($statusMap[$gitStatus]) ? $statusMap[$gitStatus] : File::STATUS_UNKNOWN; |
74
|
|
|
|
75
|
|
|
$fullPath = $baseDir . ltrim($matchedFileInfo[2], DIRECTORY_SEPARATOR); |
76
|
|
|
$filesCollection->add(new File($fullPath, $status)); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $filesCollection; |
80
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
} |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: