|
| 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; |
| 13 | + |
| 14 | +use Symfony\Component\VarDumper\Cloner\VarCloner; |
| 15 | +use Symfony\Component\VarDumper\Dumper\CliDumper; |
| 16 | +use Symfony\Component\VarDumper\Dumper\HtmlDumper; |
| 17 | + |
| 18 | +/** |
| 19 | + * @author Nicolas Grekas <p@tchwork.com> |
| 20 | + * |
| 21 | + * @method self to(callable|resource|string $output) Sets the output destination of the dump: a line callable, an opened stream or an output path. |
| 22 | + * @method self as(string $format) Sets the output format of the dump: can be "html", "cli", "text" or "env", which uses the `DUMP_AS` env var or the PHP SAPI. |
| 23 | + * @method self die() Dies after dumping. |
| 24 | + * @method self withTitle(string $title) Sets the title of the dump. |
| 25 | + * @method self withTrace(bool $trace) Enables/disables dumping the stack trace after dumping. |
| 26 | +
8000
span> * @method self withMaxItems(int $items) Limits the number of items past the first level. |
| 27 | + * @method self withMaxDepth(int $depth) Limits the depth of the dump. |
| 28 | + * @method self withMaxItemsPerDepth(int $max) Limits the number of items per depth level. |
| 29 | + * @method self withMaxStringLength(int $length) Limits the number of characters for dumped strings. |
| 30 | + * @method self withMaxStringWidth(int $width) Limits the number of characters per line for dumped strings. |
| 31 | + * @method self withRefHandles(bool $handles) Enables/disables objects' global identifiers dumping. |
| 32 | + * @method self withCasters(array $casters, bool $replace = false) Adds or replace casters for resources and objects. |
| 33 | + * @method self withFilter(int $filter) A bit field of Caster::EXCLUDE_* constants. |
| 34 | + */ |
| 35 | +class Dump |
| 36 | +{ |
| 37 | + const HTML = 'html'; |
| 38 | + const CLI = 'cli'; |
| 39 | + const TEXT = 'text'; |
| 40 | + |
| 41 | + private $vars; |
| 42 | + private $dumpOnDestruct; |
| 43 | + private $params = array( |
| 44 | + 'to' => null, |
| 45 | + 'as' => array('env'), |
| 46 | + 'die' => false, |
| 47 | +// 'withTitle' => array(''), |
| 48 | +// 'withTrace' => array(false), |
| 49 | + 'withMaxItems' => array(2500), |
| 50 | + 'withMaxDepth' => array(-1), |
| 51 | + 'withMaxItemsPerDepth' => array(-1), |
| 52 | + 'withMaxStringLength' => array(-1), |
| 53 | + 'withMaxStringWidth' => array(-1), |
| 54 | + 'withRefHandles' => array(true), |
| 55 | + 'withCasters' => array(array(), false), |
| 56 | + 'withFilter' => array(0), |
| 57 | + ); |
| 58 | + |
| 59 | + public function __construct(array $vars, $dumpOnDestruct = true) |
| 60 | + { |
| 61 | + $this->vars = $vars; |
| 62 | + $t
ED48
his->dumpOnDestruct = $dumpOnDestruct; |
| 63 | + } |
| 64 | + |
| 65 | + public function __destruct() |
| 66 | + { |
| 67 | + if ($this->dumpOnDestruct) { |
| 68 | + $this->dumpOnDestruct = false; |
| 69 | + $this->dump(); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + public function dump() |
| 74 | + { |
| 75 | + extract($this->params); |
| 76 | + |
| 77 | + if ($withCasters[1]) { |
| 78 | + $cloner = new VarCloner($withCasters[0]); |
| 79 | + } else { |
| 80 | + $cloner = new VarCloner(); |
| 81 | + $cloner->addCasters($withCasters[0]); |
| 82 | + } |
| 83 | + $cloner->setMaxItems($withMaxItems[0]); |
| 84 | + $cloner->setMaxString($withMaxStringLength[0]); |
| 85 | + |
| 86 | + $dumper = $this->getDumper($this->getFormat($as[0]), $to); |
| 87 | + $dumper->setMaxStringWidth($withMaxStringWidth[0]); |
| 88 | + |
| 89 | + foreach ($this->vars as $v) { |
| 90 | + $dumper->dump($cloner->cloneVar($v, $withFilter[0]) |
| 91 | + ->withMaxDepth($withMaxDepth[0]) |
| 92 | + ->withMaxItemsPerDepth($withMaxItemsPerDepth[0]) |
| 93 | + ->withRefHandles($withRefHandles[0]) |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | + if ($die) { |
| 98 | + exit(1); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + protected function getFormat($format) |
| 103 | + { |
| 104 | + foreach (array($format, getenv('DUMP_AS')) as $format) { |
| 105 | + $format = strtolower($format); |
| 106 | + if ('html' === $format || 'cli' === $format || 'text' === $format) { |
| 107 | + return $format; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + return 'cli' === PHP_SAPI ? 'cli' : 'html'; |
| 112 | + } |
| 113 | + |
| 114 | + protected function getDumper($format, $output) |
| 115 | + { |
| 116 | + if ('html' === $format) { |
| 117 | + return new HtmlDumper($output); |
| 118 | + } |
| 119 | + |
| 120 | + $dumper = new CliDumper($output); |
| 121 | + |
| 122 | + if ('text' === $format) { |
| 123 | + $dumper->setColors(false); |
| 124 | + } |
| 125 | + |
| 126 | + return $dumper; |
| 127 | + } |
| 128 | + |
| 129 | + public function __call($method, array $args) |
| 130 | + { |
| 131 | + if (!isset($this->params[$method])) { |
| 132 | + throw new \BadMethodCallException(sprintf('Call to undefined method %s::%s()', get_class($this), $method)); |
| 133 | + } |
| 134 | + |
| 135 | + $dump = clone $this; |
| 136 | + |
| 137 | + if ('to' === $method) { |
| 138 | + $dump->params['to'] = $args ? $args[0] : null; |
| 139 | + } elseif ('die' === $method) { |
| 140 | + $dump->params['die'] = !$args || $args[0]; |
| 141 | + } else { |
| 142 | + $params = $this->params[$method]; |
| 143 | + $numArgs = count($args); |
| 144 | + |
| 145 | + foreach ($params as $i => $p) { |
| 146 | + if ($i < $numArgs) { |
| 147 | + if (gettype($args[$i]) !== $type = gettype($p)) { |
| 148 | + settype($args[$i], $type); |
| 149 | + } |
| 150 | + $params[$i] = $args[$i]; |
| 151 | + } |
| 152 | + } |
| 153 | + $dump->params[$method] = $params; |
| 154 | + } |
| 155 | + |
| 156 | + $this->dumpOnDestruct = false; |
| 157 | + |
| 158 | + return $dump; |
| 159 | + } |
| 160 | +} |
0 commit comments