Overview

Namespaces

  • Flea
  • None

Classes

  • Flea
  • Flea\BuildUtil
  • Flea\Cache
  • Flea\DataBase
  • Flea\DataBaseCRUD
  • Flea\DataList
  • Flea\DataUtil
  • Flea\Debug
  • Flea\FileUtil
  • Flea\General
  • Flea\Header
  • Flea\InitUtil
  • Flea\LangList
  • Flea\Login
  • Flea\LoginFormHelper
  • Flea\LoginTableName
  • Flea\LoginUser
  • Flea\Page
  • Flea\PageList
  • Flea\PageListCreate
  • Flea\SqlQuery
  • Flea\TagUtil
  • Flea\UrlUtil
  • Flea\ValueObject
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: 
  3: /*
  4:  * The MIT License
  5:  *
  6:  * Copyright 2014 Damien Doussaud (namide.com).
  7:  *
  8:  * Permission is hereby granted, free of charge, to any person obtaining a copy
  9:  * of this software and associated documentation files (the "Software"), to deal
 10:  * in the Software without restriction, including without limitation the rights
 11:  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 12:  * copies of the Software, and to permit persons to whom the Software is
 13:  * furnished to do so, subject to the following conditions:
 14:  *
 15:  * The above copyright notice and this permission notice shall be included in
 16:  * all copies or substantial portions of the Software.
 17:  *
 18:  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 19:  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 20:  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 21:  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 22:  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 23:  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 24:  * THE SOFTWARE.
 25:  */
 26: 
 27: namespace Flea;
 28: 
 29: /**
 30:  * Emmiting debuging messages, warnings and errors
 31:  *
 32:  * @author Namide
 33:  */
 34: class Debug {
 35: 
 36:     private static $_INSTANCE;
 37:     private $_errorList;
 38:     private $_timer;
 39:     private $_totalTime;
 40:     private $_timerList;
 41:     private $_errorBackNum = 10;
 42:     private $_errorJsAlert = false;
 43:     private $_errorEcho = true;
 44:     private $_errorFile = true;
 45:     private $_errorFileName = 'errors.log';
 46: 
 47:     /**
 48:      * Number of functions called before the error.
 49:      * Default is 10.
 50:      * 
 51:      * @param int $num      Maximum of caller saved
 52:      */
 53:     public function setErrorBackNum($num) {
 54:         $this->_errorBackNum = $num;
 55:     }
 56: 
 57:     /**
 58:      * Number of functions called before the error.
 59:      * 
 60:      * @return int          Maximum of caller saved
 61:      */
 62:     public function getErrorBackNum() {
 63:         return $this->_errorBackNum;
 64:     }
 65: 
 66:     /**
 67:      * Force the JavaScript alert() to trace errors.
 68:      * 
 69:      * @param bool $errorJsAlert        Force it
 70:      */
 71:     public function setErrorJsAlert($errorJsAlert) {
 72:         $this->_errorJsAlert = $errorJsAlert;
 73:     }
 74: 
 75:     /**
 76:      * Errors traced with the JavaScript function: alert()
 77:      * 
 78:      * @return type                     Is it
 79:      */
 80:     public function getErrorJsAlert() {
 81:         return $this->_errorJsAlert;
 82:     }
 83: 
 84:     /**
 85:      * Force the php echo to trace errors in the page.
 86:      * 
 87:      * @param bool $errorEcho       Force it
 88:      */
 89:     public function setErrorEcho($errorEcho) {
 90:         $this->_errorEcho = $errorEcho;
 91:     }
 92: 
 93:     /**
 94:      * Errors traced with the php function: echo
 95:      * 
 96:      * @return type                     Is it
 97:      */
 98:     public function getErrorEcho() {
 99:         return $this->_errorEcho;
100:     }
101: 
102:     private $_errorJsLog = true;
103: 
104:     /**
105:      * Force the JavaScript console.log() to output errors.
106:      * 
107:      * @param bool $errorEcho       Force it
108:      */
109:     public function setErrorJsLog($errorJsLog) {
110:         $this->_errorJsLog = $errorJsLog;
111:     }
112: 
113:     /**
114:      * Errors traced with the JavaScript function: console.log()
115:      * 
116:      * @return type                     Is it
117:      */
118:     public function getErrorJsLog() {
119:         return $this->_errorJsLog;
120:     }
121: 
122:     /**
123:      * Save an error message
124:      * 
125:      * @param string $msg       Error message (information about the error)
126:      */
127:     public function addError($msg) {
128:         $error = $msg . "\n" . $this->getDebugBacktrace(1, $this->_errorBackNum);
129:         array_push($this->_errorList, $error);
130:     }
131: 
132:     /**
133:      * Dispatch all errors messages.
134:      * Echo in the HTML (PHP), in alert() (JavaScript) or console.log() (JavaScript)
135:      */
136:     public function dispatchErrors() {
137:         if (count($this->_errorList) > 0) {
138:             if ($this->_errorFile) {
139:                 include_once _SYSTEM_DIRECTORY . 'helpers/miscellaneous/FileUtil.php';
140: 
141:                 if (!file_exists(_CACHE_DIRECTORY)) {
142:                     FileUtil::writeProtectedDir(_CACHE_DIRECTORY);
143:                 }
144: 
145:                 $fileName = _CACHE_DIRECTORY . $this->_errorFileName;
146:                 $file = fopen($fileName, 'a+');
147: 
148:                 $page = General::getInstance()->getCurrentPage();
149:                 fputs($file, "\r\n");
150:                 fputs($file, "\r\n");
151:                 fputs($file, "url: " . $page->getPageUrl());
152:                 fputs($file, "\r\n");
153:                 fputs($file, "id: " . $page->getId());
154:                 fputs($file, "\r\n");
155: 
156:                 foreach ($this->_errorList as $errorStr) {
157:                     //foreach (explode('\n', $errorStr) as $single) {
158:                     fputs($file, $errorStr);
159:                     fputs($file, "\r\n");
160:                     //}
161:                 }
162:                 //$datas .= implode( "\n", $this->_errorList );
163: 
164: 
165:                 fclose($file);
166:             }
167: 
168:             echo '<script>/*Errors*/';
169:             if ($this->_errorJsAlert) {
170:                 echo 'alert("' . $this->delDoubleQuotes(implode('\n', $this->_errorList)) . '");';
171:             }
172:             if ($this->_errorJsLog) {
173:                 echo 'console.log("' . $this->delDoubleQuotes(implode('\n', $this->_errorList)) . '");';
174:             }
175:             echo '</script>';
176: 
177:             if ($this->_errorEcho) {
178:                 echo $this->getErrorsHtml();
179:             }
180:         }
181:     }
182: 
183:     /**
184:      * Get the list of errors with html tags.
185:      * 
186:      * @return string
187:      */
188:     public function getErrorsHtml() {
189:         return $this->addHtmlReturns(implode('<br>', $this->_errorList));
190:     }
191: 
192:     /**
193:      * Add a time marker
194:      * 
195:      * @param type $msg     Description of the marker
196:      */
197:     public function addTimeMark($msg) {
198:         $dt = microtime(true) - $this->_timer;
199:         $this->_timer += $dt;
200:         $this->_totalTime += $dt;
201:         $this->_timerList[] = array('dt' => $dt, 'msg' => $msg);
202:     }
203: 
204:     /**
205:      * Get all the markers and the total time
206:      * 
207:      * @param string $msg       Message for the total time
208:      * @return string           All datas
209:      */
210:     public function getTimes($msg) {
211:         $this->_timerList[] = array('dt' => $this->_totalTime, 'msg' => 'total time: ' . $msg);
212: 
213:         $output = '';
214:         foreach ($this->_timerList as $value) {
215:             $output .= $value['msg'] . ': ' . number_format($value['dt'], 3) . 's' . "\n";
216:         }
217:         return $output;
218:     }
219: 
220:     private function addHtmlReturns($str) {
221:         return str_replace('\n', '<br>', $str);
222:     }
223: 
224:     private function delDoubleQuotes($str) {
225:         $str = str_replace(array('"', '\\'), array('\"', '\\\\'), $str);
226:         $str = str_replace(array('\\\n'), array('\n'), $str);
227:         return $str;
228:     }
229: 
230:     /**
231:      * Clear all datas without total timer.
232:      */
233:     public function clear() {
234:         $this->_errorList = array();
235:         $this->_timerList = array();
236:     }
237: 
238:     final private function __construct() {
239:         $this->clear();
240:         $this->_timer = microtime(true);
241:         $this->_totalTime = 0;
242:     }
243: 
244:     /**
245:      * Unclonable
246:      */
247:     final private function __clone() {
248:         if (_DEBUG) {
249:             Debug::getInstance()->addError('You can\'t clone a singleton');
250:         }
251:     }
252: 
253:     /**
254:      * Instance of the Debug object
255:      * 
256:      * @return self     Instance of the object Debug
257:      */
258:     final public static function getInstance() {
259:         if (!isset(self::$_INSTANCE)) {
260:             self::$_INSTANCE = new self();
261:         }
262: 
263:         return self::$_INSTANCE;
264:     }
265: 
266:     /**
267:      * Get the path of errors
268:      * 
269:      * @param int $traces_to_ignore     First trace (1 if you would escape the first function)
270:      * @param int $max_trace            Maximum of recover functions
271:      * @return string                   Resume of path error
272:      */
273:     protected function getDebugBacktrace($traces_to_ignore = 1, $max_trace = 1) {
274:         $traces = debug_backtrace();
275:         $ret = array();
276:         foreach ($traces as $i => $call) {
277:             if ($i < $traces_to_ignore || $i > $traces_to_ignore + $max_trace - 1) {
278:                 continue;
279:             }
280: 
281:             //$str .= $object.$call['function'].'('.implode(', ', $call['args']).') ';
282:             if (isset($call['file']) && isset($call['line'])) {
283:                 $str = '';
284:                 if ($max_trace > 1) {
285:                     $str .= '   #' . str_pad($i - $traces_to_ignore, 3, ' ');
286:                 }
287: 
288:                 $str .= '   ' . $call['file'] . ':' . $call['line'];
289:                 $ret[] = $str;
290:             }
291:         }
292:         return implode("\n", $ret);
293:     }
294: 
295: }
296: 
Flea API documentation generated by ApiGen