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