"Fossies" - the Fresh Open Source Software Archive

Member "drupal-9.4.5/vendor/symfony/var-dumper/Dumper/AbstractDumper.php" (20 Jul 2022, 6172 Bytes) of package /linux/www/drupal-9.4.5.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "AbstractDumper.php": 9.4.4_vs_9.4.5.

    1 <?php
    2 
    3 /*
    4  * This file is part of the Symfony package.
    5  *
    6  * (c) Fabien Potencier <fabien@symfony.com>
    7  *
    8  * For the full copyright and license information, please view the LICENSE
    9  * file that was distributed with this source code.
   10  */
   11 
   12 namespace Symfony\Component\VarDumper\Dumper;
   13 
   14 use Symfony\Component\VarDumper\Cloner\Data;
   15 use Symfony\Component\VarDumper\Cloner\DumperInterface;
   16 
   17 /**
   18  * Abstract mechanism for dumping a Data object.
   19  *
   20  * @author Nicolas Grekas <p@tchwork.com>
   21  */
   22 abstract class AbstractDumper implements DataDumperInterface, DumperInterface
   23 {
   24     public const DUMP_LIGHT_ARRAY = 1;
   25     public const DUMP_STRING_LENGTH = 2;
   26     public const DUMP_COMMA_SEPARATOR = 4;
   27     public const DUMP_TRAILING_COMMA = 8;
   28 
   29     public static $defaultOutput = 'php://output';
   30 
   31     protected $line = '';
   32     protected $lineDumper;
   33     protected $outputStream;
   34     protected $decimalPoint; // This is locale dependent
   35     protected $indentPad = '  ';
   36     protected $flags;
   37 
   38     private $charset = '';
   39 
   40     /**
   41      * @param callable|resource|string|null $output  A line dumper callable, an opened stream or an output path, defaults to static::$defaultOutput
   42      * @param string|null                   $charset The default character encoding to use for non-UTF8 strings
   43      * @param int                           $flags   A bit field of static::DUMP_* constants to fine tune dumps representation
   44      */
   45     public function __construct($output = null, string $charset = null, int $flags = 0)
   46     {
   47         $this->flags = $flags;
   48         $this->setCharset($charset ?: \ini_get('php.output_encoding') ?: \ini_get('default_charset') ?: 'UTF-8');
   49         $this->decimalPoint = \PHP_VERSION_ID >= 80000 ? '.' : localeconv()['decimal_point'];
   50         $this->setOutput($output ?: static::$defaultOutput);
   51         if (!$output && \is_string(static::$defaultOutput)) {
   52             static::$defaultOutput = $this->outputStream;
   53         }
   54     }
   55 
   56     /**
   57      * Sets the output destination of the dumps.
   58      *
   59      * @param callable|resource|string $output A line dumper callable, an opened stream or an output path
   60      *
   61      * @return callable|resource|string The previous output destination
   62      */
   63     public function setOutput($output)
   64     {
   65         $prev = $this->outputStream ?? $this->lineDumper;
   66 
   67         if (\is_callable($output)) {
   68             $this->outputStream = null;
   69             $this->lineDumper = $output;
   70         } else {
   71             if (\is_string($output)) {
   72                 $output = fopen($output, 'w');
   73             }
   74             $this->outputStream = $output;
   75             $this->lineDumper = [$this, 'echoLine'];
   76         }
   77 
   78         return $prev;
   79     }
   80 
   81     /**
   82      * Sets the default character encoding to use for non-UTF8 strings.
   83      *
   84      * @return string The previous charset
   85      */
   86     public function setCharset(string $charset)
   87     {
   88         $prev = $this->charset;
   89 
   90         $charset = strtoupper($charset);
   91         $charset = null === $charset || 'UTF-8' === $charset || 'UTF8' === $charset ? 'CP1252' : $charset;
   92 
   93         $this->charset = $charset;
   94 
   95         return $prev;
   96     }
   97 
   98     /**
   99      * Sets the indentation pad string.
  100      *
  101      * @param string $pad A string that will be prepended to dumped lines, repeated by nesting level
  102      *
  103      * @return string The previous indent pad
  104      */
  105     public function setIndentPad(string $pad)
  106     {
  107         $prev = $this->indentPad;
  108         $this->indentPad = $pad;
  109 
  110         return $prev;
  111     }
  112 
  113     /**
  114      * Dumps a Data object.
  115      *
  116      * @param callable|resource|string|true|null $output A line dumper callable, an opened stream, an output path or true to return the dump
  117      *
  118      * @return string|null The dump as string when $output is true
  119      */
  120     public function dump(Data $data, $output = null)
  121     {
  122         $this->decimalPoint = \PHP_VERSION_ID >= 80000 ? '.' : localeconv()['decimal_point'];
  123 
  124         if ($locale = $this->flags & (self::DUMP_COMMA_SEPARATOR | self::DUMP_TRAILING_COMMA) ? setlocale(\LC_NUMERIC, 0) : null) {
  125             setlocale(\LC_NUMERIC, 'C');
  126         }
  127 
  128         if ($returnDump = true === $output) {
  129             $output = fopen('php://memory', 'r+');
  130         }
  131         if ($output) {
  132             $prevOutput = $this->setOutput($output);
  133         }
  134         try {
  135             $data->dump($this);
  136             $this->dumpLine(-1);
  137 
  138             if ($returnDump) {
  139                 $result = stream_get_contents($output, -1, 0);
  140                 fclose($output);
  141 
  142                 return $result;
  143             }
  144         } finally {
  145             if ($output) {
  146                 $this->setOutput($prevOutput);
  147             }
  148             if ($locale) {
  149                 setlocale(\LC_NUMERIC, $locale);
  150             }
  151         }
  152 
  153         return null;
  154     }
  155 
  156     /**
  157      * Dumps the current line.
  158      *
  159      * @param int $depth The recursive depth in the dumped structure for the line being dumped,
  160      *                   or -1 to signal the end-of-dump to the line dumper callable
  161      */
  162     protected function dumpLine(int $depth)
  163     {
  164         ($this->lineDumper)($this->line, $depth, $this->indentPad);
  165         $this->line = '';
  166     }
  167 
  168     /**
  169      * Generic line dumper callback.
  170      */
  171     protected function echoLine(string $line, int $depth, string $indentPad)
  172     {
  173         if (-1 !== $depth) {
  174             fwrite($this->outputStream, str_repeat($indentPad, $depth).$line."\n");
  175         }
  176     }
  177 
  178     /**
  179      * Converts a non-UTF-8 string to UTF-8.
  180      *
  181      * @return string|null
  182      */
  183     protected function utf8Encode(?string $s)
  184     {
  185         if (null === $s || preg_match('//u', $s)) {
  186             return $s;
  187         }
  188 
  189         if (!\function_exists('iconv')) {
  190             throw new \RuntimeException('Unable to convert a non-UTF-8 string to UTF-8: required function iconv() does not exist. You should install ext-iconv or symfony/polyfill-iconv.');
  191         }
  192 
  193         if (false !== $c = @iconv($this->charset, 'UTF-8', $s)) {
  194             return $c;
  195         }
  196         if ('CP1252' !== $this->charset && false !== $c = @iconv('CP1252', 'UTF-8', $s)) {
  197             return $c;
  198         }
  199 
  200         return iconv('CP850', 'UTF-8', $s);
  201     }
  202 }