/**
  * This function will replace any parameters in a string with their value.
  * Parameters are defined by being prefixed by a $ character. In certain
  * situations, the parameter will be surrounded by {}, which Symphony
  * takes to mean, evaluate this parameter to a value, other times it will be
  * omitted which is usually used to indicate that this parameter exists
  *
  * @param string $value
  *  The string with the parameters that need to be evaluated
  * @param array $env
  *  The environment variables from the Frontend class which includes
  *  any params set by Symphony or Events or by other Datasources
  * @param boolean $includeParenthesis
  *  Parameters will sometimes not be surrounded by {}. If this is the case
  *  setting this parameter to false will make this function automatically add
  *  them to the parameter. By default this is true, which means all parameters
  *  in the string already are surrounded by {}
  * @param boolean $escape
  *  If set to true, the resulting value will be `urlencode`'d before being returned.
  *  By default this is false
  * @return string
  *  The string will all parameters evaluated. If a parameter was not found, it will
  *  not be replaced at all.
  */
 public function __processParametersInString($value, array $env, $includeParenthesis = true, $escape = false)
 {
     if (trim($value) == '') {
         return null;
     }
     if (!$includeParenthesis) {
         $value = '{' . $value . '}';
     }
     if (preg_match_all('@{([^}]+)}@i', $value, $matches, PREG_SET_ORDER)) {
         foreach ($matches as $match) {
             list($source, $cleaned) = $match;
             $replacement = null;
             $bits = preg_split('/:/', $cleaned, -1, PREG_SPLIT_NO_EMPTY);
             foreach ($bits as $param) {
                 if ($param[0] != '$') {
                     $replacement = $param;
                     break;
                 }
                 $param = trim($param, '$');
                 $replacement = Datasource::findParameterInEnv($param, $env);
                 if (is_array($replacement)) {
                     $replacement = array_map(array('Datasource', 'escapeCommas'), $replacement);
                     if (count($replacement) > 1) {
                         $replacement = implode(',', $replacement);
                     } else {
                         $replacement = end($replacement);
                     }
                 }
                 if (!empty($replacement)) {
                     break;
                 }
             }
             if ($escape == true) {
                 $replacement = urlencode($replacement);
             }
             $value = str_replace($source, $replacement, $value);
         }
     }
     return $value;
 }