| Total Complexity | 10 |
| Total Lines | 59 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 0 | ||
| 1 | <?php |
||
| 9 | class Directory implements SourceInterface |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * @var string |
||
| 13 | */ |
||
| 14 | private $dirPath; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @var null|string |
||
| 18 | */ |
||
| 19 | private $pattern; |
||
| 20 | |||
| 21 | 2 | public function __construct(string $dirPath, ?string $pattern = null) |
|
| 25 | 2 | } |
|
| 26 | |||
| 27 | /** |
||
| 28 | * @return Text[] |
||
| 29 | */ |
||
| 30 | 2 | public function getContents(): iterable |
|
| 31 | { |
||
| 32 | 2 | $filesInDir = new \RecursiveIteratorIterator( |
|
| 33 | 2 | new \RecursiveDirectoryIterator( |
|
| 34 | 2 | $this->dirPath, |
|
| 35 | 2 | \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::CURRENT_AS_PATHNAME |
|
| 36 | ), |
||
| 37 | \RecursiveIteratorIterator::SELF_FIRST |
||
| 38 | 2 | ); |
|
| 39 | 1 | ||
| 40 | if ($this->pattern) { |
||
| 41 | $filesInDir = new \RegexIterator($filesInDir, $this->pattern, \RegexIterator::GET_MATCH); |
||
| 42 | } |
||
| 43 | 2 | ||
| 44 | 2 | /** @var \SplFileInfo|string|array $file */ |
|
| 45 | 1 | foreach ($filesInDir as $file) { |
|
| 46 | 1 | if (is_string($file)) { |
|
| 47 | $file = new \SplFileInfo($file); |
||
| 48 | 1 | } elseif (is_array($file)) { |
|
| 49 | // When regex pattern is used, an array containing the file path in its first element is returned |
||
| 50 | $file = new \SplFileInfo(current($file)); |
||
| 51 | 2 | } |
|
| 52 | 2 | ||
| 53 | if (!$file->isDir()) { |
||
| 54 | if ($file->getRealPath() !== false) { |
||
| 55 | 2 | yield current((new File($file->getRealPath()))->toTexts()); |
|
|
|
|||
| 56 | } |
||
| 57 | 2 | } |
|
| 58 | } |
||
| 59 | 2 | } |
|
| 60 | 2 | ||
| 61 | 2 | public function toTexts(array $context): iterable |
|
| 68 | ); |
||
| 69 | } |
||
| 72 |