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
|
|
|
* @param array[] $configs |
28
|
|
|
* @param ContainerBuilder $container |
29
|
|
|
*/ |
30
|
3 |
|
public function load(array $configs, ContainerBuilder $container): void |
31
|
|
|
{ |
32
|
3 |
|
$configuration = $this->getConfiguration($configs, $container); |
33
|
3 |
|
$config = $this->processConfiguration($configuration, $configs); |
34
|
|
|
|
35
|
3 |
|
$default_database = $config['default_database']; |
36
|
|
|
|
37
|
|
|
// aliases for default database |
38
|
3 |
|
$container->setAlias('geoip2.reader', sprintf('geoip2.database.%s_reader', $default_database)); |
39
|
3 |
|
$container->setAlias(Reader::class, sprintf('geoip2.database.%s_reader', $default_database)); |
40
|
|
|
|
41
|
|
|
// define database services |
42
|
3 |
|
foreach ($config['databases'] as $name => $database) { |
43
|
|
|
$container |
44
|
3 |
|
->setDefinition(sprintf('geoip2.database.%s_reader', $name), new Definition(Reader::class)) |
45
|
3 |
|
->setPublic(true) |
46
|
3 |
|
->setLazy(true) |
47
|
3 |
|
->setArguments([ |
48
|
3 |
|
$database['path'], |
49
|
3 |
|
$database['locales'], |
50
|
|
|
]); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
// define MaxMind downloader service |
54
|
|
|
$container |
55
|
3 |
|
->setDefinition(MaxMindDownloader::class, new Definition(MaxMindDownloader::class)) |
56
|
3 |
|
->setPublic(false) |
57
|
3 |
|
->setArguments([ |
58
|
3 |
|
new Reference('filesystem'), |
59
|
3 |
|
new Reference('logger'), |
60
|
|
|
]); |
61
|
|
|
|
62
|
3 |
|
$container->setAlias(Downloader::class, MaxMindDownloader::class); |
63
|
|
|
|
64
|
|
|
// configure update database console command |
65
|
|
|
$container |
66
|
3 |
|
->setDefinition(UpdateDatabaseCommand::class, new Definition(UpdateDatabaseCommand::class)) |
67
|
3 |
|
->setPublic(false) |
68
|
3 |
|
->setArguments([ |
69
|
3 |
|
new Reference(Downloader::class), |
70
|
3 |
|
$config['databases'], |
71
|
|
|
]) |
72
|
3 |
|
->addTag('console.command'); |
73
|
|
|
|
74
|
|
|
// configure download database console command |
75
|
|
|
$container |
76
|
3 |
|
->setDefinition(DownloadDatabaseCommand::class, new Definition(DownloadDatabaseCommand::class)) |
77
|
3 |
|
->setPublic(false) |
78
|
3 |
|
->setArguments([ |
79
|
3 |
|
new Reference(Downloader::class), |
80
|
|
|
]) |
81
|
3 |
|
->addTag('console.command'); |
82
|
3 |
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param array[] $config |
86
|
|
|
* @param ContainerBuilder $container |
87
|
|
|
* |
88
|
|
|
* @return Configuration |
89
|
|
|
*/ |
90
|
3 |
|
public function getConfiguration(array $config, ContainerBuilder $container): Configuration |
91
|
|
|
{ |
92
|
3 |
|
$cache_dir = null; |
93
|
|
|
|
94
|
3 |
|
if ($container->hasParameter('kernel.cache_dir')) { |
95
|
2 |
|
$cache_dir = $container->getParameter('kernel.cache_dir'); |
96
|
|
|
} |
97
|
|
|
|
98
|
3 |
|
return new Configuration(is_string($cache_dir) ? $cache_dir : null); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
1 |
|
public function getAlias(): string |
105
|
|
|
{ |
106
|
1 |
|
return 'gpslab_geoip'; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|