-
Notifications
You must be signed in to change notification settings - Fork 121
/
Copy pathCollector.php
138 lines (114 loc) · 3.64 KB
/
Collector.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php declare(strict_types = 1);
namespace TheSeer\phpDox\Collector;
use TheSeer\DirectoryScanner\DirectoryScanner;
use TheSeer\phpDox\Collector\Backend\BackendInterface;
use TheSeer\phpDox\Collector\Backend\ParseErrorException;
use TheSeer\phpDox\FileInfo;
use TheSeer\phpDox\ProgressLogger;
/**
* Collector processing class
*/
class Collector {
/**
* @var ProgressLogger
*/
private $logger;
/**
* @var Project
*/
private $project;
/**
* @var array
*/
private $parseErrors = [];
/**
* @var BackendInterface
*/
private $backend;
/**
* @var string
*/
private $encoding;
/**
* @var bool
*/
private $publicOnly;
/**
* @param string $encoding
* @param bool $publicOnly
*/
public function __construct(ProgressLogger $logger, Project $project, BackendInterface $backend, $encoding, $publicOnly) {
$this->logger = $logger;
$this->project = $project;
$this->backend = $backend;
$this->encoding = $encoding;
$this->publicOnly = $publicOnly;
}
public function run(DirectoryScanner $scanner): Project {
$srcDir = $this->project->getSourceDir();
$this->logger->log("Scanning directory '{$srcDir}' for files to process\n");
$iterator = new SourceFileIterator($scanner($srcDir), $srcDir, $this->encoding);
foreach ($iterator as $file) {
$needsProcessing = $this->project->addFile($file);
if (!$needsProcessing) {
$this->logger->progress('cached');
continue;
}
if (!$this->processFile($file)) {
$this->project->removeFile($file);
}
}
$this->logger->completed();
return $this->project;
}
public function hasParseErrors(): bool {
return \count($this->parseErrors) > 0;
}
public function getParseErrors(): array {
return $this->parseErrors;
}
/**
* @param FileInfo $file
*
* @throws CollectorException
* @throws \TheSeer\phpDox\ProgressLoggerException
*/
private function processFile(SourceFile $file): bool {
try {
if ($file->getSize() === 0) {
$this->logger->progress('processed');
return true;
}
$result = $this->backend->parse($file, $this->publicOnly);
if ($result->hasClasses()) {
foreach ($result->getClasses() as $class) {
$this->project->addClass($class);
}
}
if ($result->hasInterfaces()) {
foreach ($result->getInterfaces() as $interface) {
$this->project->addInterface($interface);
}
}
if ($result->hasTraits()) {
foreach ($result->getTraits() as $trait) {
$this->project->addTrait($trait);
}
}
$this->logger->progress('processed');
return true;
} catch (ParseErrorException $e) {
$previous = $e->getPrevious();
$this->parseErrors[$file->getPathname()] = \sprintf(
'%s [%s:%d]',
$previous->getMessage(),
\basename($previous->getFile()),
$previous->getLine()
);
$this->logger->progress('failed');
return false;
} catch (\Exception $e) {
throw new CollectorException('Error while processing source file', CollectorException::ProcessingError, $e, $file);
}
}
}