1: <?php
2:
3: namespace RedBeanPHP\Logger\RDefault;
4:
5: use RedBeanPHP\Logger as Logger;
6: use RedBeanPHP\Logger\RDefault as RDefault;
7: use RedBeanPHP\RedException as RedException;
8:
9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22:
23: class Debug extends RDefault implements Logger
24: {
25: 26: 27:
28: private $strLen = 40;
29:
30: 31: 32: 33: 34: 35: 36: 37: 38:
39: private function writeQuery( $newSql, $newBindings )
40: {
41:
42: uksort( $newBindings, function( $a, $b ) {
43: return ( strlen( $b ) - strlen( $a ) );
44: } );
45:
46: $newStr = $newSql;
47: foreach( $newBindings as $slot => $value ) {
48: if ( strpos( $slot, ':' ) === 0 ) {
49: $newStr = str_replace( $slot, $this->fillInValue( $value ), $newStr );
50: }
51: }
52: return $newStr;
53: }
54:
55: 56: 57: 58: 59: 60: 61: 62:
63: protected function fillInValue( $value )
64: {
65: if ( is_null( $value ) ) $value = 'NULL';
66:
67: $value = strval( $value );
68: if ( strlen( $value ) > ( $this->strLen ) ) {
69: $value = substr( $value, 0, ( $this->strLen ) ).'... ';
70: }
71:
72: if ( !is_numeric( $value ) && $value !== 'NULL') {
73: $value = '\''.$value.'\'';
74: }
75:
76: return $value;
77: }
78:
79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90:
91: protected function output( $str )
92: {
93: $this->logs[] = $str;
94: if ( !$this->mode ) {
95: $highlight = FALSE;
96:
97: if ( strpos( $str, 'CREATE' ) === 0
98: || strpos( $str, 'ALTER' ) === 0
99: || strpos( $str, 'DROP' ) === 0) {
100: $highlight = TRUE;
101: }
102: if (PHP_SAPI === 'cli') {
103: if ($highlight) echo "\e[91m";
104: echo $str, PHP_EOL;
105: echo "\e[39m";
106: } else {
107: if ($highlight) {
108: echo "<b style=\"color:red\">{$str}</b>";
109: } else {
110: echo $str;
111: }
112: echo '<br />';
113: }
114: }
115: }
116:
117: 118: 119: 120: 121: 122: 123: 124:
125: protected function normalizeSlots( $sql )
126: {
127: $i = 0;
128: $newSql = $sql;
129: while($i < 20 && strpos($newSql, '?') !== FALSE ){
130: $pos = strpos( $newSql, '?' );
131: $slot = ':slot'.$i;
132: $begin = substr( $newSql, 0, $pos );
133: $end = substr( $newSql, $pos+1 );
134: if (PHP_SAPI === 'cli') {
135: $newSql = "{$begin}\e[32m{$slot}\e[39m{$end}";
136: } else {
137: $newSql = "{$begin}<b style=\"color:green\">$slot</b>{$end}";
138: }
139: $i++;
140: }
141: return $newSql;
142: }
143:
144: 145: 146: 147: 148: 149: 150: 151:
152: protected function normalizeBindings( $bindings )
153: {
154: $i = 0;
155: $newBindings = array();
156: foreach( $bindings as $key => $value ) {
157: if ( is_numeric($key) ) {
158: $newKey = ':slot'.$i;
159: $newBindings[$newKey] = $value;
160: $i++;
161: } else {
162: $newBindings[$key] = $value;
163: }
164: }
165: return $newBindings;
166: }
167:
168: 169: 170: 171: 172: 173: 174: 175:
176: public function log()
177: {
178: if ( func_num_args() < 1 ) return;
179:
180: $sql = func_get_arg( 0 );
181:
182: if ( func_num_args() < 2) {
183: $bindings = array();
184: } else {
185: $bindings = func_get_arg( 1 );
186: }
187:
188: if ( !is_array( $bindings ) ) {
189: return $this->output( $sql );
190: }
191:
192: $newSql = $this->normalizeSlots( $sql );
193: $newBindings = $this->normalizeBindings( $bindings );
194: $newStr = $this->writeQuery( $newSql, $newBindings );
195: $this->output( $newStr );
196: }
197:
198: 199: 200: 201: 202: 203: 204: 205: 206:
207: public function setParamStringLength( $len = 20 )
208: {
209: $this->strLen = max(0, $len);
210: return $this;
211: }
212: }
213: