PageRenderTime 44ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/active_support/models/debug.php

https://github.com/bermi/akelos
PHP | 127 lines | 107 code | 11 blank | 9 comment | 31 complexity | a07c55374d41a5862d81a94acbd4e760 MD5 | raw file
  1. <?php
  2. # This file is part of the Akelos Framework
  3. # (Copyright) 2004-2010 Bermi Ferrer bermi a t bermilabs com
  4. # See LICENSE and CREDITS for details
  5. class AkModelDebug extends AkModelExtenssion
  6. {
  7. /**
  8. * Toggles SQL query output
  9. */
  10. public function dbug() {
  11. if(!$this->_Model->isConnected()){
  12. $this->_Model->establishConnection();
  13. }
  14. $this->_Model->getAdapter()->connection->debug = $this->_Model->getAdapter()->connection->debug ? false : true;
  15. $this->_Model->db_debug = $this->_Model->getAdapter()->connection->debug;
  16. }
  17. /**
  18. * Displays a tring representation of the model for debugging.
  19. */
  20. public function toString($print = false) {
  21. $result = '';
  22. if(!AK_CLI || (AK_ENVIRONMENT == 'testing' && !AK_CLI)){
  23. $result = "<h2>Details for ".AkInflector::humanize(AkInflector::underscore($this->_Model->getModelName()))." with ".$this->_Model->getPrimaryKey()." ".$this->_Model->getId()."</h2>\n<dl>\n";
  24. foreach ($this->_Model->getColumnNames() as $column=>$caption){
  25. $result .= "<dt>$caption</dt>\n<dd>".$this->_Model->getAttribute($column)."</dd>\n";
  26. }
  27. $result .= "</dl>\n<hr />";
  28. if($print){
  29. echo $result;
  30. }
  31. }elseif(AK_DEV_MODE){
  32. $result = "\n".
  33. str_replace("\n"," ",var_export($this->_Model->getAttributes(),true));
  34. $result .= "\n";
  35. echo $result;
  36. return '';
  37. }elseif (AK_CLI){
  38. $result = "\n-------\n Details for ".AkInflector::humanize(AkInflector::underscore($this->_Model->getModelName()))." with ".$this->_Model->getPrimaryKey()." ".$this->_Model->getId()." ==\n\n/==\n";
  39. foreach ($this->_Model->getColumnNames() as $column=>$caption){
  40. $result .= "\t * $caption: ".$this->_Model->getAttribute($column)."\n";
  41. }
  42. $result .= "\n\n-------\n";
  43. if($print){
  44. echo $result;
  45. }
  46. }
  47. return $result;
  48. }
  49. public function dbugging($trace_this_on_debug_mode = null) {
  50. if(!empty($this->_Model->getAdapter()->debug) && !empty($trace_this_on_debug_mode)){
  51. $message = !is_scalar($trace_this_on_debug_mode) ? var_export($trace_this_on_debug_mode, true) : (string)$trace_this_on_debug_mode;
  52. AkDebug::trace($message);
  53. }
  54. return !empty($this->_Model->getAdapter()->debug);
  55. }
  56. public function debug ($data = 'active_record_class', $_functions=0) {
  57. if(!AK_DEBUG && !AK_DEV_MODE){
  58. return;
  59. }
  60. $data = $data == 'active_record_class' ? clone($this->_Model) : $data;
  61. if($_functions!=0) {
  62. $sf=1;
  63. } else {
  64. $sf=0 ;
  65. }
  66. if (isset ($data)) {
  67. if (is_array($data) || is_object($data)) {
  68. if (count ($data)) {
  69. echo AK_CLI ? "/--\n" : "<ol>\n";
  70. while (list ($key,$value) = each ($data)) {
  71. if($key{0} == '_'){
  72. continue;
  73. }
  74. $type=gettype($value);
  75. if ($type=="array") {
  76. AK_CLI ? printf ("\t* (%s) %s:\n",$type, $key) :
  77. printf ("<li>(%s) <b>%s</b>:\n",$type, $key);
  78. ob_start();
  79. AkDebug::debug ($value,$sf);
  80. $lines = explode("\n",ob_get_clean()."\n");
  81. foreach ($lines as $line){
  82. echo "\t".$line."\n";
  83. }
  84. }elseif($type == "object"){
  85. if(method_exists($value,'hasColumn') && $value->hasColumn($key)){
  86. $value->toString(true);
  87. AK_CLI ? printf ("\t* (%s) %s:\n",$type, $key) :
  88. printf ("<li>(%s) <b>%s</b>:\n",$type, $key);
  89. ob_start();
  90. AkDebug::debug ($value,$sf);
  91. $lines = explode("\n",ob_get_clean()."\n");
  92. foreach ($lines as $line){
  93. echo "\t".$line."\n";
  94. }
  95. }
  96. }elseif (stristr($type, "function")) {
  97. if ($sf) {
  98. AK_CLI ? printf ("\t* (%s) %s:\n",$type, $key, $value) :
  99. printf ("<li>(%s) <b>%s</b> </li>\n",$type, $key, $value);
  100. }
  101. } else {
  102. if (!$value) {
  103. $value = "(none)";
  104. }
  105. AK_CLI ? printf ("\t* (%s) %s = %s\n",$type, $key, $value) :
  106. printf ("<li>(%s) <b>%s</b> = %s</li>\n",$type, $key, $value);
  107. }
  108. }
  109. echo AK_CLI ? "\n--/\n" : "</ol>fin.\n";
  110. } else {
  111. echo "(empty)";
  112. }
  113. }
  114. }
  115. }
  116. }