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