PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/Library/Kumbia/Debug/Debug.php

http://kumbia-enterprise.googlecode.com/
PHP | 304 lines | 149 code | 21 blank | 134 comment | 29 complexity | cf41b9afeff667ae16a449a5f62bed9e MD5 | raw file
  1. <?php
  2. /**
  3. * Kumbia Enterprise Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the New BSD License that is bundled
  8. * with this package in the file docs/LICENSE.txt.
  9. *
  10. * If you did not receive a copy of the license and are unable to
  11. * obtain it through the world-wide-web, please send an email
  12. * to license@loudertechnology.com so we can send you a copy immediately.
  13. *
  14. * @category Kumbia
  15. * @package Debug
  16. * @copyright Copyright (c) 2008-2009 Louder Technology COL. (http://www.loudertechnology.com)
  17. * @copyright Copyright (c) 2005-2009 Andres Felipe Gutierrez (gutierrezandresfelipe at gmail.com)
  18. * @license New BSD License
  19. * @version $Id: Debug.php 150 2010-09-24 16:20:10Z gutierrezandresfelipe $
  20. */
  21. /**
  22. * Debug
  23. *
  24. * Clase que facilita el debug de aplicaciones
  25. *
  26. * @category Kumbia
  27. * @package Debug
  28. * @copyright Copyright (c) 2008-2009 Louder Technology COL. (http://www.loudertechnology.com)
  29. * @copyright Copyright (c) 2005-2009 Andres Felipe Gutierrez (gutierrezandresfelipe at gmail.com)
  30. * @license New BSD License
  31. * @abstract
  32. */
  33. abstract class Debug {
  34. /**
  35. * Mensajes de Debug
  36. *
  37. * @var array
  38. */
  39. private static $_messages = array();
  40. /**
  41. * Memoria de Variables
  42. *
  43. * @var array
  44. */
  45. private static $_variables = array();
  46. /**
  47. * Handlers de debugs a archivos
  48. *
  49. * @var array
  50. */
  51. private static $_fileDebugs = array();
  52. /**
  53. * Accion para generar una excepcion al terminar la peticion
  54. *
  55. */
  56. const ACTION_HALT = 0;
  57. /**
  58. * Accion para generar una excepcion un log al terminar
  59. *
  60. */
  61. const ACTION_LOG = 1;
  62. /**
  63. * Almacena un mensaje para seguimiento
  64. *
  65. * @param string $value
  66. */
  67. static public function add($value, $completeBackTrace=false){
  68. $backtrace = debug_backtrace();
  69. if($value===null){
  70. $value = 'NULL';
  71. }
  72. if($value===true){
  73. $value = 'TRUE';
  74. }
  75. if($value===false){
  76. $value = 'FALSE';
  77. }
  78. if(is_resource($value)){
  79. $value = '<'.get_resource_type($value).'> '.((string) $value);
  80. }
  81. if(isset($backtrace[0])){
  82. self::$_messages[] = array(
  83. 'backtrace' => $backtrace,
  84. 'completeBacktrace' => $completeBackTrace,
  85. 'class' => isset($backtrace[1]['class']) ? $backtrace[1]['class'] : '',
  86. 'function' => $backtrace[1]['function'],
  87. 'line' => $backtrace[0]['line'],
  88. 'file' => $backtrace[0]['file'],
  89. 'time' => microtime(true),
  90. 'value' => $value
  91. );
  92. } else {
  93. self::$_messages[] = array(
  94. 'backtrace' => $backtrace,
  95. 'completeBacktrace' => $completeBackTrace,
  96. 'class' => '',
  97. 'function' => '',
  98. 'line' => '',
  99. 'file' => '',
  100. 'time' => microtime(true),
  101. 'value' => $value
  102. );
  103. }
  104. }
  105. /**
  106. * Obtiene los mensajes de debug creados
  107. *
  108. * @return unknown
  109. */
  110. static public function getMessages(){
  111. return self::$_messages;
  112. }
  113. /**
  114. * Obtiene las variables de memoria
  115. *
  116. * @return array
  117. */
  118. static public function getMemory(){
  119. return self::$_variables;
  120. }
  121. /**
  122. * Genera una cadena con el llamado a un metodo o funcion
  123. *
  124. * @param array $backtrace
  125. */
  126. static public function getFunctionCallAsString($backtrace){
  127. $arguments = array();
  128. foreach($backtrace['args'] as $arg){
  129. if($arg===false){
  130. $arguments[] = 'FALSE';
  131. }
  132. if($arg===true){
  133. $arguments[] = 'TRUE';
  134. }
  135. if($arg===null){
  136. $arguments[] = 'NULL';
  137. }
  138. if(is_array($arg)){
  139. $arguments[] = 'Array';
  140. }
  141. if(is_numeric($arg)){
  142. $arguments[] = $arg;
  143. }
  144. if(is_resource($arg)){
  145. $arguments[] = '<'.get_resource_type($arg).'>'.((string) $arg);
  146. }
  147. if(is_object($arg)){
  148. $arguments[] = '<'.get_class($arg).">";
  149. }
  150. if(is_string($arg)){
  151. $arguments[] = "\"$arg\"";
  152. }
  153. }
  154. if(isset($backtrace['class'])){
  155. return $backtrace['class']."::".$backtrace['function']."(".join(", ", $arguments).")";
  156. } else {
  157. return $backtrace['function'].'()';
  158. }
  159. }
  160. /**
  161. * Asercion si son iguales
  162. *
  163. * @param mixed $val1
  164. * @param mixed $val2
  165. * @param boolean $showTrace
  166. * @throws DebugException
  167. */
  168. public static function assertEquals($val1, $val2, $showTrace=false){
  169. Debug::add('assetEquals - '.$val1." ".$val2, $showTrace);
  170. if($val1==$val2){
  171. throw new DebugException("assetEquals - ".$val1." ".$val2);
  172. }
  173. }
  174. /**
  175. * Asercion si no son iguales
  176. *
  177. * @param mixed $val1
  178. * @param mixed $val2
  179. * @param boolean $showTrace
  180. * @throws DebugException
  181. */
  182. public static function assertNotEquals($val1, $val2, $showTrace=false){
  183. Debug::add("assetNotEquals - ".$val1." ".$val2, $showTrace);
  184. if($val1!=$val2){
  185. throw new DebugException("assetNotEquals - ".$val1." ".$val2);
  186. }
  187. }
  188. /**
  189. * Asercion si $val1 es verdadero
  190. *
  191. * @param mixed $val1
  192. * @param mixed $val2
  193. * @param boolean $showTrace
  194. * @throws DebugException
  195. */
  196. public static function assertTrue($val1, $showTrace=false){
  197. Debug::add("assetTrue - ".$val1, $showTrace);
  198. if($val1==true){
  199. throw new DebugException("assetTrue - ".$val1);
  200. }
  201. }
  202. /**
  203. * Asercion si $val1 es falso
  204. *
  205. * @param mixed $val1
  206. * @param mixed $val2
  207. * @param boolean $showTrace
  208. * @throws DebugException
  209. */
  210. public static function assertFalse($val1, $showTrace=false){
  211. Debug::add("assetTrue - ".$val1, $showTrace);
  212. if($val1==false){
  213. throw new DebugException("assetTrue - ".$val1);
  214. }
  215. }
  216. /**
  217. * Valores de Memoria
  218. *
  219. * @param string $varname
  220. * @param mixed $value
  221. */
  222. public static function addVariable($varname, $value){
  223. if($value===null){
  224. $value = 'NULL';
  225. }
  226. if($value===true){
  227. $value = 'TRUE';
  228. }
  229. if($value===false){
  230. $value = 'FALSE';
  231. }
  232. if(is_array($value)){
  233. $value = '<Array> '.print_r($value, true);
  234. }
  235. if(is_object($value)){
  236. #if(method_exists($value, 'inspect')){
  237. # $value = $value->inspect();
  238. #} else {
  239. $value = '<'.get_class($value).'>';
  240. #}
  241. }
  242. if(is_resource($value)){
  243. $value = '<'.get_resource_type($value)."> ".((string) $value);
  244. }
  245. self::$_variables[$varname] = $value;
  246. }
  247. /**
  248. * Detiene la aplicacion generando un excepcion de Debug
  249. *
  250. * @param int $action
  251. * @param array $options
  252. */
  253. public static function setActionOnFinish($action, $options=array()){
  254. if($action==self::ACTION_HALT){
  255. EventManager::attachEvent(new Event('finishRequest', array('Debug', 'haltRequest')));
  256. }
  257. if($action==self::ACTION_LOG){
  258. }
  259. }
  260. /**
  261. * Termina la ejecuci?n de la petici?n
  262. *
  263. * @throws DebugException
  264. */
  265. public static function haltRequest(){
  266. $exception = new DebugException('Visualizando entorno de seguimiento');
  267. $exception->setUserCatchable(false);
  268. throw $exception;
  269. }
  270. /**
  271. * Envia un debug a un archivo
  272. *
  273. * @param string $filePath
  274. * @param mixed $value
  275. * @param string $flags
  276. */
  277. public static function addToFile($filePath, $value, $flags='w'){
  278. if(!isset(self::$_fileDebugs[$filePath])){
  279. self::$_fileDebugs[$filePath] = fopen($filePath, $flags);
  280. }
  281. fputs(self::$_fileDebugs[$filePath], print_r($value, true));
  282. }
  283. }