Chris@0:
Chris@0: *
Chris@0: * For the full copyright and license information, please view the LICENSE
Chris@0: * file that was distributed with this source code.
Chris@0: */
Chris@0:
Chris@0: namespace SebastianBergmann\Exporter;
Chris@0:
Chris@0: use SebastianBergmann\RecursionContext\Context;
Chris@0:
Chris@0: /**
Chris@0: * A nifty utility for visualizing PHP variables.
Chris@0: *
Chris@0: *
Chris@0: * export(new Exception);
Chris@0: *
Chris@0: */
Chris@0: class Exporter
Chris@0: {
Chris@0: /**
Chris@0: * Exports a value as a string
Chris@0: *
Chris@0: * The output of this method is similar to the output of print_r(), but
Chris@0: * improved in various aspects:
Chris@0: *
Chris@0: * - NULL is rendered as "null" (instead of "")
Chris@0: * - TRUE is rendered as "true" (instead of "1")
Chris@0: * - FALSE is rendered as "false" (instead of "")
Chris@0: * - Strings are always quoted with single quotes
Chris@0: * - Carriage returns and newlines are normalized to \n
Chris@0: * - Recursion and repeated rendering is treated properly
Chris@0: *
Chris@14: * @param mixed $value
Chris@14: * @param int $indentation The indentation level of the 2nd+ line
Chris@14: *
Chris@0: * @return string
Chris@0: */
Chris@0: public function export($value, $indentation = 0)
Chris@0: {
Chris@0: return $this->recursiveExport($value, $indentation);
Chris@0: }
Chris@0:
Chris@0: /**
Chris@14: * @param mixed $data
Chris@14: * @param Context $context
Chris@14: *
Chris@0: * @return string
Chris@0: */
Chris@0: public function shortenedRecursiveExport(&$data, Context $context = null)
Chris@0: {
Chris@14: $result = [];
Chris@0: $exporter = new self();
Chris@0:
Chris@0: if (!$context) {
Chris@0: $context = new Context;
Chris@0: }
Chris@0:
Chris@14: $array = $data;
Chris@0: $context->add($data);
Chris@0:
Chris@14: foreach ($array as $key => $value) {
Chris@0: if (is_array($value)) {
Chris@0: if ($context->contains($data[$key]) !== false) {
Chris@0: $result[] = '*RECURSION*';
Chris@14: } else {
Chris@0: $result[] = sprintf(
Chris@0: 'array(%s)',
Chris@0: $this->shortenedRecursiveExport($data[$key], $context)
Chris@0: );
Chris@0: }
Chris@14: } else {
Chris@0: $result[] = $exporter->shortenedExport($value);
Chris@0: }
Chris@0: }
Chris@0:
Chris@0: return implode(', ', $result);
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Exports a value into a single-line string
Chris@0: *
Chris@0: * The output of this method is similar to the output of
Chris@0: * SebastianBergmann\Exporter\Exporter::export().
Chris@0: *
Chris@0: * Newlines are replaced by the visible string '\n'.
Chris@0: * Contents of arrays and objects (if any) are replaced by '...'.
Chris@0: *
Chris@14: * @param mixed $value
Chris@14: *
Chris@0: * @return string
Chris@14: *
Chris@0: * @see SebastianBergmann\Exporter\Exporter::export
Chris@0: */
Chris@0: public function shortenedExport($value)
Chris@0: {
Chris@0: if (is_string($value)) {
Chris@14: $string = str_replace("\n", '', $this->export($value));
Chris@0:
Chris@0: if (function_exists('mb_strlen')) {
Chris@0: if (mb_strlen($string) > 40) {
Chris@0: $string = mb_substr($string, 0, 30) . '...' . mb_substr($string, -7);
Chris@0: }
Chris@0: } else {
Chris@0: if (strlen($string) > 40) {
Chris@0: $string = substr($string, 0, 30) . '...' . substr($string, -7);
Chris@0: }
Chris@0: }
Chris@0:
Chris@14: return $string;
Chris@0: }
Chris@0:
Chris@0: if (is_object($value)) {
Chris@0: return sprintf(
Chris@0: '%s Object (%s)',
Chris@0: get_class($value),
Chris@0: count($this->toArray($value)) > 0 ? '...' : ''
Chris@0: );
Chris@0: }
Chris@0:
Chris@0: if (is_array($value)) {
Chris@0: return sprintf(
Chris@0: 'Array (%s)',
Chris@0: count($value) > 0 ? '...' : ''
Chris@0: );
Chris@0: }
Chris@0:
Chris@0: return $this->export($value);
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Converts an object to an array containing all of its private, protected
Chris@0: * and public properties.
Chris@0: *
Chris@14: * @param mixed $value
Chris@14: *
Chris@0: * @return array
Chris@0: */
Chris@0: public function toArray($value)
Chris@0: {
Chris@0: if (!is_object($value)) {
Chris@0: return (array) $value;
Chris@0: }
Chris@0:
Chris@14: $array = [];
Chris@0:
Chris@0: foreach ((array) $value as $key => $val) {
Chris@0: // properties are transformed to keys in the following way:
Chris@0: // private $property => "\0Classname\0property"
Chris@0: // protected $property => "\0*\0property"
Chris@0: // public $property => "property"
Chris@0: if (preg_match('/^\0.+\0(.+)$/', $key, $matches)) {
Chris@0: $key = $matches[1];
Chris@0: }
Chris@0:
Chris@0: // See https://github.com/php/php-src/commit/5721132
Chris@0: if ($key === "\0gcdata") {
Chris@0: continue;
Chris@0: }
Chris@0:
Chris@0: $array[$key] = $val;
Chris@0: }
Chris@0:
Chris@0: // Some internal classes like SplObjectStorage don't work with the
Chris@0: // above (fast) mechanism nor with reflection in Zend.
Chris@0: // Format the output similarly to print_r() in this case
Chris@0: if ($value instanceof \SplObjectStorage) {
Chris@0: // However, the fast method does work in HHVM, and exposes the
Chris@0: // internal implementation. Hide it again.
Chris@0: if (property_exists('\SplObjectStorage', '__storage')) {
Chris@0: unset($array['__storage']);
Chris@0: } elseif (property_exists('\SplObjectStorage', 'storage')) {
Chris@0: unset($array['storage']);
Chris@0: }
Chris@0:
Chris@0: if (property_exists('\SplObjectStorage', '__key')) {
Chris@0: unset($array['__key']);
Chris@0: }
Chris@0:
Chris@0: foreach ($value as $key => $val) {
Chris@14: $array[spl_object_hash($val)] = [
Chris@0: 'obj' => $val,
Chris@0: 'inf' => $value->getInfo(),
Chris@14: ];
Chris@0: }
Chris@0: }
Chris@0:
Chris@0: return $array;
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Recursive implementation of export
Chris@0: *
Chris@14: * @param mixed $value The value to export
Chris@14: * @param int $indentation The indentation level of the 2nd+ line
Chris@14: * @param \SebastianBergmann\RecursionContext\Context $processed Previously processed objects
Chris@14: *
Chris@0: * @return string
Chris@14: *
Chris@0: * @see SebastianBergmann\Exporter\Exporter::export
Chris@0: */
Chris@0: protected function recursiveExport(&$value, $indentation, $processed = null)
Chris@0: {
Chris@0: if ($value === null) {
Chris@0: return 'null';
Chris@0: }
Chris@0:
Chris@0: if ($value === true) {
Chris@0: return 'true';
Chris@0: }
Chris@0:
Chris@0: if ($value === false) {
Chris@0: return 'false';
Chris@0: }
Chris@0:
Chris@0: if (is_float($value) && floatval(intval($value)) === $value) {
Chris@0: return "$value.0";
Chris@0: }
Chris@0:
Chris@0: if (is_resource($value)) {
Chris@0: return sprintf(
Chris@0: 'resource(%d) of type (%s)',
Chris@0: $value,
Chris@0: get_resource_type($value)
Chris@0: );
Chris@0: }
Chris@0:
Chris@0: if (is_string($value)) {
Chris@0: // Match for most non printable chars somewhat taking multibyte chars into account
Chris@0: if (preg_match('/[^\x09-\x0d\x1b\x20-\xff]/', $value)) {
Chris@0: return 'Binary String: 0x' . bin2hex($value);
Chris@0: }
Chris@0:
Chris@0: return "'" .
Chris@14: str_replace('', "\n",
Chris@14: str_replace(
Chris@14: ["\r\n", "\n\r", "\r", "\n"],
Chris@14: ['\r\n', '\n\r', '\r', '\n'],
Chris@14: $value
Chris@14: )
Chris@14: ) .
Chris@0: "'";
Chris@0: }
Chris@0:
Chris@0: $whitespace = str_repeat(' ', 4 * $indentation);
Chris@0:
Chris@0: if (!$processed) {
Chris@0: $processed = new Context;
Chris@0: }
Chris@0:
Chris@0: if (is_array($value)) {
Chris@0: if (($key = $processed->contains($value)) !== false) {
Chris@0: return 'Array &' . $key;
Chris@0: }
Chris@0:
Chris@14: $array = $value;
Chris@0: $key = $processed->add($value);
Chris@0: $values = '';
Chris@0:
Chris@14: if (count($array) > 0) {
Chris@14: foreach ($array as $k => $v) {
Chris@0: $values .= sprintf(
Chris@0: '%s %s => %s' . "\n",
Chris@0: $whitespace,
Chris@0: $this->recursiveExport($k, $indentation),
Chris@0: $this->recursiveExport($value[$k], $indentation + 1, $processed)
Chris@0: );
Chris@0: }
Chris@0:
Chris@0: $values = "\n" . $values . $whitespace;
Chris@0: }
Chris@0:
Chris@0: return sprintf('Array &%s (%s)', $key, $values);
Chris@0: }
Chris@0:
Chris@0: if (is_object($value)) {
Chris@0: $class = get_class($value);
Chris@0:
Chris@0: if ($hash = $processed->contains($value)) {
Chris@0: return sprintf('%s Object &%s', $class, $hash);
Chris@0: }
Chris@0:
Chris@0: $hash = $processed->add($value);
Chris@0: $values = '';
Chris@0: $array = $this->toArray($value);
Chris@0:
Chris@0: if (count($array) > 0) {
Chris@0: foreach ($array as $k => $v) {
Chris@0: $values .= sprintf(
Chris@0: '%s %s => %s' . "\n",
Chris@0: $whitespace,
Chris@0: $this->recursiveExport($k, $indentation),
Chris@0: $this->recursiveExport($v, $indentation + 1, $processed)
Chris@0: );
Chris@0: }
Chris@0:
Chris@0: $values = "\n" . $values . $whitespace;
Chris@0: }
Chris@0:
Chris@0: return sprintf('%s Object &%s (%s)', $class, $hash, $values);
Chris@0: }
Chris@0:
Chris@0: return var_export($value, true);
Chris@0: }
Chris@0: }