|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace hanneskod\readmetester; |
|
6
|
|
|
|
|
7
|
|
|
use hanneskod\readmetester\Config\Suite; |
|
8
|
|
|
use hanneskod\readmetester\Compiler\Pass\FilterPass; |
|
9
|
|
|
use hanneskod\readmetester\Compiler\StdinInput; |
|
10
|
|
|
use hanneskod\readmetester\Example\ExampleStoreInterface; |
|
11
|
|
|
|
|
12
|
|
|
final class ExampleProvider implements ExampleProviderInterface |
|
13
|
|
|
{ |
|
14
|
|
|
private ?StdinInput $stdin = null; |
|
15
|
|
|
|
|
16
|
|
|
public function __construct( |
|
17
|
|
|
private Compiler\CompilerFactoryFactory $compilerFactoryFactory, |
|
18
|
|
|
private Compiler\Pass\DefaultCompilerPasses $defaultPasses, |
|
19
|
|
|
private FilesystemFacade $filesystemFacade, |
|
20
|
|
|
) {} |
|
21
|
|
|
|
|
22
|
|
|
public function getExamplesForSuite(Suite $suite): ExampleStoreInterface |
|
23
|
|
|
{ |
|
24
|
|
|
$prependPasses = []; |
|
25
|
|
|
$appendPasses = []; |
|
26
|
|
|
|
|
27
|
|
|
// Setup filtering |
|
28
|
|
|
|
|
29
|
|
|
if ($filter = $suite->getFilter()) { |
|
30
|
|
|
$appendPasses[] = new FilterPass( |
|
31
|
|
|
new Utils\Regexp('/' . preg_quote($filter, '/') . '/i') |
|
32
|
|
|
); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
// Create inputs |
|
36
|
|
|
|
|
37
|
|
|
$inputs = match($suite->readFromStdin()) { |
|
38
|
|
|
true => [$this->getStdin()], |
|
39
|
|
|
false => $this->filesystemFacade->getFilesystemInput( |
|
40
|
|
|
'.', |
|
41
|
|
|
$suite->getIncludePaths(), |
|
42
|
|
|
$suite->getFileExtensions(), |
|
43
|
|
|
$suite->getExcludePaths() |
|
44
|
|
|
), |
|
45
|
|
|
}; |
|
46
|
|
|
|
|
47
|
|
|
// Create compiler and compile |
|
48
|
|
|
|
|
49
|
|
|
return $this->compilerFactoryFactory |
|
50
|
|
|
->createCompilerFactory($suite->getInputLanguage()) |
|
51
|
|
|
->createCompiler([...$prependPasses, ...$this->defaultPasses->getPasses(), ...$appendPasses]) |
|
52
|
|
|
->compile($inputs); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
private function getStdin(): StdinInput |
|
56
|
|
|
{ |
|
57
|
|
|
if (!isset($this->stdin)) { |
|
58
|
|
|
$this->stdin = new StdinInput(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return $this->stdin; |
|
|
|
|
|
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|