You are here

class Variable in Zircon Profile 8.0

Same name in this branch
  1. 8.0 vendor/symfony/dependency-injection/Variable.php \Symfony\Component\DependencyInjection\Variable
  2. 8.0 core/lib/Drupal/Component/Utility/Variable.php \Drupal\Component\Utility\Variable
  3. 8.0 core/modules/migrate_drupal/src/Plugin/migrate/source/Variable.php \Drupal\migrate_drupal\Plugin\migrate\source\Variable
Same name and namespace in other branches
  1. 8 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

3 files declare their use of Variable
DbDumpCommand.php in core/lib/Drupal/Core/Command/DbDumpCommand.php
Contains \Drupal\Core\Command\DbDumpCommand.
KernelTestBase.php in core/modules/simpletest/src/KernelTestBase.php
Contains \Drupal\simpletest\KernelTestBase.
VariableTest.php in core/tests/Drupal/Tests/Component/Utility/VariableTest.php
Contains \Drupal\Tests\Component\Utility\VariableTest.
2 string references to 'Variable'
contact.source.schema.yml in core/modules/contact/config/schema/contact.source.schema.yml
core/modules/contact/config/schema/contact.source.schema.yml
migrate_drupal.source.schema.yml in core/modules/migrate_drupal/config/schema/migrate_drupal.source.schema.yml
core/modules/migrate_drupal/config/schema/migrate_drupal.source.schema.yml

File

core/lib/Drupal/Component/Utility/Variable.php, line 15
Contains \Drupal\Component\Utility\Variable.

Namespace

Drupal\Component\Utility
View source
class Variable {

  /**
   * 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 = '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 ? 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, dollar symbols, 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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Variable::export public static function Drupal-friendly var_export().