From ab69f178b63d67dd571d24783124cf07970ece0a Mon Sep 17 00:00:00 2001 From: Nathaniel Catchpole Date: Wed, 21 Aug 2013 08:45:23 +0100 Subject: [PATCH] Issue #2022931 by tim.plunkett, ParisLiakos: Move drupal_var_export() to \Drupal\Component\Utility. --- core/includes/utility.inc | 50 ++----------- .../lib/Drupal/Component/Utility/Variable.php | 74 +++++++++++++++++++ 2 files changed, 79 insertions(+), 45 deletions(-) create mode 100644 core/lib/Drupal/Component/Utility/Variable.php diff --git a/core/includes/utility.inc b/core/includes/utility.inc index cea75d224af..5b2719d34d8 100644 --- a/core/includes/utility.inc +++ b/core/includes/utility.inc @@ -5,6 +5,8 @@ * Miscellaneous functions. */ +use Drupal\Component\Utility\Variable; + /** * Drupal-friendly var_export(). * @@ -15,51 +17,9 @@ * * @return string * The variable exported in a way compatible to Drupal's coding standards. + * + * @deprecated Use \Drupal\Component\Utility\Variable::export(). */ function drupal_var_export($var, $prefix = '') { - if (is_array($var)) { - if (empty($var)) { - $output = 'array()'; - } - else { - $output = "array(\n"; - // Don't export keys if the array is non associative. - $export_keys = array_values($var) != $var; - foreach ($var as $key => $value) { - $output .= ' ' . ($export_keys ? drupal_var_export($key) . ' => ' : '') . drupal_var_export($value, ' ', FALSE) . ",\n"; - } - $output .= ')'; - } - } - elseif (is_bool($var)) { - $output = $var ? 'TRUE' : 'FALSE'; - } - elseif (is_string($var)) { - if (strpos($var, "\n") !== FALSE || strpos($var, "'") !== FALSE) { - // If the string contains a line break or a single quote, use the - // double quote export mode. Encode backslash and double quotes and - // transform some common control characters. - $var = str_replace(array('\\', '"', "\n", "\r", "\t"), array('\\\\', '\"', '\n', '\r', '\t'), $var); - $output = '"' . $var . '"'; - } - else { - $output = "'" . $var . "'"; - } - } - elseif (is_object($var) && get_class($var) === 'stdClass') { - // var_export() will export stdClass objects using an undefined - // magic method __set_state() leaving the export broken. This - // workaround avoids this by casting the object as an array for - // export and casting it back to an object when evaluated. - $output = '(object) ' . drupal_var_export((array) $var, $prefix); - } - else { - $output = var_export($var, TRUE); - } - - if ($prefix) { - $output = str_replace("\n", "\n$prefix", $output); - } - - return $output; + return Variable::export($var, $prefix); } diff --git a/core/lib/Drupal/Component/Utility/Variable.php b/core/lib/Drupal/Component/Utility/Variable.php new file mode 100644 index 00000000000..81378960e61 --- /dev/null +++ b/core/lib/Drupal/Component/Utility/Variable.php @@ -0,0 +1,74 @@ + $value) { + $output .= ' ' . ($export_keys ? static::export($key) . ' => ' : '') . static::export($value, ' ', FALSE) . ",\n"; + } + $output .= ')'; + } + } + elseif (is_bool($var)) { + $output = $var ? 'TRUE' : 'FALSE'; + } + elseif (is_string($var)) { + if (strpos($var, "\n") !== FALSE || strpos($var, "'") !== FALSE) { + // If the string contains a line break or a single quote, use the + // double quote export mode. Encode backslash and double quotes and + // transform some common control characters. + $var = str_replace(array('\\', '"', "\n", "\r", "\t"), array('\\\\', '\"', '\n', '\r', '\t'), $var); + $output = '"' . $var . '"'; + } + else { + $output = "'" . $var . "'"; + } + } + elseif (is_object($var) && get_class($var) === 'stdClass') { + // var_export() will export stdClass objects using an undefined + // magic method __set_state() leaving the export broken. This + // workaround avoids this by casting the object as an array for + // export and casting it back to an object when evaluated. + $output = '(object) ' . static::export((array) $var, $prefix); + } + else { + $output = var_export($var, TRUE); + } + + if ($prefix) { + $output = str_replace("\n", "\n$prefix", $output); + } + + return $output; + } + +}