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
|
|
|
|
21
|
|
|
class UpdateDatabaseCommand extends Command |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var Stopwatch |
25
|
|
|
*/ |
26
|
|
|
private $stopwatch; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var CompressorInterface |
30
|
|
|
*/ |
31
|
|
|
private $compressor; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var Filesystem |
35
|
|
|
*/ |
36
|
|
|
private $fs; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
private $url = ''; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
private $cache = ''; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param Filesystem $fs |
50
|
|
|
* @param Stopwatch $stopwatch |
51
|
|
|
* @param CompressorInterface $compressor |
52
|
|
|
* @param string $url |
53
|
|
|
* @param string $cache |
54
|
|
|
*/ |
55
|
|
|
public function __construct(Filesystem $fs, Stopwatch $stopwatch, CompressorInterface $compressor, $url, $cache) |
56
|
|
|
{ |
57
|
|
|
$this->fs = $fs; |
58
|
|
|
$this->url = $url; |
59
|
|
|
$this->cache = $cache; |
60
|
|
|
$this->stopwatch = $stopwatch; |
61
|
|
|
$this->compressor = $compressor; |
62
|
|
|
|
63
|
|
|
parent::__construct(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
protected function configure() |
67
|
|
|
{ |
68
|
|
|
$this |
69
|
|
|
->setName('geoip2:update') |
70
|
|
|
->setDescription('Downloads and update the GeoIP2 database') |
71
|
|
|
->addArgument( |
72
|
|
|
'url', |
73
|
|
|
InputArgument::OPTIONAL, |
74
|
|
|
'URL to downloaded GeoIP2 database', |
75
|
|
|
$this->url |
76
|
|
|
) |
77
|
|
|
->addArgument( |
78
|
|
|
'target', |
79
|
|
|
InputArgument::OPTIONAL, |
80
|
|
|
'Target download path', |
81
|
|
|
$this->cache |
82
|
|
|
) |
83
|
|
|
; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param InputInterface $input |
88
|
|
|
* @param OutputInterface $output |
89
|
|
|
* |
90
|
|
|
* @return int |
91
|
|
|
*/ |
92
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
93
|
|
|
{ |
94
|
|
|
$url = $input->getArgument('url'); |
95
|
|
|
$target = $input->getArgument('target'); |
96
|
|
|
|
97
|
|
|
$io = new SymfonyStyle($input, $output); |
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
|
|
|
$ch = curl_init(); |
107
|
|
|
curl_setopt_array($ch, [ |
108
|
|
|
CURLOPT_FILE => fopen($tmp, 'wb'), |
109
|
|
|
CURLOPT_TIMEOUT => 28800, |
110
|
|
|
CURLOPT_URL => $url |
111
|
|
|
]); |
112
|
|
|
curl_exec($ch); |
113
|
|
|
|
114
|
|
|
$io->comment('Download complete'); |
115
|
|
|
$io->comment('De-compressing file'); |
116
|
|
|
|
117
|
|
|
$this->compressor->uncompress($tmp, $target); |
118
|
|
|
$this->fs->chmod($target, 0777); |
119
|
|
|
|
120
|
|
|
$io->comment('Decompression complete'); |
121
|
|
|
$io->success('Finished downloading.'); |
122
|
|
|
|
123
|
|
|
$event = $this->stopwatch->stop('update'); |
124
|
|
|
$io->writeln([ |
125
|
|
|
sprintf('Time: <info>%.2F</info> s.', $event->getDuration() / 1000), |
126
|
|
|
sprintf('Memory: <info>%.2F</info> MiB.', $event->getMemory() / 1024 / 1024), |
127
|
|
|
]); |
128
|
|
|
|
129
|
|
|
return 0; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|