class Variable
Same name in this branch
- 11.x core/modules/migrate_drupal/src/Plugin/migrate/source/Variable.php \Drupal\migrate_drupal\Plugin\migrate\source\Variable
Same name in other branches
- 9 core/modules/migrate_drupal/src/Plugin/migrate/source/Variable.php \Drupal\migrate_drupal\Plugin\migrate\source\Variable
- 9 core/lib/Drupal/Component/Utility/Variable.php \Drupal\Component\Utility\Variable
- 8.9.x core/modules/migrate_drupal/src/Plugin/migrate/source/Variable.php \Drupal\migrate_drupal\Plugin\migrate\source\Variable
- 8.9.x core/lib/Drupal/Component/Utility/Variable.php \Drupal\Component\Utility\Variable
- 10 core/modules/migrate_drupal/src/Plugin/migrate/source/Variable.php \Drupal\migrate_drupal\Plugin\migrate\source\Variable
- 10 core/lib/Drupal/Component/Utility/Variable.php \Drupal\Component\Utility\Variable
Provides helpers for dealing with variables.
Hierarchy
- class \Drupal\Component\Utility\Variable
Expanded class hierarchy of Variable
Related topics
10 files declare their use of Variable
- Datelist.php in core/
lib/ Drupal/ Core/ Datetime/ Element/ Datelist.php - DatelistElementFormTest.php in core/
tests/ Drupal/ KernelTests/ Core/ Datetime/ DatelistElementFormTest.php - Datetime.php in core/
lib/ Drupal/ Core/ Datetime/ Element/ Datetime.php - DatetimeElementFormTest.php in core/
tests/ Drupal/ KernelTests/ Core/ Datetime/ DatetimeElementFormTest.php - DbDumpCommand.php in core/
lib/ Drupal/ Core/ Command/ DbDumpCommand.php
69 string references to 'Variable'
- action_settings.yml in core/
modules/ system/ migrations/ action_settings.yml - core/modules/system/migrations/action_settings.yml
- book_settings.yml in core/
modules/ book/ migrations/ book_settings.yml - core/modules/book/migrations/book_settings.yml
- d6_dblog_settings.yml in core/
modules/ dblog/ migrations/ d6_dblog_settings.yml - core/modules/dblog/migrations/d6_dblog_settings.yml
- d6_forum_settings.yml in core/
modules/ forum/ migrations/ d6_forum_settings.yml - core/modules/forum/migrations/d6_forum_settings.yml
- d6_language_negotiation_settings.yml in core/
modules/ language/ migrations/ d6_language_negotiation_settings.yml - core/modules/language/migrations/d6_language_negotiation_settings.yml
File
-
core/
lib/ Drupal/ Component/ Utility/ Variable.php, line 10
Namespace
Drupal\Component\UtilityView source
class Variable {
/**
* Generates a human-readable name for a callable.
*
* @param callable $callable
* A callable.
*
* @return string
* A human-readable name for the callable.
*/
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]';
}
}
/**
* Drupal-friendly var_export().
*
* @param mixed $var
* The variable to export.
* @param string $prefix
* A prefix that will be added at the beginning of every lines of the output.
*
* @return string
* The variable exported in a way compatible to Drupal's coding standards.
*/
public static function export($var, $prefix = '') {
if (is_array($var)) {
if (empty($var)) {
$output = '[]';
}
else {
$output = "[\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 ? 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, "'")) {
// If the string contains a line break or a single quote, use the
// double quote export mode. Encode backslash, dollar symbols, and
// double quotes and transform some common control characters.
$var = str_replace([
'\\',
'$',
'"',
"\n",
"\r",
"\t",
], [
'\\\\',
'\\$',
'\\"',
'\\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 {
// @todo var_export() does not use long array syntax. Fix in
// https://www.drupal.org/project/drupal/issues/3476894
$output = var_export($var, TRUE);
}
if ($prefix) {
$output = str_replace("\n", "\n{$prefix}", $output);
}
return $output;
}
}
Members
Title Sort descending | Modifiers | Object type | Summary |
---|---|---|---|
Variable::callableToString | public static | function | Generates a human-readable name for a callable. |
Variable::export | public static | function | Drupal-friendly var_export(). |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.