1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* GpsLab component. |
4
|
|
|
* |
5
|
|
|
* @author Peter Gribanov <[email protected]> |
6
|
|
|
* @copyright Copyright (c) 2017, Peter Gribanov |
7
|
|
|
* @license http://opensource.org/licenses/MIT |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace GpsLab\Bundle\GeoIP2Bundle\Command; |
11
|
|
|
|
12
|
|
|
use GpsLab\Component\Compressor\CompressorInterface; |
13
|
|
|
use Symfony\Component\Console\Command\Command; |
14
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
15
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
16
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
17
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
18
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
19
|
|
|
use Symfony\Component\Stopwatch\Stopwatch; |
20
|
|
|
use Symfony\Component\Stopwatch\StopwatchEvent; |
21
|
|
|
|
22
|
|
|
class UpdateDatabaseCommand extends Command |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var Stopwatch |
26
|
|
|
*/ |
27
|
|
|
private $stopwatch; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var CompressorInterface |
31
|
|
|
*/ |
32
|
|
|
private $compressor; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var Filesystem |
36
|
|
|
*/ |
37
|
|
|
private $fs; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
private $url = ''; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
private $cache = ''; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param Filesystem $fs |
51
|
|
|
* @param Stopwatch $stopwatch |
52
|
|
|
* @param CompressorInterface $compressor |
53
|
|
|
* @param string $url |
54
|
|
|
* @param string $cache |
55
|
|
|
*/ |
56
|
|
|
public function __construct(Filesystem $fs, Stopwatch $stopwatch, CompressorInterface $compressor, $url, $cache) |
57
|
|
|
{ |
58
|
|
|
$this->fs = $fs; |
59
|
|
|
$this->url = $url; |
60
|
|
|
$this->cache = $cache; |
61
|
|
|
$this->stopwatch = $stopwatch; |
62
|
|
|
$this->compressor = $compressor; |
63
|
|
|
|
64
|
|
|
parent::__construct(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
protected function configure() |
68
|
|
|
{ |
69
|
|
|
$this |
70
|
|
|
->setName('geoip2:update') |
71
|
|
|
->setDescription('Downloads and update the GeoIP2 database') |
72
|
|
|
->addArgument( |
73
|
|
|
'url', |
74
|
|
|
InputArgument::OPTIONAL, |
75
|
|
|
'URL to downloaded GeoIP2 database', |
76
|
|
|
$this->url |
77
|
|
|
) |
78
|
|
|
->addArgument( |
79
|
|
|
'target', |
80
|
|
|
InputArgument::OPTIONAL, |
81
|
|
|
'Target download path', |
82
|
|
|
$this->cache |
83
|
|
|
) |
84
|
|
|
; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param InputInterface $input |
89
|
|
|
* @param OutputInterface $output |
90
|
|
|
* |
91
|
|
|
* @return int |
92
|
|
|
*/ |
93
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
94
|
|
|
{ |
95
|
|
|
$io = new SymfonyStyle($input, $output); |
96
|
|
|
$url = $input->getArgument('url'); |
97
|
|
|
$target = $input->getArgument('target'); |
98
|
|
|
|
99
|
|
|
$io->title('Update the GeoIP2 database'); |
100
|
|
|
$this->stopwatch->start('update'); |
101
|
|
|
|
102
|
|
|
$tmp = sys_get_temp_dir().basename($target); |
103
|
|
|
|
104
|
|
|
$io->comment(sprintf('Beginning download of file: %s', $url)); |
105
|
|
|
|
106
|
|
|
$this->download($url, $tmp); |
107
|
|
|
|
108
|
|
|
$io->comment('Download complete'); |
109
|
|
|
$io->comment('De-compressing file'); |
110
|
|
|
|
111
|
|
|
$this->compressor->uncompress($tmp, $target); |
112
|
|
|
$this->fs->chmod($target, 0777); |
113
|
|
|
|
114
|
|
|
$io->comment('Decompression complete'); |
115
|
|
|
$io->success('Finished downloading.'); |
116
|
|
|
|
117
|
|
|
$this->stopwatch($io, $this->stopwatch->stop('update')); |
118
|
|
|
|
119
|
|
|
return 0; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param string $url |
124
|
|
|
* @param string $target |
125
|
|
|
*/ |
126
|
|
|
private function download($url, $target) |
127
|
|
|
{ |
128
|
|
|
$ch = curl_init(); |
129
|
|
|
curl_setopt_array($ch, [ |
130
|
|
|
CURLOPT_FILE => fopen($target, 'wb'), |
131
|
|
|
CURLOPT_TIMEOUT => 28800, |
132
|
|
|
CURLOPT_URL => $url, |
133
|
|
|
]); |
134
|
|
|
curl_exec($ch); |
135
|
|
|
curl_close($ch); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param SymfonyStyle $io |
140
|
|
|
* @param StopwatchEvent $event |
141
|
|
|
*/ |
142
|
|
|
private function stopwatch(SymfonyStyle $io, StopwatchEvent $event) |
143
|
|
|
{ |
144
|
|
|
$io->writeln([ |
145
|
|
|
sprintf('Time: <info>%.2F</info> s.', $event->getDuration() / 1000), |
146
|
|
|
sprintf('Memory: <info>%.2F</info> MiB.', $event->getMemory() / 1024 / 1024), |
147
|
|
|
]); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|