|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* GpsLab component. |
|
6
|
|
|
* |
|
7
|
|
|
* @author Peter Gribanov <[email protected]> |
|
8
|
|
|
* @copyright Copyright (c) 2017, Peter Gribanov |
|
9
|
|
|
* @license http://opensource.org/licenses/MIT |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace GpsLab\Bundle\GeoIP2Bundle\Command; |
|
13
|
|
|
|
|
14
|
|
|
use GpsLab\Bundle\GeoIP2Bundle\Downloader\Downloader; |
|
15
|
|
|
use Symfony\Component\Console\Command\Command; |
|
16
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
17
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
18
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
19
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
|
20
|
|
|
|
|
21
|
|
|
class DownloadDatabaseCommand extends Command |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var Downloader |
|
25
|
|
|
*/ |
|
26
|
|
|
private $downloader; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param Downloader $downloader |
|
30
|
|
|
*/ |
|
31
|
5 |
|
public function __construct(Downloader $downloader) |
|
32
|
|
|
{ |
|
33
|
5 |
|
$this->downloader = $downloader; |
|
34
|
5 |
|
parent::__construct(); |
|
35
|
5 |
|
} |
|
36
|
|
|
|
|
37
|
5 |
|
protected function configure(): void |
|
38
|
|
|
{ |
|
39
|
|
|
$this |
|
40
|
5 |
|
->setName('geoip2:download') |
|
41
|
5 |
|
->setDescription('Downloads the GeoIP2 database') |
|
42
|
5 |
|
->addArgument( |
|
43
|
5 |
|
'url', |
|
44
|
5 |
|
InputArgument::REQUIRED, |
|
45
|
5 |
|
'URL of downloaded GeoIP2 database' |
|
46
|
|
|
) |
|
47
|
5 |
|
->addArgument( |
|
48
|
5 |
|
'target', |
|
49
|
5 |
|
InputArgument::REQUIRED, |
|
50
|
5 |
|
'Target download path' |
|
51
|
|
|
) |
|
52
|
|
|
; |
|
53
|
5 |
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param InputInterface $input |
|
57
|
|
|
* @param OutputInterface $output |
|
58
|
|
|
* |
|
59
|
|
|
* @return int |
|
60
|
|
|
*/ |
|
61
|
5 |
|
protected function execute(InputInterface $input, OutputInterface $output): int |
|
62
|
|
|
{ |
|
63
|
5 |
|
$io = new SymfonyStyle($input, $output); |
|
64
|
5 |
|
$url = $input->getArgument('url'); |
|
65
|
5 |
|
$target = $input->getArgument('target'); |
|
66
|
|
|
|
|
67
|
5 |
|
if (!is_string($url)) { |
|
68
|
2 |
|
throw new \InvalidArgumentException(sprintf('URL of downloaded GeoIP2 database should be a string, got %s instead.', json_encode($url))); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
3 |
|
if (!is_string($target)) { |
|
72
|
2 |
|
throw new \InvalidArgumentException(sprintf('Target download path should be a string, got %s instead.', json_encode($target))); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
1 |
|
$io->title('Download the GeoIP2 database'); |
|
76
|
|
|
|
|
77
|
1 |
|
$this->downloader->download($url, $target); |
|
78
|
|
|
|
|
79
|
1 |
|
$io->success('Finished downloading'); |
|
80
|
|
|
|
|
81
|
1 |
|
return 0; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|