2016-08-12 19:34:27 +01:00
< ? 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\RuntimeException ;
2016-09-20 21:49:22 +01:00
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException ;
2016-08-12 19:34:27 +01:00
/**
* @ author Nicolas Grekas < p @ tchwork . com >
*/
class EnvPlaceholderParameterBag extends ParameterBag
{
private $envPlaceholders = array ();
/**
* { @ 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 ])) {
2016-10-11 04:25:54 +01:00
foreach ( $this -> envPlaceholders [ $env ] as $placeholder ) {
return $placeholder ; // return first result
}
2016-08-12 19:34:27 +01:00
}
if ( preg_match ( '/\W/' , $env )) {
throw new InvalidArgumentException ( sprintf ( 'Invalid %s name: only "word" characters are allowed.' , $name ));
}
if ( $this -> has ( $name )) {
$defaultValue = parent :: get ( $name );
2016-11-22 12:01:02 +00:00
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 ));
2016-08-12 19:34:27 +01:00
}
}
2016-10-11 04:25:54 +01:00
$uniqueName = md5 ( $name . uniqid ( mt_rand (), true ));
$placeholder = sprintf ( 'env_%s_%s' , $env , $uniqueName );
$this -> envPlaceholders [ $env ][ $placeholder ] = $placeholder ;
return $placeholder ;
2016-08-12 19:34:27 +01:00
}
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 )
{
2016-10-13 13:42:55 +01:00
if ( $newPlaceholders = $bag -> getEnvPlaceholders ()) {
$this -> envPlaceholders += $newPlaceholders ;
foreach ( $newPlaceholders as $env => $placeholders ) {
$this -> envPlaceholders [ $env ] += $placeholders ;
}
}
2016-08-12 19:34:27 +01:00
}
2016-11-08 08:48:20 +00:00
/**
* { @ inheritdoc }
*/
public function resolve ()
{
if ( $this -> resolved ) {
return ;
}
parent :: resolve ();
foreach ( $this -> envPlaceholders as $env => $placeholders ) {
2017-08-12 10:16:19 +01:00
if ( ! $this -> has ( $name = " env( $env ) " )) {
2016-11-08 08:48:20 +00:00
continue ;
}
if ( is_numeric ( $default = $this -> parameters [ $name ])) {
$this -> parameters [ $name ] = ( string ) $default ;
2016-11-22 12:01:02 +00:00
} 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 )));
2016-11-08 08:48:20 +00:00
}
}
}
2016-08-12 19:34:27 +01:00
}