Overview

Namespaces

  • None
  • RedBeanPHP
    • Adapter
    • BeanHelper
    • Cursor
    • Driver
    • Logger
      • RDefault
    • QueryWriter
    • RedException
    • Repository
    • Util

Classes

  • R
  • RedBean_SimpleModel
  • RedBeanPHP\Adapter\DBAdapter
  • RedBeanPHP\AssociationManager
  • RedBeanPHP\BeanCollection
  • RedBeanPHP\BeanHelper\SimpleFacadeBeanHelper
  • RedBeanPHP\Cursor\NullCursor
  • RedBeanPHP\Cursor\PDOCursor
  • RedBeanPHP\Driver\RPDO
  • RedBeanPHP\DuplicationManager
  • RedBeanPHP\Facade
  • RedBeanPHP\Finder
  • RedBeanPHP\Jsonable
  • RedBeanPHP\LabelMaker
  • RedBeanPHP\Logger\RDefault
  • RedBeanPHP\Logger\RDefault\Debug
  • RedBeanPHP\Observable
  • RedBeanPHP\OODB
  • RedBeanPHP\OODBBean
  • RedBeanPHP\QueryWriter\AQueryWriter
  • RedBeanPHP\QueryWriter\CUBRID
  • RedBeanPHP\QueryWriter\MySQL
  • RedBeanPHP\QueryWriter\PostgreSQL
  • RedBeanPHP\QueryWriter\SQLiteT
  • RedBeanPHP\R
  • RedBeanPHP\Repository
  • RedBeanPHP\Repository\Fluid
  • RedBeanPHP\Repository\Frozen
  • RedBeanPHP\SimpleModel
  • RedBeanPHP\SimpleModelHelper
  • RedBeanPHP\TagManager
  • RedBeanPHP\ToolBox
  • RedBeanPHP\Util\ArrayTool
  • RedBeanPHP\Util\DispenseHelper
  • RedBeanPHP\Util\Dump
  • RedBeanPHP\Util\MultiLoader
  • RedBeanPHP\Util\Transaction

Interfaces

  • RedBeanPHP\Adapter
  • RedBeanPHP\BeanHelper
  • RedBeanPHP\Cursor
  • RedBeanPHP\Driver
  • RedBeanPHP\Logger
  • RedBeanPHP\Observer
  • RedBeanPHP\Plugin
  • RedBeanPHP\QueryWriter

Exceptions

  • RedBeanPHP\RedException
  • RedBeanPHP\RedException\SQL

Functions

  • array_flatten
  • dmp
  • EID
  • genslots
  • Overview
  • Namespace
  • Class
  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:  * Debug logger.
 11:  * A special logger for debugging purposes.
 12:  * Provides debugging logging functions for RedBeanPHP.
 13:  *
 14:  * @file    RedBeanPHP/Logger/RDefault/Debug.php
 15:  * @author  Gabor de Mooij and the RedBeanPHP Community
 16:  * @license BSD/GPLv2
 17:  *
 18:  * @copyright
 19:  * copyright (c) G.J.G.T. (Gabor) de Mooij
 20:  * This source file is subject to the BSD/GPLv2 License that is bundled
 21:  * with this source code in the file license.txt.
 22:  */
 23: class Debug extends RDefault implements Logger
 24: {
 25:     /**
 26:      * @var integer
 27:      */
 28:     private $strLen = 40;
 29: 
 30:     /**
 31:      * Writes a query for logging with all bindings / params filled
 32:      * in.
 33:      *
 34:      * @param string $newSql   the query
 35:      * @param array  $bindings the bindings to process (key-value pairs)
 36:      *
 37:      * @return string
 38:      */
 39:     private function writeQuery( $newSql, $newBindings )
 40:     {
 41:         //avoid str_replace collisions: slot1 and slot10 (issue 407).
 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:      * Fills in a value of a binding and truncates the
 57:      * resulting string if necessary.
 58:      *
 59:      * @param mixed $value bound value
 60:      *
 61:      * @return string
 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:      * Dependending on the current mode of operation,
 81:      * this method will either log and output to STDIN or
 82:      * just log.
 83:      *
 84:      * Depending on the value of constant PHP_SAPI this function
 85:      * will format output for console or HTML.
 86:      *
 87:      * @param string $str string to log or output and log
 88:      *
 89:      * @return void
 90:      */
 91:     protected function output( $str )
 92:     {
 93:         $this->logs[] = $str;
 94:         if ( !$this->mode ) {
 95:             $highlight = FALSE;
 96:             /* just a quick heuritsic to highlight schema changes */
 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:      * Normalizes the slots in an SQL string.
119:      * Replaces question mark slots with :slot1 :slot2 etc.
120:      *
121:      * @param string $sql sql to normalize
122:      *
123:      * @return string
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:      * Normalizes the bindings.
146:      * Replaces numeric binding keys with :slot1 :slot2 etc.
147:      *
148:      * @param array $bindings bindings to normalize
149:      *
150:      * @return array
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:      * Logger method.
170:      *
171:      * Takes a number of arguments tries to create
172:      * a proper debug log based on the available data.
173:      *
174:      * @return void
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:      * Sets the max string length for the parameter output in
200:      * SQL queries. Set this value to a reasonable number to
201:      * keep you SQL queries readable.
202:      *
203:      * @param integer $len string length
204:      *
205:      * @return self
206:      */
207:     public function setParamStringLength( $len = 20 )
208:     {
209:         $this->strLen = max(0, $len);
210:         return $this;
211:     }
212: }
213: 
API documentation generated by ApiGen