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