1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpUnitGen\Parser; |
4
|
|
|
|
5
|
|
|
use League\Flysystem\FilesystemInterface; |
6
|
|
|
use PhpUnitGen\Configuration\ConsoleConfigInterface; |
7
|
|
|
use PhpUnitGen\Exception\InvalidArgumentException; |
8
|
|
|
use PhpUnitGen\Exception\ParsingException; |
9
|
|
|
use PhpUnitGen\Model\DirectoryModel; |
10
|
|
|
use PhpUnitGen\Model\ModelInterface\DirectoryModelInterface; |
11
|
|
|
use PhpUnitGen\Parser\ParserInterface\DirectoryParserInterface; |
12
|
|
|
use PhpUnitGen\Parser\ParserInterface\PhpFileParserInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class DirectoryParser. |
16
|
|
|
* |
17
|
|
|
* @author Paul Thébaud <[email protected]>. |
18
|
|
|
* @copyright 2017-2018 Paul Thébaud <[email protected]>. |
19
|
|
|
* @license https://opensource.org/licenses/MIT The MIT license. |
20
|
|
|
* @link https://github.com/paul-thebaud/phpunit-generator |
21
|
|
|
* @since Class available since Release 2.0.0. |
22
|
|
|
*/ |
23
|
|
|
class DirectoryParser implements DirectoryParserInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var ConsoleConfigInterface $config The configuration to use. |
27
|
|
|
*/ |
28
|
|
|
private $config; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var FilesystemInterface $fileSystem The file system to use. |
32
|
|
|
*/ |
33
|
|
|
private $fileSystem; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var PhpFileParserInterface $phpFileParser The php file parser to use. |
37
|
|
|
*/ |
38
|
|
|
private $phpFileParser; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* DirectoryParser constructor. |
42
|
|
|
* |
43
|
|
|
* @param ConsoleConfigInterface $config A config instance. |
44
|
|
|
* @param FilesystemInterface $fileSystem A file system instance. |
45
|
|
|
* @param PhpFileParserInterface $phpFileParser A php file parser instance. |
46
|
|
|
*/ |
47
|
|
|
public function __construct( |
48
|
|
|
ConsoleConfigInterface $config, |
49
|
|
|
FilesystemInterface $fileSystem, |
50
|
|
|
PhpFileParserInterface $phpFileParser |
51
|
|
|
) { |
52
|
|
|
$this->config = $config; |
53
|
|
|
$this->fileSystem = $fileSystem; |
54
|
|
|
$this->phpFileParser = $phpFileParser; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
|
|
public function parse(string $sourceDirectory, string $targetDirectory): DirectoryModelInterface |
61
|
|
|
{ |
62
|
|
|
if (! $this->fileSystem->has($sourceDirectory) || ! $this->fileSystem->getMimetype($sourceDirectory) === 'directory') { |
63
|
|
|
throw new ParsingException(sprintf('The source directory "%s" does not exist.', $sourceDirectory)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$directoryModel = new DirectoryModel(); |
67
|
|
|
$directoryModel->setSourceDirectory($sourceDirectory); |
68
|
|
|
$directoryModel->setTargetDirectory($targetDirectory); |
69
|
|
|
|
70
|
|
|
foreach ($this->fileSystem->listContents($sourceDirectory) as $file) { |
71
|
|
|
if ($file['type'] === 'file' |
72
|
|
|
&& @preg_match($this->config->getIncludeRegex(), $file['path']) === 1 |
73
|
|
|
&& @preg_match($this->config->getExcludeRegex(), $file['path']) === 0 |
74
|
|
|
) { |
75
|
|
|
$directoryModel->addPhpFile($this->phpFileParser->parse($this->fileSystem->read($file['path']))); |
|
|
|
|
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $directoryModel; |
80
|
|
|
} |
81
|
|
|
} |