1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Oliverde8\Component\PhpEtl\ChainOperation\Extract; |
5
|
|
|
|
6
|
|
|
use Oliverde8\Component\PhpEtl\ChainOperation\AbstractChainOperation; |
7
|
|
|
use Oliverde8\Component\PhpEtl\ChainOperation\DataChainOperationInterface; |
8
|
|
|
use Oliverde8\Component\PhpEtl\Item\DataItemInterface; |
9
|
|
|
use Oliverde8\Component\PhpEtl\Item\ExternalFileItem; |
10
|
|
|
use Oliverde8\Component\PhpEtl\Item\ItemInterface; |
11
|
|
|
use Oliverde8\Component\PhpEtl\Item\MixItem; |
12
|
|
|
use Oliverde8\Component\PhpEtl\Model\ExecutionContext; |
13
|
|
|
use Oliverde8\Component\PhpEtl\Model\File\FileSystemInterface; |
14
|
|
|
use Symfony\Component\ExpressionLanguage\ExpressionLanguage; |
15
|
|
|
|
16
|
|
|
class ExternalFileFinderOperation extends AbstractChainOperation implements DataChainOperationInterface |
17
|
|
|
{ |
18
|
|
|
protected FileSystemInterface $fileSystem; |
19
|
|
|
|
20
|
|
|
protected string $directory; |
21
|
|
|
|
22
|
|
|
private ExpressionLanguage $expressionLanguage; |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
public function __construct(FileSystemInterface $fileSystem, string $directory) |
26
|
|
|
{ |
27
|
|
|
$this->fileSystem = $fileSystem; |
28
|
|
|
$this->directory = $directory; |
29
|
|
|
|
30
|
|
|
$this->expressionLanguage = new ExpressionLanguage(); |
31
|
|
|
|
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
function processData(DataItemInterface $item, ExecutionContext $context): ItemInterface |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
$pattern = $item->getData(); |
37
|
|
|
$files = []; |
38
|
|
|
|
39
|
|
|
if (strpos($this->directory, "@") === 0) { |
40
|
|
|
$directory = ltrim($this->directory, '@'); |
41
|
|
|
$directory = $this->expressionLanguage->evaluate($directory, ['context' => $context->getParameters()]); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
foreach ($this->fileSystem->listContents($directory) as $file) { |
45
|
|
|
if (preg_match($pattern, $file) !== 0) { |
46
|
|
|
$files[] = new ExternalFileItem($directory . "/" . $file, $this->fileSystem); |
|
|
|
|
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return new MixItem($files); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.