Skip to content

Commit

Permalink
Merge pull request #52 from peter-gribanov/no_aliases
Browse files Browse the repository at this point in the history
Do not create aliases for the default database
  • Loading branch information
peter-gribanov authored Feb 10, 2020
2 parents 01e6b4e + e4773d0 commit 5511a01
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/DependencyInjection/GpsLabGeoIP2Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@

class GpsLabGeoIP2Extension extends Extension
{
/**
* Pattern of database service name.
*/
private const SERVICE_NAME = 'geoip2.database.%s_reader';

/**
* @param array[] $configs
* @param ContainerBuilder $container
Expand All @@ -33,15 +38,18 @@ public function load(array $configs, ContainerBuilder $container): void
$config = $this->processConfiguration($configuration, $configs);

$default_database = $config['default_database'];
$databases = $config['databases'];

// aliases for default database
$container->setAlias('geoip2.reader', sprintf('geoip2.database.%s_reader', $default_database));
$container->setAlias(Reader::class, sprintf('geoip2.database.%s_reader', $default_database));
if (array_key_exists($default_database, $databases)) {
$container->setAlias('geoip2.reader', sprintf(self::SERVICE_NAME, $default_database));
$container->setAlias(Reader::class, sprintf(self::SERVICE_NAME, $default_database));
}

// define database services
foreach ($config['databases'] as $name => $database) {
foreach ($databases as $name => $database) {
$container
->setDefinition(sprintf('geoip2.database.%s_reader', $name), new Definition(Reader::class))
->setDefinition(sprintf(self::SERVICE_NAME, $name), new Definition(Reader::class))
->setPublic(true)
->setLazy(true)
->setArguments([
Expand All @@ -67,7 +75,7 @@ public function load(array $configs, ContainerBuilder $container): void
->setPublic(false)
->setArguments([
new Reference(Downloader::class),
$config['databases'],
$databases,
])
->addTag('console.command');

Expand Down
58 changes: 58 additions & 0 deletions tests/DependencyInjection/GpsLabGeoIP2ExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,64 @@ public function testLoad($cache_dir): void
]);
}

/**
* @dataProvider getCacheDirs
*
* @param string|bool|null $cache_dir
*/
public function testLoadWithEmptyConfiguration($cache_dir): void
{
$container = new ContainerBuilder();

if ($cache_dir !== null) {
$container->setParameter('kernel.cache_dir', $cache_dir);
}

$extension = new GpsLabGeoIP2Extension();
$extension->load([], $container);

$this->assertFalse($container->hasAlias('geoip2.reader'));
$this->assertFalse($container->hasAlias(Reader::class));

$this->assertTrue($container->hasAlias(Downloader::class));
$alias = $container->getAlias(Downloader::class);
$this->assertTrue($alias->isPublic());
$this->assertContains((string) $alias, [
MaxMindDownloader::class, // Symfony >= 4.0
strtolower(MaxMindDownloader::class), // Symfony < 4.0
]);

$this->assertTrue($container->hasDefinition(MaxMindDownloader::class));
$downloader = $container->getDefinition(MaxMindDownloader::class);
$this->assertFalse($downloader->isPublic()); // isPrivate() allowed in Symfony >= 3.4
$this->assertInstanceOf(Reference::class, $downloader->getArgument(0));
$this->assertSame('filesystem', (string) $downloader->getArgument(0));
$this->assertInstanceOf(Reference::class, $downloader->getArgument(1));
$this->assertSame('logger', (string) $downloader->getArgument(1));

$this->assertTrue($container->hasDefinition(UpdateDatabaseCommand::class));
$update_command = $container->getDefinition(UpdateDatabaseCommand::class);
$this->assertFalse($update_command->isPublic()); // isPrivate() allowed in Symfony >= 3.4
$this->assertTrue($update_command->hasTag('console.command'));
$this->assertInstanceOf(Reference::class, $update_command->getArgument(0));
$this->assertContains((string) $update_command->getArgument(0), [
Downloader::class, // Symfony >= 4.0
strtolower(Downloader::class), // Symfony < 4.0
]);
$this->assertIsArray($update_command->getArgument(1));
$this->assertSame([], $update_command->getArgument(1));

$this->assertTrue($container->hasDefinition(DownloadDatabaseCommand::class));
$download_command = $container->getDefinition(DownloadDatabaseCommand::class);
$this->assertFalse($download_command->isPublic()); // isPrivate() allowed in Symfony >= 3.4
$this->assertTrue($download_command->hasTag('console.command'));
$this->assertInstanceOf(Reference::class, $download_command->getArgument(0));
$this->assertContains((string) $update_command->getArgument(0), [
Downloader::class, // Symfony >= 4.0
strtolower(Downloader::class), // Symfony < 4.0
]);
}

public function testGetAlias(): void
{
$this->assertSame('gpslab_geoip', $this->extension->getAlias());
Expand Down

0 comments on commit 5511a01

Please sign in to comment.