From 4fdf62850ac827d1cfccf9ef14fde56a8bb8b864 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Wed, 25 Mar 2020 21:35:51 +0300 Subject: [PATCH] add dirty hack for Symfony Flex --- src/DependencyInjection/Configuration.php | 29 +++++++++++++++++++ .../DependencyInjection/ConfigurationTest.php | 25 ++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 9db6eae..3ab1de6 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -22,6 +22,8 @@ class Configuration implements ConfigurationInterface private const PATH = '%s/%s.mmdb'; + private const LICENSE_DIRTY_HACK = 'YOUR-LICENSE-KEY'; + /** * @var string */ @@ -45,6 +47,7 @@ public function getConfigTreeBuilder(): TreeBuilder $this->normalizeDefaultDatabase($root_node); $this->normalizeRootConfigurationToDefaultDatabase($root_node); + $this->normalizeLicenseDirtyHack($root_node); $this->validateAvailableDefaultDatabase($root_node); $this->allowGlobalLicense($root_node); $this->allowGlobalLocales($root_node); @@ -217,6 +220,32 @@ private function normalizeRootConfigurationToDefaultDatabase(NodeDefinition $roo }); } + /** + * Dirty hack for Symfony Flex. + * + * @see https://github.com/symfony/recipes-contrib/pull/837 + * + * @param NodeDefinition $root_node + */ + private function normalizeLicenseDirtyHack(NodeDefinition $root_node): void + { + $root_node + ->beforeNormalization() + ->ifTrue(static function ($v): bool { + return $v && is_array($v) && array_key_exists('databases', $v) && is_array($v['databases']); + }) + ->then(static function (array $v): array { + foreach ($v['databases'] as $name => $database) { + if (isset($database['license']) && $database['license'] === self::LICENSE_DIRTY_HACK) { + unset($v['databases'][$name]); + @trigger_error(sprintf('License for downloaded database "%s" is not specified.', $name), E_USER_WARNING); + } + } + + return $v; + }); + } + /** * Validate that the default_database exists in the list of databases. * diff --git a/tests/DependencyInjection/ConfigurationTest.php b/tests/DependencyInjection/ConfigurationTest.php index a7a7beb..214e1c3 100644 --- a/tests/DependencyInjection/ConfigurationTest.php +++ b/tests/DependencyInjection/ConfigurationTest.php @@ -331,6 +331,31 @@ public function getConfigs(): array ], 'default_database' => 'default', ]]; + + // test dirty hack for Symfony Flex + // https://github.com/symfony/recipes-contrib/pull/837 + $return[] = [$cache_dir, [ + 'gpslab_geoip' => [ + 'license' => 'YOUR-LICENSE-KEY', + ], + ], [ + 'default_database' => 'default', + 'databases' => [], + 'locales' => ['en'], + ]]; + $return[] = [$cache_dir, [ + 'gpslab_geoip' => [ + 'databases' => [ + 'default' => [ + 'license' => 'YOUR-LICENSE-KEY', + ], + ], + ], + ], [ + 'databases' => [], + 'default_database' => 'default', + 'locales' => ['en'], + ]]; } return $return;