1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
use Doctrine\DBAL\DriverManager; |
5
|
|
|
use SlayerBirden\DataFlow\DataBagInterface; |
6
|
|
|
use SlayerBirden\DataFlow\Pipe\MapperCallbackInterface; |
7
|
|
|
use SlayerBirden\DataFlow\PipelineBuilder; |
8
|
|
|
use SlayerBirden\DataFlow\Plumber; |
9
|
|
|
use SlayerBirden\DataFlow\Provider\Csv; |
10
|
|
|
use SlayerBirden\DataFlow\Writer\Dbal\UpdateStrategy\UniqueIndexStrategy; |
11
|
|
|
use SlayerBirden\DataFlow\Writer\Dbal\Write; |
12
|
|
|
use SlayerBirden\DataFlow\Writer\Dbal\WriterUtility; |
13
|
|
|
|
14
|
|
|
require '../../vendor/autoload.php'; |
15
|
|
|
|
16
|
|
|
# bootstrap |
17
|
|
|
$connection = DriverManager::getConnection([ |
18
|
|
|
'url' => 'mysql://test-user:testpwd@localhost:4486/foo?charset=UTF8', |
19
|
|
|
]); |
20
|
|
|
// this is just a utility class to "cache" schema info |
21
|
|
|
$utility = new WriterUtility($connection); |
22
|
|
|
$dbWrite = new Write( |
23
|
|
|
'users_write', // pipe ID for reporting |
24
|
|
|
$connection, // DBAL connection |
25
|
|
|
'users', // db table name |
26
|
|
|
$utility, // utility class |
27
|
|
|
new UniqueIndexStrategy('users', $utility), // update or insert will depend on unique fields in the table |
28
|
|
|
$this->emitter |
29
|
|
|
); |
30
|
|
|
|
31
|
|
|
$emitter = new class implements \SlayerBirden\DataFlow\EmitterInterface |
32
|
|
|
{ |
33
|
|
|
public function emit(string $event, ...$args): void |
34
|
|
|
{ |
35
|
|
|
echo $event, ' ==> ', implode(', ', $args), PHP_EOL; |
36
|
|
|
} |
37
|
|
|
}; |
38
|
|
|
|
39
|
|
|
# pipeline |
40
|
|
|
$pipeline = (new PipelineBuilder($emitter)) |
41
|
|
|
->map('name', new class implements MapperCallbackInterface |
42
|
|
|
{ |
43
|
|
|
public function __invoke($value, DataBagInterface $dataBag) |
44
|
|
|
{ |
45
|
|
|
return $dataBag['first'] . ' ' . $dataBag['last']; |
46
|
|
|
} |
47
|
|
|
}) |
48
|
|
|
->addSection($dbWrite) |
49
|
|
|
->build(); |
50
|
|
|
|
51
|
|
|
$file = new \SplFileObject(__DIR__ . '/users.csv'); |
52
|
|
|
$file->setFlags(\SplFileObject::READ_CSV | \SplFileObject::READ_AHEAD | \SplFileObject::SKIP_EMPTY); |
53
|
|
|
|
54
|
|
|
(new Plumber(new Csv('users_file', $file), $pipeline, $emitter))->pour(); |
55
|
|
|
|