Same filename in this branch
- 10 core/lib/Drupal/Component/Utility/Variable.php
- 10 core/modules/migrate_drupal/src/Plugin/migrate/source/Variable.php
Same filename and directory in other branches
- 8.9.x core/lib/Drupal/Component/Utility/Variable.php
- 9 core/lib/Drupal/Component/Utility/Variable.php
Namespace
Drupal\Component\Utility
File
core/lib/Drupal/Component/Utility/Variable.php
View source
<?php
namespace Drupal\Component\Utility;
class Variable {
public static function callableToString($callable) : string {
if ($callable instanceof \Closure) {
return '[closure]';
}
elseif (is_array($callable) && $callable) {
if (is_object($callable[0])) {
$callable[0] = get_class($callable[0]);
}
return implode('::', $callable);
}
elseif (is_string($callable)) {
return $callable;
}
else {
return '[unknown]';
}
}
public static function export($var, $prefix = '') {
if (is_array($var)) {
if (empty($var)) {
$output = 'array()';
}
else {
$output = "array(\n";
$export_keys = array_values($var) != $var;
foreach ($var as $key => $value) {
$output .= ' ' . ($export_keys ? static::export($key) . ' => ' : '') . static::export($value, ' ') . ",\n";
}
$output .= ')';
}
}
elseif (is_bool($var)) {
$output = $var ? 'TRUE' : 'FALSE';
}
elseif (is_string($var)) {
if (str_contains($var, "\n") || str_contains($var, "'")) {
$var = str_replace([
'\\',
'$',
'"',
"\n",
"\r",
"\t",
], [
'\\\\',
'\\$',
'\\"',
'\\n',
'\\r',
'\\t',
], $var);
$output = '"' . $var . '"';
}
else {
$output = "'" . $var . "'";
}
}
elseif (is_object($var) && get_class($var) === 'stdClass') {
$output = '(object) ' . static::export((array) $var, $prefix);
}
else {
$output = var_export($var, TRUE);
}
if ($prefix) {
$output = str_replace("\n", "\n{$prefix}", $output);
}
return $output;
}
}
Classes
Name |
Description |
Variable |
Provides helpers for dealing with variables. |