Drupal investigation

Exporter.php 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. <?php
  2. /*
  3. * This file is part of the Exporter package.
  4. *
  5. * (c) Sebastian Bergmann <sebastian@phpunit.de>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace SebastianBergmann\Exporter;
  11. use SebastianBergmann\RecursionContext\Context;
  12. /**
  13. * A nifty utility for visualizing PHP variables.
  14. *
  15. * <code>
  16. * <?php
  17. * use SebastianBergmann\Exporter\Exporter;
  18. *
  19. * $exporter = new Exporter;
  20. * print $exporter->export(new Exception);
  21. * </code>
  22. */
  23. class Exporter
  24. {
  25. /**
  26. * Exports a value as a string
  27. *
  28. * The output of this method is similar to the output of print_r(), but
  29. * improved in various aspects:
  30. *
  31. * - NULL is rendered as "null" (instead of "")
  32. * - TRUE is rendered as "true" (instead of "1")
  33. * - FALSE is rendered as "false" (instead of "")
  34. * - Strings are always quoted with single quotes
  35. * - Carriage returns and newlines are normalized to \n
  36. * - Recursion and repeated rendering is treated properly
  37. *
  38. * @param mixed $value
  39. * @param int $indentation The indentation level of the 2nd+ line
  40. * @return string
  41. */
  42. public function export($value, $indentation = 0)
  43. {
  44. return $this->recursiveExport($value, $indentation);
  45. }
  46. /**
  47. * @param mixed $data
  48. * @param Context $context
  49. * @return string
  50. */
  51. public function shortenedRecursiveExport(&$data, Context $context = null)
  52. {
  53. $result = array();
  54. $exporter = new self();
  55. if (!$context) {
  56. $context = new Context;
  57. }
  58. $context->add($data);
  59. foreach ($data as $key => $value) {
  60. if (is_array($value)) {
  61. if ($context->contains($data[$key]) !== false) {
  62. $result[] = '*RECURSION*';
  63. }
  64. else {
  65. $result[] = sprintf(
  66. 'array(%s)',
  67. $this->shortenedRecursiveExport($data[$key], $context)
  68. );
  69. }
  70. }
  71. else {
  72. $result[] = $exporter->shortenedExport($value);
  73. }
  74. }
  75. return implode(', ', $result);
  76. }
  77. /**
  78. * Exports a value into a single-line string
  79. *
  80. * The output of this method is similar to the output of
  81. * SebastianBergmann\Exporter\Exporter::export().
  82. *
  83. * Newlines are replaced by the visible string '\n'.
  84. * Contents of arrays and objects (if any) are replaced by '...'.
  85. *
  86. * @param mixed $value
  87. * @return string
  88. * @see SebastianBergmann\Exporter\Exporter::export
  89. */
  90. public function shortenedExport($value)
  91. {
  92. if (is_string($value)) {
  93. $string = $this->export($value);
  94. if (function_exists('mb_strlen')) {
  95. if (mb_strlen($string) > 40) {
  96. $string = mb_substr($string, 0, 30) . '...' . mb_substr($string, -7);
  97. }
  98. } else {
  99. if (strlen($string) > 40) {
  100. $string = substr($string, 0, 30) . '...' . substr($string, -7);
  101. }
  102. }
  103. return str_replace("\n", '\n', $string);
  104. }
  105. if (is_object($value)) {
  106. return sprintf(
  107. '%s Object (%s)',
  108. get_class($value),
  109. count($this->toArray($value)) > 0 ? '...' : ''
  110. );
  111. }
  112. if (is_array($value)) {
  113. return sprintf(
  114. 'Array (%s)',
  115. count($value) > 0 ? '...' : ''
  116. );
  117. }
  118. return $this->export($value);
  119. }
  120. /**
  121. * Converts an object to an array containing all of its private, protected
  122. * and public properties.
  123. *
  124. * @param mixed $value
  125. * @return array
  126. */
  127. public function toArray($value)
  128. {
  129. if (!is_object($value)) {
  130. return (array) $value;
  131. }
  132. $array = array();
  133. foreach ((array) $value as $key => $val) {
  134. // properties are transformed to keys in the following way:
  135. // private $property => "\0Classname\0property"
  136. // protected $property => "\0*\0property"
  137. // public $property => "property"
  138. if (preg_match('/^\0.+\0(.+)$/', $key, $matches)) {
  139. $key = $matches[1];
  140. }
  141. // See https://github.com/php/php-src/commit/5721132
  142. if ($key === "\0gcdata") {
  143. continue;
  144. }
  145. $array[$key] = $val;
  146. }
  147. // Some internal classes like SplObjectStorage don't work with the
  148. // above (fast) mechanism nor with reflection in Zend.
  149. // Format the output similarly to print_r() in this case
  150. if ($value instanceof \SplObjectStorage) {
  151. // However, the fast method does work in HHVM, and exposes the
  152. // internal implementation. Hide it again.
  153. if (property_exists('\SplObjectStorage', '__storage')) {
  154. unset($array['__storage']);
  155. } elseif (property_exists('\SplObjectStorage', 'storage')) {
  156. unset($array['storage']);
  157. }
  158. if (property_exists('\SplObjectStorage', '__key')) {
  159. unset($array['__key']);
  160. }
  161. foreach ($value as $key => $val) {
  162. $array[spl_object_hash($val)] = array(
  163. 'obj' => $val,
  164. 'inf' => $value->getInfo(),
  165. );
  166. }
  167. }
  168. return $array;
  169. }
  170. /**
  171. * Recursive implementation of export
  172. *
  173. * @param mixed $value The value to export
  174. * @param int $indentation The indentation level of the 2nd+ line
  175. * @param \SebastianBergmann\RecursionContext\Context $processed Previously processed objects
  176. * @return string
  177. * @see SebastianBergmann\Exporter\Exporter::export
  178. */
  179. protected function recursiveExport(&$value, $indentation, $processed = null)
  180. {
  181. if ($value === null) {
  182. return 'null';
  183. }
  184. if ($value === true) {
  185. return 'true';
  186. }
  187. if ($value === false) {
  188. return 'false';
  189. }
  190. if (is_float($value) && floatval(intval($value)) === $value) {
  191. return "$value.0";
  192. }
  193. if (is_resource($value)) {
  194. return sprintf(
  195. 'resource(%d) of type (%s)',
  196. $value,
  197. get_resource_type($value)
  198. );
  199. }
  200. if (is_string($value)) {
  201. // Match for most non printable chars somewhat taking multibyte chars into account
  202. if (preg_match('/[^\x09-\x0d\x1b\x20-\xff]/', $value)) {
  203. return 'Binary String: 0x' . bin2hex($value);
  204. }
  205. return "'" .
  206. str_replace(array("\r\n", "\n\r", "\r"), array("\n", "\n", "\n"), $value) .
  207. "'";
  208. }
  209. $whitespace = str_repeat(' ', 4 * $indentation);
  210. if (!$processed) {
  211. $processed = new Context;
  212. }
  213. if (is_array($value)) {
  214. if (($key = $processed->contains($value)) !== false) {
  215. return 'Array &' . $key;
  216. }
  217. $key = $processed->add($value);
  218. $values = '';
  219. if (count($value) > 0) {
  220. foreach ($value as $k => $v) {
  221. $values .= sprintf(
  222. '%s %s => %s' . "\n",
  223. $whitespace,
  224. $this->recursiveExport($k, $indentation),
  225. $this->recursiveExport($value[$k], $indentation + 1, $processed)
  226. );
  227. }
  228. $values = "\n" . $values . $whitespace;
  229. }
  230. return sprintf('Array &%s (%s)', $key, $values);
  231. }
  232. if (is_object($value)) {
  233. $class = get_class($value);
  234. if ($hash = $processed->contains($value)) {
  235. return sprintf('%s Object &%s', $class, $hash);
  236. }
  237. $hash = $processed->add($value);
  238. $values = '';
  239. $array = $this->toArray($value);
  240. if (count($array) > 0) {
  241. foreach ($array as $k => $v) {
  242. $values .= sprintf(
  243. '%s %s => %s' . "\n",
  244. $whitespace,
  245. $this->recursiveExport($k, $indentation),
  246. $this->recursiveExport($v, $indentation + 1, $processed)
  247. );
  248. }
  249. $values = "\n" . $values . $whitespace;
  250. }
  251. return sprintf('%s Object &%s (%s)', $class, $hash, $values);
  252. }
  253. return var_export($value, true);
  254. }
  255. }