1: <?php
2: /**
3: * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
4: * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
11: * @link https://cakephp.org CakePHP(tm) Project
12: * @since 0.2.9
13: * @license https://opensource.org/licenses/mit-license.php MIT License
14: */
15: use Cake\Core\Configure;
16: use Cake\Error\Debugger;
17:
18: /**
19: * Basic defines for timing functions.
20: */
21: define('SECOND', 1);
22: define('MINUTE', 60);
23: define('HOUR', 3600);
24: define('DAY', 86400);
25: define('WEEK', 604800);
26: define('MONTH', 2592000);
27: define('YEAR', 31536000);
28:
29: if (!function_exists('debug')) {
30: /**
31: * Prints out debug information about given variable and returns the
32: * variable that was passed.
33: *
34: * Only runs if debug mode is enabled.
35: *
36: * @param mixed $var Variable to show debug information for.
37: * @param bool|null $showHtml If set to true, the method prints the debug data in a browser-friendly way.
38: * @param bool $showFrom If set to true, the method prints from where the function was called.
39: * @return mixed The same $var that was passed
40: * @link https://book.cakephp.org/3.0/en/development/debugging.html#basic-debugging
41: * @link https://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html#debug
42: */
43: function debug($var, $showHtml = null, $showFrom = true)
44: {
45: if (!Configure::read('debug')) {
46: return $var;
47: }
48:
49: $location = [];
50: if ($showFrom) {
51: $trace = Debugger::trace(['start' => 1, 'depth' => 2, 'format' => 'array']);
52: $location = [
53: 'line' => $trace[0]['line'],
54: 'file' => $trace[0]['file']
55: ];
56: }
57:
58: Debugger::printVar($var, $location, $showHtml);
59:
60: return $var;
61: }
62:
63: }
64:
65: if (!function_exists('stackTrace')) {
66: /**
67: * Outputs a stack trace based on the supplied options.
68: *
69: * ### Options
70: *
71: * - `depth` - The number of stack frames to return. Defaults to 999
72: * - `args` - Should arguments for functions be shown? If true, the arguments for each method call
73: * will be displayed.
74: * - `start` - The stack frame to start generating a trace from. Defaults to 1
75: *
76: * @param array $options Format for outputting stack trace
77: * @return mixed Formatted stack trace
78: */
79: function stackTrace(array $options = [])
80: {
81: if (!Configure::read('debug')) {
82: return;
83: }
84:
85: $options += ['start' => 0];
86: $options['start']++;
87: echo Debugger::trace($options);
88: }
89:
90: }
91:
92: if (!function_exists('breakpoint')) {
93: /**
94: * Command to return the eval-able code to startup PsySH in interactive debugger
95: * Works the same way as eval(\Psy\sh());
96: * psy/psysh must be loaded in your project
97: * @link http://psysh.org/
98: * ```
99: * eval(breakpoint());
100: * ```
101: * @return string
102: */
103: function breakpoint()
104: {
105: if ((PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg') && class_exists('\Psy\Shell')) {
106: return 'extract(\Psy\Shell::debug(get_defined_vars(), isset($this) ? $this : null));';
107: }
108: trigger_error(
109: 'psy/psysh must be installed and you must be in a CLI environment to use the breakpoint function',
110: E_USER_WARNING
111: );
112: }
113: }
114:
115: if (!function_exists('dd')) {
116: /**
117: * Prints out debug information about given variable and dies.
118: *
119: * Only runs if debug mode is enabled.
120: * It will otherwise just continue code execution and ignore this function.
121: *
122: * @param mixed $var Variable to show debug information for.
123: * @param bool|null $showHtml If set to true, the method prints the debug data in a browser-friendly way.
124: * @return void
125: * @link https://book.cakephp.org/3.0/en/development/debugging.html#basic-debugging
126: */
127: function dd($var, $showHtml = null)
128: {
129: if (!Configure::read('debug')) {
130: return;
131: }
132:
133: $trace = Debugger::trace(['start' => 1, 'depth' => 2, 'format' => 'array']);
134: $location = [
135: 'line' => $trace[0]['line'],
136: 'file' => $trace[0]['file']
137: ];
138:
139: Debugger::printVar($var, $location, $showHtml);
140: die(1);
141: }
142: }
143:
144: if (!function_exists('loadPHPUnitAliases')) {
145: /**
146: * Loads PHPUnit aliases
147: *
148: * This is an internal function used for backwards compatibility during
149: * fixture related tests.
150: *
151: * @return void
152: */
153: function loadPHPUnitAliases()
154: {
155: require_once dirname(__DIR__) . DS . 'tests' . DS . 'phpunit_aliases.php';
156: }
157: }
158: