SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Helpers/Debug.php

  1. <?php
  2. /*
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  */
  13.  
  14. namespace Sprout\Helpers;
  15.  
  16. use InvalidArgumentException;
  17. use PDOStatement;
  18.  
  19.  
  20. /** Useful methods for debugging */
  21. class Debug
  22. {
  23.  
  24. /**
  25.   * Wraps a value in <pre> tags and calls print_r to show all of its data.
  26.   *
  27.   * N.B. Boolean and null values are displayed as strings instead of merely
  28.   * returning an empty <pre>.
  29.   *
  30.   * @param mixed $val
  31.   * @return string HTML
  32.   */
  33. static function pre($val)
  34. {
  35. if (is_bool($val)) {
  36. $val = '<i>' . ($val? 'true': 'false') . '</i>';
  37. } else if ($val === null) {
  38. $val = '<i>null</i>';
  39. } else {
  40. $val = Enc::html(print_r($val, true));
  41. }
  42. return '<pre>' . $val . '</pre>';
  43. }
  44.  
  45.  
  46. /**
  47.   * Determines the type of a variable.
  48.   *
  49.   * For objects, the class name is returned. For other variable types,
  50.   * the type and possibly some extra info about its value is returned.
  51.   *
  52.   * @param mixed $val
  53.   * @return string Plain text
  54.   */
  55. static function type($val)
  56. {
  57. if (is_object($val)) {
  58. return get_class($val);
  59. } else if (is_array($val)) {
  60. $out = '[';
  61. $num = 0;
  62. foreach ($val as $key => $item) {
  63. if (++$num != 1) $out .= ', ';
  64.  
  65. // Array keys can only be ints or strings
  66. // See http://php.net/manual/en/language.types.array.php
  67. if (is_int($key)) {
  68. $out .= $key;
  69. } else {
  70. $out .= "'" . addslashes($key) . "'";
  71. }
  72. $out .= ': ';
  73. if (is_array($item)) {
  74. $out .= 'Array';
  75. } else {
  76. $out .= self::type($item);
  77. }
  78. }
  79. $out .= ']';
  80. } else if (is_string($val)) {
  81. $out = gettype($val) . ': ' . str_replace("\0", "\\0", $val);
  82. } else {
  83. $out = gettype($val);
  84. if (is_bool($val)) {
  85. $out .= ': ' . ($val? 'true': 'false');
  86. } else if (!is_null($val)) {
  87. $out .= ': ' . $val;
  88. }
  89. }
  90. return $out;
  91. }
  92.  
  93.  
  94. /**
  95.   * Converts a DB result set or similar multi-dimensional array into HTML for a table
  96.   * @param array|PDOStatement $data If a PDOStatement, the cursor will automatically be closed after rendering
  97.   * @return string
  98.   */
  99. static function table($data)
  100. {
  101. if (!is_array($data) and !($data instanceof PDOStatement)) {
  102. throw new InvalidArgumentException('Must be array or PDOStatement');
  103. }
  104.  
  105. if (is_array($data) and count($data) == 0) {
  106. return '<p>No data</p>';
  107. } else if ($data instanceof PDOStatement and $data->rowCount() == 0) {
  108. return '<p>No data</p>';
  109. }
  110.  
  111. $out = "<table>\n";
  112.  
  113. $first_row = true;
  114. foreach ($data as $row) {
  115. if ($first_row) {
  116. $first_row = false;
  117. $out .= "<tr>";
  118. foreach ($row as $col => $val) {
  119. $out .= '<th>' . Enc::html($col) . '</th>';
  120. }
  121. $out .= "</tr>\n";
  122. }
  123.  
  124. $out .= "<tr>";
  125. foreach ($row as $val) {
  126. if ($val === null) {
  127. $out .= '<td><i>null</i></td>';
  128. continue;
  129. }
  130. $out .= '<td>' . Enc::html($val) . '</td>';
  131. }
  132. $out .= "</tr>\n";
  133. }
  134.  
  135. $out .= "</table>\n";
  136.  
  137. if ($data instanceof PDOStatement) {
  138. $data->closeCursor();
  139. }
  140.  
  141. return $out;
  142. }
  143. }
  144.