Augmentation vers version 3.3.0
This commit is contained in:
123
vendor/symfony/dependency-injection/ParameterBag/EnvPlaceholderParameterBag.php
vendored
Normal file
123
vendor/symfony/dependency-injection/ParameterBag/EnvPlaceholderParameterBag.php
vendored
Normal file
@@ -0,0 +1,123 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\DependencyInjection\ParameterBag;
|
||||
|
||||
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
|
||||
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
|
||||
|
||||
/**
|
||||
* @author Nicolas Grekas <p@tchwork.com>
|
||||
*/
|
||||
class EnvPlaceholderParameterBag extends ParameterBag
|
||||
{
|
||||
private $envPlaceholders = [];
|
||||
private $providedTypes = [];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function get($name)
|
||||
{
|
||||
if (0 === strpos($name, 'env(') && ')' === substr($name, -1) && 'env()' !== $name) {
|
||||
$env = substr($name, 4, -1);
|
||||
|
||||
if (isset($this->envPlaceholders[$env])) {
|
||||
foreach ($this->envPlaceholders[$env] as $placeholder) {
|
||||
return $placeholder; // return first result
|
||||
}
|
||||
}
|
||||
if (!preg_match('/^(?:\w++:)*+\w++$/', $env)) {
|
||||
throw new InvalidArgumentException(sprintf('Invalid %s name: only "word" characters are allowed.', $name));
|
||||
}
|
||||
|
||||
if ($this->has($name)) {
|
||||
$defaultValue = parent::get($name);
|
||||
|
||||
if (null !== $defaultValue && !is_scalar($defaultValue)) {
|
||||
throw new RuntimeException(sprintf('The default value of an env() parameter must be scalar or null, but "%s" given to "%s".', \gettype($defaultValue), $name));
|
||||
}
|
||||
}
|
||||
|
||||
$uniqueName = md5($name.uniqid(mt_rand(), true));
|
||||
$placeholder = sprintf('env_%s_%s', str_replace(':', '_', $env), $uniqueName);
|
||||
$this->envPlaceholders[$env][$placeholder] = $placeholder;
|
||||
|
||||
return $placeholder;
|
||||
}
|
||||
|
||||
return parent::get($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the map of env vars used in the resolved parameter values to their placeholders.
|
||||
*
|
||||
* @return string[][] A map of env var names to their placeholders
|
||||
*/
|
||||
public function getEnvPlaceholders()
|
||||
{
|
||||
return $this->envPlaceholders;
|
||||
}
|
||||
|
||||
/**
|
||||
* Merges the env placeholders of another EnvPlaceholderParameterBag.
|
||||
*/
|
||||
public function mergeEnvPlaceholders(self $bag)
|
||||
{
|
||||
if ($newPlaceholders = $bag->getEnvPlaceholders()) {
|
||||
$this->envPlaceholders += $newPlaceholders;
|
||||
|
||||
foreach ($newPlaceholders as $env => $placeholders) {
|
||||
$this->envPlaceholders[$env] += $placeholders;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Maps env prefixes to their corresponding PHP types.
|
||||
*/
|
||||
public function setProvidedTypes(array $providedTypes)
|
||||
{
|
||||
$this->providedTypes = $providedTypes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the PHP types corresponding to env() parameter prefixes.
|
||||
*
|
||||
* @return string[][]
|
||||
*/
|
||||
public function getProvidedTypes()
|
||||
{
|
||||
return $this->providedTypes;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function resolve()
|
||||
{
|
||||
if ($this->resolved) {
|
||||
return;
|
||||
}
|
||||
parent::resolve();
|
||||
|
||||
foreach ($this->envPlaceholders as $env => $placeholders) {
|
||||
if (!$this->has($name = "env($env)")) {
|
||||
continue;
|
||||
}
|
||||
if (is_numeric($default = $this->parameters[$name])) {
|
||||
$this->parameters[$name] = (string) $default;
|
||||
} elseif (null !== $default && !is_scalar($default)) {
|
||||
throw new RuntimeException(sprintf('The default value of env parameter "%s" must be scalar or null, %s given.', $env, \gettype($default)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -28,7 +28,7 @@ class FrozenParameterBag extends ParameterBag
|
||||
*
|
||||
* @param array $parameters An array of parameters
|
||||
*/
|
||||
public function __construct(array $parameters = array())
|
||||
public function __construct(array $parameters = [])
|
||||
{
|
||||
$this->parameters = $parameters;
|
||||
$this->resolved = true;
|
||||
|
||||
@@ -22,13 +22,15 @@ use Symfony\Component\DependencyInjection\Exception\RuntimeException;
|
||||
*/
|
||||
class ParameterBag implements ParameterBagInterface
|
||||
{
|
||||
protected $parameters = array();
|
||||
protected $parameters = [];
|
||||
protected $resolved = false;
|
||||
|
||||
private $normalizedNames = [];
|
||||
|
||||
/**
|
||||
* @param array $parameters An array of parameters
|
||||
*/
|
||||
public function __construct(array $parameters = array())
|
||||
public function __construct(array $parameters = [])
|
||||
{
|
||||
$this->add($parameters);
|
||||
}
|
||||
@@ -38,7 +40,7 @@ class ParameterBag implements ParameterBagInterface
|
||||
*/
|
||||
public function clear()
|
||||
{
|
||||
$this->parameters = array();
|
||||
$this->parameters = [];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -49,7 +51,7 @@ class ParameterBag implements ParameterBagInterface
|
||||
public function add(array $parameters)
|
||||
{
|
||||
foreach ($parameters as $key => $value) {
|
||||
$this->parameters[strtolower($key)] = $value;
|
||||
$this->set($key, $value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,14 +68,14 @@ class ParameterBag implements ParameterBagInterface
|
||||
*/
|
||||
public function get($name)
|
||||
{
|
||||
$name = strtolower($name);
|
||||
$name = $this->normalizeName($name);
|
||||
|
||||
if (!array_key_exists($name, $this->parameters)) {
|
||||
if (!\array_key_exists($name, $this->parameters)) {
|
||||
if (!$name) {
|
||||
throw new ParameterNotFoundException($name);
|
||||
}
|
||||
|
||||
$alternatives = array();
|
||||
$alternatives = [];
|
||||
foreach ($this->parameters as $key => $parameterValue) {
|
||||
$lev = levenshtein($name, $key);
|
||||
if ($lev <= \strlen($name) / 3 || false !== strpos($key, $name)) {
|
||||
@@ -81,7 +83,23 @@ class ParameterBag implements ParameterBagInterface
|
||||
}
|
||||
}
|
||||
|
||||
throw new ParameterNotFoundException($name, null, null, null, $alternatives);
|
||||
$nonNestedAlternative = null;
|
||||
if (!\count($alternatives) && false !== strpos($name, '.')) {
|
||||
$namePartsLength = array_map('strlen', explode('.', $name));
|
||||
$key = substr($name, 0, -1 * (1 + array_pop($namePartsLength)));
|
||||
while (\count($namePartsLength)) {
|
||||
if ($this->has($key)) {
|
||||
if (\is_array($this->get($key))) {
|
||||
$nonNestedAlternative = $key;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
$key = substr($key, 0, -1 * (1 + array_pop($namePartsLength)));
|
||||
}
|
||||
}
|
||||
|
||||
throw new ParameterNotFoundException($name, null, null, null, $alternatives, $nonNestedAlternative);
|
||||
}
|
||||
|
||||
return $this->parameters[$name];
|
||||
@@ -95,7 +113,7 @@ class ParameterBag implements ParameterBagInterface
|
||||
*/
|
||||
public function set($name, $value)
|
||||
{
|
||||
$this->parameters[strtolower($name)] = $value;
|
||||
$this->parameters[$this->normalizeName($name)] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -103,7 +121,7 @@ class ParameterBag implements ParameterBagInterface
|
||||
*/
|
||||
public function has($name)
|
||||
{
|
||||
return array_key_exists(strtolower($name), $this->parameters);
|
||||
return \array_key_exists($this->normalizeName($name), $this->parameters);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -113,7 +131,7 @@ class ParameterBag implements ParameterBagInterface
|
||||
*/
|
||||
public function remove($name)
|
||||
{
|
||||
unset($this->parameters[strtolower($name)]);
|
||||
unset($this->parameters[$this->normalizeName($name)]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -125,7 +143,7 @@ class ParameterBag implements ParameterBagInterface
|
||||
return;
|
||||
}
|
||||
|
||||
$parameters = array();
|
||||
$parameters = [];
|
||||
foreach ($this->parameters as $key => $value) {
|
||||
try {
|
||||
$value = $this->resolveValue($value);
|
||||
@@ -153,18 +171,18 @@ class ParameterBag implements ParameterBagInterface
|
||||
* @throws ParameterCircularReferenceException if a circular reference if detected
|
||||
* @throws RuntimeException when a given parameter has a type problem
|
||||
*/
|
||||
public function resolveValue($value, array $resolving = array())
|
||||
public function resolveValue($value, array $resolving = [])
|
||||
{
|
||||
if (\is_array($value)) {
|
||||
$args = array();
|
||||
$args = [];
|
||||
foreach ($value as $k => $v) {
|
||||
$args[$this->resolveValue($k, $resolving)] = $this->resolveValue($v, $resolving);
|
||||
$args[\is_string($k) ? $this->resolveValue($k, $resolving) : $k] = $this->resolveValue($v, $resolving);
|
||||
}
|
||||
|
||||
return $args;
|
||||
}
|
||||
|
||||
if (!\is_string($value)) {
|
||||
if (!\is_string($value) || 2 > \strlen($value)) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
@@ -177,52 +195,52 @@ class ParameterBag implements ParameterBagInterface
|
||||
* @param string $value The string to resolve
|
||||
* @param array $resolving An array of keys that are being resolved (used internally to detect circular references)
|
||||
*
|
||||
* @return string The resolved string
|
||||
* @return mixed The resolved string
|
||||
*
|
||||
* @throws ParameterNotFoundException if a placeholder references a parameter that does not exist
|
||||
* @throws ParameterCircularReferenceException if a circular reference if detected
|
||||
* @throws RuntimeException when a given parameter has a type problem
|
||||
*/
|
||||
public function resolveString($value, array $resolving = array())
|
||||
public function resolveString($value, array $resolving = [])
|
||||
{
|
||||
// we do this to deal with non string values (Boolean, integer, ...)
|
||||
// as the preg_replace_callback throw an exception when trying
|
||||
// a non-string in a parameter value
|
||||
if (preg_match('/^%([^%\s]+)%$/', $value, $match)) {
|
||||
$key = strtolower($match[1]);
|
||||
$key = $match[1];
|
||||
$lcKey = strtolower($key); // strtolower() to be removed in 4.0
|
||||
|
||||
if (isset($resolving[$key])) {
|
||||
if (isset($resolving[$lcKey])) {
|
||||
throw new ParameterCircularReferenceException(array_keys($resolving));
|
||||
}
|
||||
|
||||
$resolving[$key] = true;
|
||||
$resolving[$lcKey] = true;
|
||||
|
||||
return $this->resolved ? $this->get($key) : $this->resolveValue($this->get($key), $resolving);
|
||||
}
|
||||
|
||||
$self = $this;
|
||||
|
||||
return preg_replace_callback('/%%|%([^%\s]+)%/', function ($match) use ($self, $resolving, $value) {
|
||||
return preg_replace_callback('/%%|%([^%\s]+)%/', function ($match) use ($resolving, $value) {
|
||||
// skip %%
|
||||
if (!isset($match[1])) {
|
||||
return '%%';
|
||||
}
|
||||
|
||||
$key = strtolower($match[1]);
|
||||
if (isset($resolving[$key])) {
|
||||
$key = $match[1];
|
||||
$lcKey = strtolower($key); // strtolower() to be removed in 4.0
|
||||
if (isset($resolving[$lcKey])) {
|
||||
throw new ParameterCircularReferenceException(array_keys($resolving));
|
||||
}
|
||||
|
||||
$resolved = $self->get($key);
|
||||
$resolved = $this->get($key);
|
||||
|
||||
if (!\is_string($resolved) && !is_numeric($resolved)) {
|
||||
throw new RuntimeException(sprintf('A string value must be composed of strings and/or numbers, but found parameter "%s" of type %s inside string value "%s".', $key, \gettype($resolved), $value));
|
||||
}
|
||||
|
||||
$resolved = (string) $resolved;
|
||||
$resolving[$key] = true;
|
||||
$resolving[$lcKey] = true;
|
||||
|
||||
return $self->isResolved() ? $resolved : $self->resolveString($resolved, $resolving);
|
||||
return $this->isResolved() ? $resolved : $this->resolveString($resolved, $resolving);
|
||||
}, $value);
|
||||
}
|
||||
|
||||
@@ -241,7 +259,7 @@ class ParameterBag implements ParameterBagInterface
|
||||
}
|
||||
|
||||
if (\is_array($value)) {
|
||||
$result = array();
|
||||
$result = [];
|
||||
foreach ($value as $k => $v) {
|
||||
$result[$k] = $this->escapeValue($v);
|
||||
}
|
||||
@@ -262,7 +280,7 @@ class ParameterBag implements ParameterBagInterface
|
||||
}
|
||||
|
||||
if (\is_array($value)) {
|
||||
$result = array();
|
||||
$result = [];
|
||||
foreach ($value as $k => $v) {
|
||||
$result[$k] = $this->unescapeValue($v);
|
||||
}
|
||||
@@ -272,4 +290,18 @@ class ParameterBag implements ParameterBagInterface
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
private function normalizeName($name)
|
||||
{
|
||||
if (isset($this->normalizedNames[$normalizedName = strtolower($name)])) {
|
||||
$normalizedName = $this->normalizedNames[$normalizedName];
|
||||
if ((string) $name !== $normalizedName) {
|
||||
@trigger_error(sprintf('Parameter names will be made case sensitive in Symfony 4.0. Using "%s" instead of "%s" is deprecated since Symfony 3.4.', $name, $normalizedName), E_USER_DEPRECATED);
|
||||
}
|
||||
} else {
|
||||
$normalizedName = $this->normalizedNames[$normalizedName] = (string) $name;
|
||||
}
|
||||
|
||||
return $normalizedName;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,6 +55,13 @@ interface ParameterBagInterface
|
||||
*/
|
||||
public function get($name);
|
||||
|
||||
/**
|
||||
* Removes a parameter.
|
||||
*
|
||||
* @param string $name The parameter name
|
||||
*/
|
||||
public function remove($name);
|
||||
|
||||
/**
|
||||
* Sets a service container parameter.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user