1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace UCD\Console\Application\Command; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Helper\ProgressBar; |
6
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
7
|
|
|
use Symfony\Component\Console\Input\InputDefinition; |
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
10
|
|
|
|
11
|
|
|
use UCD\Unicode\Character\Repository; |
12
|
|
|
use UCD\Unicode\Character\WritableRepository; |
13
|
|
|
use UCD\Exception\InvalidArgumentException; |
14
|
|
|
use UCD\Infrastructure\Repository\CharacterRepository\XMLRepository; |
15
|
|
|
|
16
|
|
|
class RepositoryTransferCommand extends RepositoryUtilisingCommand implements \SplObserver |
17
|
|
|
{ |
18
|
|
|
const COMMAND_NAME = 'repository-transfer'; |
19
|
|
|
const ARGUMENT_FROM = 'from'; |
20
|
|
|
const ARGUMENT_TO = 'to'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var ProgressBar |
24
|
|
|
*/ |
25
|
|
|
protected $progress; |
26
|
|
|
|
27
|
|
|
protected function configure() |
28
|
|
|
{ |
29
|
|
|
$this->setName(self::COMMAND_NAME); |
30
|
|
|
$this->setDescription('Reads characters from one repository and adds them to another'); |
31
|
|
|
$this->setDefinition($this->createInputDefinition()); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param InputInterface $input |
36
|
|
|
* @param OutputInterface $output |
37
|
|
|
* @return int |
38
|
|
|
* @throws InvalidArgumentException |
39
|
|
|
*/ |
40
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
41
|
|
|
{ |
42
|
|
|
$start = microtime(true); |
43
|
|
|
$from = $input->getArgument(self::ARGUMENT_FROM); |
44
|
|
|
$to = $input->getArgument(self::ARGUMENT_TO); |
45
|
|
|
|
46
|
|
|
if ($from === $to) { |
47
|
|
|
throw new InvalidArgumentException('Repositories must differ'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$source = $this->getRepositoryByName($from); |
51
|
|
|
$destination = $this->getWritableRepositoryByName($to); |
52
|
|
|
$this->setupProgressBar($output, $source, $destination); |
53
|
|
|
|
54
|
|
|
$characters = $source->getAll(); |
55
|
|
|
$destination->addMany($characters); |
56
|
|
|
|
57
|
|
|
$this->tearDownProgressBar(); |
58
|
|
|
|
59
|
|
|
$output->writeln(''); |
60
|
|
|
$output->writeln('<info>Database Generated</info>'); |
61
|
|
|
$output->writeln(sprintf('Memory peak: %.5f MB', memory_get_peak_usage() / 1048576)); |
62
|
|
|
$output->writeln(sprintf('Took: %.5f seconds', microtime(true) - $start)); |
63
|
|
|
|
64
|
|
|
return 0; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param OutputInterface $output |
69
|
|
|
* @param Repository $source |
70
|
|
|
* @param WritableRepository $destination |
71
|
|
|
*/ |
72
|
|
|
private function setupProgressBar(OutputInterface $output, Repository $source, WritableRepository $destination) |
73
|
|
|
{ |
74
|
|
|
$this->progress = new ProgressBar($output, count($source)); |
75
|
|
|
$this->progress->setMessage('Generating database...'); |
76
|
|
|
$this->progress->start(); |
77
|
|
|
|
78
|
|
|
$destination->attach($this); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
private function tearDownProgressBar() |
82
|
|
|
{ |
83
|
|
|
$this->progress->finish(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return InputDefinition |
88
|
|
|
*/ |
89
|
|
|
private function createInputDefinition() |
90
|
|
|
{ |
91
|
|
|
$readNamesList = implode(', ', $this->getRepositoryNames()); |
92
|
|
|
$writeNamesList = implode(', ', $this->getWritableRepositoryNames()); |
93
|
|
|
|
94
|
|
|
$from = new InputArgument( |
95
|
|
|
self::ARGUMENT_FROM, |
96
|
|
|
InputArgument::REQUIRED, |
97
|
|
|
sprintf('Repository from which the characters should be retrieved. Choose from: %s', $readNamesList) |
98
|
|
|
); |
99
|
|
|
|
100
|
|
|
$to = new InputArgument( |
101
|
|
|
self::ARGUMENT_TO, |
102
|
|
|
InputArgument::REQUIRED, |
103
|
|
|
sprintf('Repository to which the characters should be added. Choose from: %s', $writeNamesList) |
104
|
|
|
); |
105
|
|
|
|
106
|
|
|
return new InputDefinition([$from, $to]); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param \SplSubject $subject |
111
|
|
|
*/ |
112
|
|
|
public function update(\SplSubject $subject) |
113
|
|
|
{ |
114
|
|
|
if ($subject instanceof WritableRepository) { |
115
|
|
|
$this->progress->setProgress(count($subject)); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
} |