3 * This file is part of the Exporter package.
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
11 namespace SebastianBergmann\Exporter;
13 use SebastianBergmann\RecursionContext\Context;
16 * A nifty utility for visualizing PHP variables.
20 * use SebastianBergmann\Exporter\Exporter;
22 * $exporter = new Exporter;
23 * print $exporter->export(new Exception);
29 * Exports a value as a string
31 * The output of this method is similar to the output of print_r(), but
32 * improved in various aspects:
34 * - NULL is rendered as "null" (instead of "")
35 * - TRUE is rendered as "true" (instead of "1")
36 * - FALSE is rendered as "false" (instead of "")
37 * - Strings are always quoted with single quotes
38 * - Carriage returns and newlines are normalized to \n
39 * - Recursion and repeated rendering is treated properly
42 * @param int $indentation The indentation level of the 2nd+ line
45 public function export($value, $indentation = 0)
47 return $this->recursiveExport($value, $indentation);
52 * @param Context $context
55 public function shortenedRecursiveExport(&$data, Context $context = null)
58 $exporter = new self();
61 $context = new Context;
66 foreach ($data as $key => $value) {
67 if (is_array($value)) {
68 if ($context->contains($data[$key]) !== false) {
69 $result[] = '*RECURSION*';
75 $this->shortenedRecursiveExport($data[$key], $context)
81 $result[] = $exporter->shortenedExport($value);
85 return implode(', ', $result);
89 * Exports a value into a single-line string
91 * The output of this method is similar to the output of
92 * SebastianBergmann\Exporter\Exporter::export().
94 * Newlines are replaced by the visible string '\n'.
95 * Contents of arrays and objects (if any) are replaced by '...'.
99 * @see SebastianBergmann\Exporter\Exporter::export
101 public function shortenedExport($value)
103 if (is_string($value)) {
104 $string = $this->export($value);
106 if (function_exists('mb_strlen')) {
107 if (mb_strlen($string) > 40) {
108 $string = mb_substr($string, 0, 30) . '...' . mb_substr($string, -7);
111 if (strlen($string) > 40) {
112 $string = substr($string, 0, 30) . '...' . substr($string, -7);
116 return str_replace("\n", '\n', $string);
119 if (is_object($value)) {
123 count($this->toArray($value)) > 0 ? '...' : ''
127 if (is_array($value)) {
130 count($value) > 0 ? '...' : ''
134 return $this->export($value);
138 * Converts an object to an array containing all of its private, protected
139 * and public properties.
141 * @param mixed $value
144 public function toArray($value)
146 if (!is_object($value)) {
147 return (array) $value;
152 foreach ((array) $value as $key => $val) {
153 // properties are transformed to keys in the following way:
154 // private $property => "\0Classname\0property"
155 // protected $property => "\0*\0property"
156 // public $property => "property"
157 if (preg_match('/^\0.+\0(.+)$/', $key, $matches)) {
161 // See https://github.com/php/php-src/commit/5721132
162 if ($key === "\0gcdata") {
169 // Some internal classes like SplObjectStorage don't work with the
170 // above (fast) mechanism nor with reflection in Zend.
171 // Format the output similarly to print_r() in this case
172 if ($value instanceof \SplObjectStorage) {
173 // However, the fast method does work in HHVM, and exposes the
174 // internal implementation. Hide it again.
175 if (property_exists('\SplObjectStorage', '__storage')) {
176 unset($array['__storage']);
177 } elseif (property_exists('\SplObjectStorage', 'storage')) {
178 unset($array['storage']);
181 if (property_exists('\SplObjectStorage', '__key')) {
182 unset($array['__key']);
185 foreach ($value as $key => $val) {
186 $array[spl_object_hash($val)] = array(
188 'inf' => $value->getInfo(),
197 * Recursive implementation of export
199 * @param mixed $value The value to export
200 * @param int $indentation The indentation level of the 2nd+ line
201 * @param \SebastianBergmann\RecursionContext\Context $processed Previously processed objects
203 * @see SebastianBergmann\Exporter\Exporter::export
205 protected function recursiveExport(&$value, $indentation, $processed = null)
207 if ($value === null) {
211 if ($value === true) {
215 if ($value === false) {
219 if (is_float($value) && floatval(intval($value)) === $value) {
223 if (is_resource($value)) {
225 'resource(%d) of type (%s)',
227 get_resource_type($value)
231 if (is_string($value)) {
232 // Match for most non printable chars somewhat taking multibyte chars into account
233 if (preg_match('/[^\x09-\x0d\x1b\x20-\xff]/', $value)) {
234 return 'Binary String: 0x' . bin2hex($value);
238 str_replace(array("\r\n", "\n\r", "\r"), array("\n", "\n", "\n"), $value) .
242 $whitespace = str_repeat(' ', 4 * $indentation);
245 $processed = new Context;
248 if (is_array($value)) {
249 if (($key = $processed->contains($value)) !== false) {
250 return 'Array &' . $key;
253 $key = $processed->add($value);
256 if (count($value) > 0) {
257 foreach ($value as $k => $v) {
259 '%s %s => %s' . "\n",
261 $this->recursiveExport($k, $indentation),
262 $this->recursiveExport($value[$k], $indentation + 1, $processed)
266 $values = "\n" . $values . $whitespace;
269 return sprintf('Array &%s (%s)', $key, $values);
272 if (is_object($value)) {
273 $class = get_class($value);
275 if ($hash = $processed->contains($value)) {
276 return sprintf('%s Object &%s', $class, $hash);
279 $hash = $processed->add($value);
281 $array = $this->toArray($value);
283 if (count($array) > 0) {
284 foreach ($array as $k => $v) {
286 '%s %s => %s' . "\n",
288 $this->recursiveExport($k, $indentation),
289 $this->recursiveExport($v, $indentation + 1, $processed)
293 $values = "\n" . $values . $whitespace;
296 return sprintf('%s Object &%s (%s)', $class, $hash, $values);
299 return var_export($value, true);