1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpSpellcheck\Source; |
6
|
|
|
|
7
|
|
|
use PhpSpellcheck\Exception\RuntimeException; |
8
|
|
|
use PhpSpellcheck\Text; |
9
|
|
|
|
10
|
|
|
class Directory implements SourceInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
private $dirPath; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string|null |
19
|
|
|
*/ |
20
|
|
|
private $pattern; |
21
|
2 |
|
|
22
|
|
|
public function __construct(string $dirPath, ?string $pattern = null) |
23
|
2 |
|
{ |
24
|
2 |
|
$this->dirPath = $dirPath; |
25
|
2 |
|
$this->pattern = $pattern; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param array<mixed> $context |
30
|
2 |
|
* |
31
|
|
|
* @return iterable<Text> |
32
|
2 |
|
*/ |
33
|
2 |
|
public function toTexts(array $context): iterable |
34
|
2 |
|
{ |
35
|
2 |
|
foreach ($this->getContents() as $text) { |
36
|
|
|
yield new Text($text->getContent(), array_merge($text->getContext(), $context)); |
37
|
|
|
} |
38
|
2 |
|
} |
39
|
1 |
|
|
40
|
|
|
/** |
41
|
|
|
* @return iterable<Text> |
42
|
|
|
*/ |
43
|
2 |
|
private function getContents(): iterable |
44
|
2 |
|
{ |
45
|
1 |
|
$filesInDir = new \RecursiveIteratorIterator( |
46
|
1 |
|
new \RecursiveDirectoryIterator( |
47
|
|
|
$this->dirPath, |
48
|
1 |
|
\FilesystemIterator::SKIP_DOTS | \FilesystemIterator::CURRENT_AS_PATHNAME |
49
|
|
|
), |
50
|
|
|
\RecursiveIteratorIterator::SELF_FIRST |
51
|
2 |
|
); |
52
|
2 |
|
|
53
|
|
|
if ($this->pattern !== null) { |
54
|
|
|
$filesInDir = new \RegexIterator($filesInDir, $this->pattern, \RegexIterator::GET_MATCH); |
55
|
2 |
|
} |
56
|
|
|
|
57
|
2 |
|
/** @var array<string>|\SplFileInfo|string $file */ |
58
|
|
|
foreach ($filesInDir as $file) { |
59
|
2 |
|
if (\is_string($file)) { |
60
|
2 |
|
$file = new \SplFileInfo($file); |
61
|
2 |
|
} elseif (\is_array($file) && !empty($file)) { |
62
|
2 |
|
// When regex pattern is used, an array containing the file path in its first element is returned |
63
|
2 |
|
$file = new \SplFileInfo(current($file)); |
64
|
|
|
} else { |
65
|
|
|
throw new RuntimeException(\Safe\sprintf('Couldn\'t create "%s" object from the given file', \SplFileInfo::class)); |
|
|
|
|
66
|
2 |
|
} |
67
|
|
|
|
68
|
|
|
if (!$file->isDir() && $file->getRealPath() !== false) { |
69
|
|
|
yield from (new File($file->getRealPath()))->toTexts(); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.