Skip to content

Commit

Permalink
Merge pull request #58 from peter-gribanov/license_dirty_hack_for_flex
Browse files Browse the repository at this point in the history
Add dirty hack for Symfony Flex
  • Loading branch information
peter-gribanov committed Mar 26, 2020
2 parents 967e2b1 + 4fdf628 commit 3c9ba80
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/DependencyInjection/Configuration.php
Expand Up @@ -22,6 +22,8 @@ class Configuration implements ConfigurationInterface

private const PATH = '%s/%s.mmdb';

private const LICENSE_DIRTY_HACK = 'YOUR-LICENSE-KEY';

/**
* @var string
*/
Expand All @@ -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);
Expand Down Expand Up @@ -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.
*
Expand Down
25 changes: 25 additions & 0 deletions tests/DependencyInjection/ConfigurationTest.php
Expand Up @@ -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;
Expand Down

0 comments on commit 3c9ba80

Please sign in to comment.