basics.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 0.2.9
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. use Cake\Core\Configure;
  16. use Cake\Error\Debugger;
  17. /**
  18. * Basic defines for timing functions.
  19. */
  20. define('SECOND', 1);
  21. define('MINUTE', 60);
  22. define('HOUR', 3600);
  23. define('DAY', 86400);
  24. define('WEEK', 604800);
  25. define('MONTH', 2592000);
  26. define('YEAR', 31536000);
  27. if (!function_exists('debug')) {
  28. /**
  29. * Prints out debug information about given variable.
  30. *
  31. * Only runs if debug level is greater than zero.
  32. *
  33. * @param mixed $var Variable to show debug information for.
  34. * @param bool|null $showHtml If set to true, the method prints the debug data in a browser-friendly way.
  35. * @param bool $showFrom If set to true, the method prints from where the function was called.
  36. * @return void
  37. * @link http://book.cakephp.org/3.0/en/development/debugging.html#basic-debugging
  38. * @link http://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html#debug
  39. */
  40. function debug($var, $showHtml = null, $showFrom = true)
  41. {
  42. if (!Configure::read('debug')) {
  43. return;
  44. }
  45. $file = '';
  46. $line = '';
  47. $lineInfo = '';
  48. if ($showFrom) {
  49. $trace = Debugger::trace(['start' => 1, 'depth' => 2, 'format' => 'array']);
  50. $search = [ROOT];
  51. if (defined('CAKE_CORE_INCLUDE_PATH')) {
  52. array_unshift($search, CAKE_CORE_INCLUDE_PATH);
  53. }
  54. $file = str_replace($search, '', $trace[0]['file']);
  55. $line = $trace[0]['line'];
  56. }
  57. $html = <<<HTML
  58. <div class="cake-debug-output">
  59. %s
  60. <pre class="cake-debug">
  61. %s
  62. </pre>
  63. </div>
  64. HTML;
  65. $text = <<<TEXT
  66. %s
  67. ########## DEBUG ##########
  68. %s
  69. ###########################
  70. TEXT;
  71. $template = $html;
  72. if (PHP_SAPI === 'cli' || $showHtml === false) {
  73. $template = $text;
  74. if ($showFrom) {
  75. $lineInfo = sprintf('%s (line %s)', $file, $line);
  76. }
  77. }
  78. if ($showHtml === null && $template !== $text) {
  79. $showHtml = true;
  80. }
  81. $var = Debugger::exportVar($var, 25);
  82. if ($showHtml) {
  83. $template = $html;
  84. $var = h($var);
  85. if ($showFrom) {
  86. $lineInfo = sprintf('<span><strong>%s</strong> (line <strong>%s</strong>)</span>', $file, $line);
  87. }
  88. }
  89. printf($template, $lineInfo, $var);
  90. }
  91. }
  92. if (!function_exists('stackTrace')) {
  93. /**
  94. * Outputs a stack trace based on the supplied options.
  95. *
  96. * ### Options
  97. *
  98. * - `depth` - The number of stack frames to return. Defaults to 999
  99. * - `args` - Should arguments for functions be shown? If true, the arguments for each method call
  100. * will be displayed.
  101. * - `start` - The stack frame to start generating a trace from. Defaults to 1
  102. *
  103. * @param array $options Format for outputting stack trace
  104. * @return mixed Formatted stack trace
  105. */
  106. function stackTrace(array $options = [])
  107. {
  108. if (!Configure::read('debug')) {
  109. return;
  110. }
  111. $options += ['start' => 0];
  112. $options['start']++;
  113. echo Debugger::trace($options);
  114. }
  115. }
  116. if (!function_exists('json_last_error_msg')) {
  117. /**
  118. * Provides the fallback implementation of json_last_error_msg() available in PHP 5.5 and above.
  119. *
  120. * @return string Error message.
  121. */
  122. function json_last_error_msg()
  123. {
  124. static $errors = [
  125. JSON_ERROR_NONE => '',
  126. JSON_ERROR_DEPTH => 'Maximum stack depth exceeded',
  127. JSON_ERROR_STATE_MISMATCH => 'Invalid or malformed JSON',
  128. JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded',
  129. JSON_ERROR_SYNTAX => 'Syntax error',
  130. JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded'
  131. ];
  132. $error = json_last_error();
  133. return array_key_exists($error, $errors) ? $errors[$error] : "Unknown error ({$error})";
  134. }
  135. }
  136. if (!function_exists('breakpoint')) {
  137. /**
  138. * Command to return the eval-able code to startup PsySH in interactive debugger
  139. * Works the same way as eval(\Psy\sh());
  140. * psy/psysh must be loaded in your project
  141. * @link http://psysh.org/
  142. * ```
  143. * eval(breakpoint());
  144. * ```
  145. * @return string
  146. */
  147. function breakpoint()
  148. {
  149. if (PHP_SAPI === 'cli' && class_exists('\Psy\Shell')) {
  150. return 'extract(\Psy\Shell::debug(get_defined_vars(), isset($this) ? $this : null));';
  151. }
  152. trigger_error(
  153. "psy/psysh must be installed and you must be in a CLI environment to use the breakpoint function",
  154. E_USER_WARNING
  155. );
  156. }
  157. }