Skip to content

Commit

Permalink
Feat: Add support for DSN (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
RikudouSage committed Jan 19, 2024
1 parent a8941a5 commit c4a88d4
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 6 deletions.
17 changes: 14 additions & 3 deletions README.md
Expand Up @@ -24,7 +24,14 @@ Requires php 7.3 or newer.
## Basic usage

First configure the basic parameters, these three are mandatory:
First configure the basic parameters, either using a DSN or as separate parameters:

```yaml
unleash_symfony_client:
dsn: http://localhost:4242/api&instance_id=myCoolApp-Server1&app_name=myCoolApp
```

or

```yaml
unleash_symfony_client:
Expand Down Expand Up @@ -512,8 +519,12 @@ This is the autogenerated config dump (by running `php bin/console config:dump u

```yaml
# Default configuration for extension with alias: "unleash_symfony_client"
# Default configuration for extension with alias: "unleash_symfony_client"
unleash_symfony_client:

# You can provide the connection details as a DSN instead of app_url, instance_id and app_name. DSN takes precedence over individual parameters.
dsn: null # Example: 'https://localhost:4242/api?instance_id=myCoolApp-Server1&app_name=myCoolApp'

# The application api URL
app_url: null

Expand All @@ -536,8 +547,8 @@ unleash_symfony_client:
# The http client service, must implement the Psr\Http\Client\ClientInterface or Symfony\Contracts\HttpClient\HttpClientInterface interface
http_client_service: psr18.http_client

# The request factory service, must implement the Psr\Http\Message\RequestFactoryInterface interface
request_factory_service: nyholm.psr7.psr17_factory
# The request factory service, must implement the Psr\Http\Message\RequestFactoryInterface interface. Providing null means autodetect between supported default services.
request_factory_service: null

# The cache service, must implement the Psr\SimpleCache\CacheInterface or Psr\Cache\CacheItemPoolInterface interface
cache_service: cache.app
Expand Down
5 changes: 5 additions & 0 deletions src/DependencyInjection/Configuration.php
Expand Up @@ -33,6 +33,11 @@ public function getConfigTreeBuilder(): TreeBuilder
$rootNode
->addDefaultsIfNotSet()
->children()
->scalarNode('dsn')
->info('You can provide the connection details as a DSN instead of app_url, instance_id and app_name. DSN takes precedence over individual parameters.')
->example('https://localhost:4242/api?instance_id=myCoolApp-Server1&app_name=myCoolApp')
->defaultNull()
->end()
->scalarNode('app_url')
->info('The application api URL')
->defaultNull()
Expand Down
42 changes: 39 additions & 3 deletions src/DependencyInjection/UnleashClientExtension.php
Expand Up @@ -39,9 +39,19 @@ public function load(array $configs, ContainerBuilder $container): void
'request_factory_service' => $configs['request_factory_service'],
'cache_service' => $configs['cache_service'],
]);
$container->setParameter('unleash.client.internal.app_url', $configs['app_url'] ?? '');
$container->setParameter('unleash.client.internal.instance_id', $configs['instance_id'] ?? '');
$container->setParameter('unleash.client.internal.app_name', $configs['app_name'] ?? '');

$dsn = $configs['dsn'] ?? null;
if ($dsn !== null) {
$details = $this->parseDsn($dsn);
$container->setParameter('unleash.client.internal.app_url', $details['url'] ?? '');
$container->setParameter('unleash.client.internal.instance_id', $details['instanceId'] ?? '');
$container->setParameter('unleash.client.internal.app_name', $details['appName'] ?? '');
} else {
$container->setParameter('unleash.client.internal.app_url', $configs['app_url'] ?? '');
$container->setParameter('unleash.client.internal.instance_id', $configs['instance_id'] ?? '');
$container->setParameter('unleash.client.internal.app_name', $configs['app_name'] ?? '');
}

$container->setParameter('unleash.client.internal.cache_ttl', $configs['cache_ttl']);
$container->setParameter('unleash.client.internal.metrics_send_interval', $configs['metrics_send_interval']);
$container->setParameter('unleash.client.internal.metrics_enabled', $configs['metrics_enabled']);
Expand Down Expand Up @@ -100,4 +110,30 @@ private function getDefaultStrategyHandlers(ContainerBuilder $container): array

return $result;
}

/**
* @return array{url: string|null, instanceId: string|null, appName: string|null}
*/
private function parseDsn(string $dsn): array
{
$query = parse_url($dsn, PHP_URL_QUERY);
assert(is_string($query));
$instanceUrl = str_replace("?{$query}", '', $dsn);
if (str_contains($instanceUrl, '%3F')) {
$instanceUrl = urldecode($instanceUrl);
}
parse_str($query, $queryParts);

$instanceId = $queryParts['instance_id'] ?? null;
$appName = $queryParts['app_name'] ?? null;

assert(is_string($instanceId));
assert(is_string($appName));

return [
'url' => $instanceUrl,
'instanceId' => $instanceId,
'appName' => $appName,
];
}
}

0 comments on commit c4a88d4

Please sign in to comment.