1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BrainExe\Core\Redis\Command; |
4
|
|
|
|
5
|
|
|
use BrainExe\Core\Traits\RedisTrait; |
6
|
|
|
use Symfony\Component\Console\Command\Command as SymfonyCommand; |
7
|
|
|
use BrainExe\Core\Annotations\Command as CommandAnnotation; |
8
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
10
|
|
|
use Symfony\Component\Console\Input\InputOption; |
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @CommandAnnotation("Redis.Command.Import") |
15
|
|
|
* @codeCoverageIgnore |
16
|
|
|
*/ |
17
|
|
|
class Import extends SymfonyCommand |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
use RedisTrait; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* {@inheritdoc} |
24
|
|
|
*/ |
25
|
|
|
protected function configure() |
26
|
|
|
{ |
27
|
|
|
$this->setName('redis:import') |
28
|
|
|
->setDescription('Import Redis database') |
29
|
|
|
->addArgument('file', InputArgument::OPTIONAL, 'File to export', 'database.txt') |
30
|
|
|
->addOption('flush', null, InputOption::VALUE_NONE, 'Flush the current database before import'); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritdoc} |
35
|
|
|
*/ |
36
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
37
|
|
|
{ |
38
|
|
|
$file = $input->getArgument('file'); |
39
|
|
|
$content = file_get_contents($file); |
40
|
|
|
|
41
|
|
|
if ($input->getOption('flush')) { |
42
|
|
|
$this->getRedis()->flushdb(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$this->handleImport($content); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param string $content |
50
|
|
|
*/ |
51
|
|
|
protected function handleImport(string $content) |
52
|
|
|
{ |
53
|
|
|
$redis = $this->getRedis(); |
54
|
|
|
|
55
|
|
|
foreach (explode("\n", $content) as $line) { |
56
|
|
|
preg_match_all('/"(?:\\\\.|[^\\\\"])*"|\S+/', $line, $matches); |
57
|
|
|
if (empty($matches[0])) { |
58
|
|
|
continue; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
foreach ($matches[0] as &$value) { |
62
|
|
|
$value = trim($value, '"'); |
63
|
|
|
$value = str_replace('\"', '"', $value); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$redis->executeRaw($matches[0]); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|