Skip to content

Commit

Permalink
Remove env placeholder regexp (#422)
Browse files Browse the repository at this point in the history
fix #356

This is an internal detail that is not covered by the BC policy of Symfony and can thus break on any version
We now detect if env variable is used by checking the 3rd parameter of method ContainerBuilder::resolveEnvPlaceholders
  • Loading branch information
B-Galati authored and curry684 committed May 7, 2018
1 parent 1fc324a commit 6b3d92f
Show file tree
Hide file tree
Showing 13 changed files with 140 additions and 397 deletions.
42 changes: 35 additions & 7 deletions DependencyInjection/Configuration/RedisDsn.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@

namespace Snc\RedisBundle\DependencyInjection\Configuration;

/**
* RedisDsn
*/
class RedisDsn implements RedisDsnInterface
class RedisDsn
{
/**
* @var string
Expand Down Expand Up @@ -57,12 +54,17 @@ class RedisDsn implements RedisDsnInterface
protected $alias;

/**
* Constructor
*
* @var bool
*/
protected $isEnv;

/**
* @param string $dsn
* @param bool $isEnv
*/
public function __construct($dsn)
public function __construct($dsn, $isEnv)
{
$this->isEnv = $isEnv;
$this->dsn = $dsn;
$this->parseDsn($dsn);
}
Expand Down Expand Up @@ -135,11 +137,33 @@ public function getPersistentId()
return md5($this->dsn);
}

/**
* Return the env DSN if one exists, null otherwise
*
* @return string|null
*/
public function getEnvDsn()
{
return $this->isEnv() ? $this->dsn : null;
}

/**
* @return bool
*/
public function isEnv()
{
return $this->isEnv;
}

/**
* @return bool
*/
public function isValid()
{
if ($this->isEnv()) {
return true;
}

if (0 !== strpos($this->dsn, 'redis://')) {
return false;
}
Expand All @@ -160,6 +184,10 @@ public function isValid()
*/
protected function parseDsn($dsn)
{
if ($this->isEnv()) {
return;
}

$dsn = str_replace('redis://', '', $dsn); // remove "redis://"
if (false !== $pos = strrpos($dsn, '@')) {
// parse password
Expand Down
16 changes: 0 additions & 16 deletions DependencyInjection/Configuration/RedisDsnInterface.php

This file was deleted.

43 changes: 0 additions & 43 deletions DependencyInjection/Configuration/RedisEnvDsn.php

This file was deleted.

77 changes: 23 additions & 54 deletions DependencyInjection/SncRedisExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

use Snc\RedisBundle\DependencyInjection\Configuration\Configuration;
use Snc\RedisBundle\DependencyInjection\Configuration\RedisDsn;
use Snc\RedisBundle\DependencyInjection\Configuration\RedisDsnInterface;
use Snc\RedisBundle\DependencyInjection\Configuration\RedisEnvDsn;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
Expand All @@ -23,9 +21,6 @@
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

/**
* SncRedisExtension
*/
class SncRedisExtension extends Extension
{
/**
Expand Down Expand Up @@ -100,17 +95,13 @@ public function getXsdValidationBasePath()
*/
protected function loadClient(array $client, ContainerBuilder $container)
{
$parameterBag = $container->getParameterBag();

$dsnResolver = function ($dsn) use ($parameterBag) {
$dsn = $parameterBag->resolveValue($dsn);
$parsedDsn = new RedisDsn($dsn);

if ($parsedDsn->isValid()) {
return $parsedDsn;
$dsnResolver = function ($dsn) use ($container) {
$usedEnvs = null;
if (method_exists($container, 'resolveEnvPlaceholders')) {
$container->resolveEnvPlaceholders($dsn, null, $usedEnvs);
}

$parsedDsn = new RedisEnvDsn($dsn);
$parsedDsn = new RedisDsn($dsn, !empty($usedEnvs));

if ($parsedDsn->isValid()) {
return $parsedDsn;
Expand Down Expand Up @@ -155,7 +146,8 @@ protected function loadPredisClient(array $client, ContainerBuilder $container)

$connectionAliases = array();
$connectionCount = count($client['dsns']);
/** @var RedisDsnInterface $dsn */

/** @var RedisDsn $dsn */
foreach ($client['dsns'] as $i => $dsn) {
if (!$connectionAlias = $dsn->getAlias()) {
$connectionAlias = 1 === $connectionCount ? $client['alias'] : $client['alias'] . ($i + 1);
Expand All @@ -166,7 +158,7 @@ protected function loadPredisClient(array $client, ContainerBuilder $container)
$connection['logging'] = $client['logging'];
$connection['alias'] = $connectionAlias;

if ($dsn instanceof RedisDsn) {
if (!$dsn->isEnv()) {
if (null !== $dsn->getSocket()) {
$connection['scheme'] = 'unix';
$connection['path'] = $dsn->getSocket();
Expand All @@ -183,16 +175,16 @@ protected function loadPredisClient(array $client, ContainerBuilder $container)
}
$connection['password'] = $dsn->getPassword();
$connection['weight'] = $dsn->getWeight();

$this->loadPredisConnectionParameters($client['alias'], $connection, $container);
}
if ($dsn instanceof RedisEnvDsn) {
$this->loadEnvPredisConnectionParameters($client['alias'], $connection, $dsn->getDsn(), $container);
}

$this->loadPredisConnectionParameters($client['alias'], $connection, $container, $dsn);
}

$profile = $client['options']['profile'];
// TODO can be shared between clients?!
$profile = self::tryResolveEnvPlaceholders($client['options']['profile'], $container);
if (method_exists($container, 'resolveEnvPlaceholders')) {
$profile = $container->resolveEnvPlaceholders($profile, true);
}
$profile = !is_string($profile) ? sprintf('%.1F', $profile) : $profile;
$profileId = sprintf('snc_redis.client.%s_profile', $client['alias']);
$profileDef = new Definition(get_class(\Predis\Profile\Factory::get($profile))); // TODO get_class alternative?
Expand Down Expand Up @@ -235,34 +227,23 @@ protected function loadPredisClient(array $client, ContainerBuilder $container)
* @param string $clientAlias The client alias
* @param array $connection A connection configuration
* @param ContainerBuilder $container A ContainerBuilder instance
* @param string|null $envDsn Env DSN
*/
protected function loadPredisConnectionParameters($clientAlias, array $connection, ContainerBuilder $container)
{
$parameterId = sprintf('snc_redis.connection.%s_parameters.%s', $connection['alias'], $clientAlias);
$parameterDef = new Definition($container->getParameter('snc_redis.connection_parameters.class'));
$parameterDef->setPublic(false);
$parameterDef->addArgument($connection);
$parameterDef->addTag('snc_redis.connection_parameters', array('clientAlias' => $clientAlias));
$container->setDefinition($parameterId, $parameterDef);
}

/**
* @param string $clientAlias The client alias
* @param array $connection A connection configuration
* @param string $dsn The DSN placeholder
* @param ContainerBuilder $container A ContainerBuilder instance
*/
protected function loadEnvPredisConnectionParameters($clientAlias, array $connection, $dsn, ContainerBuilder $container)
protected function loadPredisConnectionParameters($clientAlias, array $connection, ContainerBuilder $container, RedisDsn $dsn)
{
$parametersClass = $container->getParameter('snc_redis.connection_parameters.class');

$parameterId = sprintf('snc_redis.connection.%s_parameters.%s', $connection['alias'], $clientAlias);
$parameterDef = new Definition($parametersClass);
$parameterDef->setPublic(false);
$parameterDef->setFactory(array('Snc\RedisBundle\Factory\EnvParametersFactory', 'create'));
$parameterDef->addArgument($connection);
$parameterDef->addArgument($parametersClass);
$parameterDef->addArgument($dsn);

if ($dsn->isEnv()) {
$parameterDef->setFactory(array('Snc\RedisBundle\Factory\EnvParametersFactory', 'create'));
$parameterDef->addArgument($parametersClass);
$parameterDef->addArgument($dsn->getEnvDsn());
}

$parameterDef->addTag('snc_redis.connection_parameters', array('clientAlias' => $clientAlias));
$container->setDefinition($parameterId, $parameterDef);
}
Expand Down Expand Up @@ -511,16 +492,4 @@ public function getConfiguration(array $config, ContainerBuilder $container)
{
return new Configuration($container->getParameter('kernel.debug'));
}

/**
* @internal
*/
public static function tryResolveEnvPlaceholders($value, ContainerBuilder $container)
{
if (!class_exists('Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag')) {
return $value;
}

return $container->resolveEnvPlaceholders($value, true);
}
}
Loading

0 comments on commit 6b3d92f

Please sign in to comment.