/
Collector.php
84 lines (74 loc) · 2.71 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
<?php
namespace TheSeer\Autoload {
use TheSeer\DirectoryScanner\PHPFilterIterator;
class Collector {
/**
* @var Parser
*/
private $parser;
/**
* @var CollectorResult
*/
private $collectorResult;
/**
* @var bool
*/
private $toleranceMode;
/**
* @var bool
*/
private $mimeCheck;
/**
* @param Parser $parser
* @param bool $toleranceMode
* @param bool $mimeCheck
*/
public function __construct(Parser $parser, $toleranceMode = false, $mimeCheck = false) {
$this->parser = $parser;
$this->toleranceMode = $toleranceMode;
$this->mimeCheck = $mimeCheck;
$this->collectorResult = new CollectorResult();
}
public function getResult() {
return $this->collectorResult;
}
public function addDirectory(\Iterator $sources) {
$worker = $this->mimeCheck ? new PHPFilterIterator($sources) : $sources;
foreach($worker as $file) {
try {
$parseResult = $this->parser->parse(new SourceFile($file->getRealpath()));
if ($parseResult->hasRedeclarations() && !$this->toleranceMode) {
throw new CollectorException(
sprintf(
'The file "%s" conains duplicate (potentially conditional) definitions of the following unit(s): %s',
$file->getRealPath(),
join(', ', $parseResult->getRedeclarations())
),
CollectorException::InFileRedeclarationFound
);
}
$this->collectorResult->addParseResult($file, $parseResult);
} catch(ParserException $e) {
throw new CollectorException(
sprintf(
'Could not process file "%s" due to parse errors',
$file->getRealPath()
),
CollectorException::ParseErrror,
$e
);
} catch(CollectorResultException $e) {
throw new CollectorException(
$e->getMessage(),
CollectorException::RedeclarationFound
);
}
}
}
}
class CollectorException extends \Exception {
const ParseErrror = 1;
const RedeclarationFound = 2;
const InFileRedeclarationFound = 3;
}
}