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\DependencyInjection; |
13
|
|
|
|
14
|
|
|
use GeoIp2\Database\Reader; |
15
|
|
|
use GpsLab\Bundle\GeoIP2Bundle\Command\DownloadDatabaseCommand; |
16
|
|
|
use GpsLab\Bundle\GeoIP2Bundle\Command\UpdateDatabaseCommand; |
17
|
|
|
use GpsLab\Bundle\GeoIP2Bundle\Downloader\Downloader; |
18
|
|
|
use GpsLab\Bundle\GeoIP2Bundle\Downloader\MaxMindDownloader; |
19
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
20
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
21
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
22
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
23
|
|
|
|
24
|
|
|
class GpsLabGeoIP2Extension extends Extension |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* Pattern of database service name. |
28
|
|
|
*/ |
29
|
|
|
private const SERVICE_NAME = 'geoip2.database.%s_reader'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param array[] $configs |
33
|
|
|
* @param ContainerBuilder $container |
34
|
|
|
*/ |
35
|
6 |
|
public function load(array $configs, ContainerBuilder $container): void |
36
|
|
|
{ |
37
|
6 |
|
$configuration = $this->getConfiguration($configs, $container); |
38
|
6 |
|
$config = $this->processConfiguration($configuration, $configs); |
39
|
|
|
|
40
|
6 |
|
$default_database = $config['default_database']; |
41
|
6 |
|
$databases = $config['databases']; |
42
|
|
|
|
43
|
|
|
// aliases for default database |
44
|
6 |
|
if (array_key_exists($default_database, $databases)) { |
45
|
3 |
|
$container->setAlias('geoip2.reader', sprintf(self::SERVICE_NAME, $default_database)); |
46
|
3 |
|
$container->setAlias(Reader::class, sprintf(self::SERVICE_NAME, $default_database)); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
// define database services |
50
|
6 |
|
foreach ($databases as $name => $database) { |
51
|
|
|
$container |
52
|
3 |
|
->setDefinition(sprintf(self::SERVICE_NAME, $name), new Definition(Reader::class)) |
53
|
3 |
|
->setPublic(true) |
54
|
3 |
|
->setLazy(true) |
55
|
3 |
|
->setArguments([ |
56
|
3 |
|
$database['path'], |
57
|
3 |
|
$database['locales'], |
58
|
|
|
]); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
// define MaxMind downloader service |
62
|
|
|
$container |
63
|
6 |
|
->setDefinition(MaxMindDownloader::class, new Definition(MaxMindDownloader::class)) |
64
|
6 |
|
->setPublic(false) |
65
|
6 |
|
->setArguments([ |
66
|
6 |
|
new Reference('filesystem'), |
67
|
6 |
|
new Reference('logger'), |
68
|
|
|
]); |
69
|
|
|
|
70
|
6 |
|
$container->setAlias(Downloader::class, MaxMindDownloader::class); |
71
|
|
|
|
72
|
|
|
// configure update database console command |
73
|
|
|
$container |
74
|
6 |
|
->setDefinition(UpdateDatabaseCommand::class, new Definition(UpdateDatabaseCommand::class)) |
75
|
6 |
|
->setPublic(false) |
76
|
6 |
|
->setArguments([ |
77
|
6 |
|
new Reference(Downloader::class), |
78
|
6 |
|
$databases, |
79
|
|
|
]) |
80
|
6 |
|
->addTag('console.command'); |
81
|
|
|
|
82
|
|
|
// configure download database console command |
83
|
|
|
$container |
84
|
6 |
|
->setDefinition(DownloadDatabaseCommand::class, new Definition(DownloadDatabaseCommand::class)) |
85
|
6 |
|
->setPublic(false) |
86
|
6 |
|
->setArguments([ |
87
|
6 |
|
new Reference(Downloader::class), |
88
|
|
|
]) |
89
|
6 |
|
->addTag('console.command'); |
90
|
6 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param array[] $config |
94
|
|
|
* @param ContainerBuilder $container |
95
|
|
|
* |
96
|
|
|
* @return Configuration |
97
|
|
|
*/ |
98
|
6 |
|
public function getConfiguration(array $config, ContainerBuilder $container): Configuration |
99
|
|
|
{ |
100
|
6 |
|
$cache_dir = null; |
101
|
|
|
|
102
|
6 |
|
if ($container->hasParameter('kernel.cache_dir')) { |
103
|
4 |
|
$cache_dir = $container->getParameter('kernel.cache_dir'); |
104
|
|
|
} |
105
|
|
|
|
106
|
6 |
|
return new Configuration(is_string($cache_dir) ? $cache_dir : null); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
1 |
|
public function getAlias(): string |
113
|
|
|
{ |
114
|
1 |
|
return 'gpslab_geoip'; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|