/framework/core/utils/BacktraceViewCli.php

http://zoop.googlecode.com/ · PHP · 115 lines · 103 code · 12 blank · 0 comment · 8 complexity · bb3014c5a67174bf83e2b200ec6060de MD5 · raw file

  1. <?php
  2. class BacktraceViewCli
  3. {
  4. private $info, $padding, $formatted;
  5. function __construct($info)
  6. {
  7. $this->info = $info;
  8. $this->padding = 4;
  9. }
  10. function display()
  11. {
  12. $this->formatFields();
  13. $maxLengths = $this->getMaxLengths();
  14. $fields = array('file', 'line', 'function');
  15. foreach($this->formatted as $thisRow)
  16. {
  17. $parts = array();
  18. foreach($fields as $thisField)
  19. {
  20. $padside = $thisField == 'file' ? STR_PAD_LEFT : STR_PAD_RIGHT;
  21. $padding = $thisField == 'function' ? '' : str_pad('', $this->padding, ' ');
  22. if(isset($thisRow[$thisField]))
  23. {
  24. if($thisField == 'function')
  25. $parts[] = $thisRow[$thisField];
  26. else
  27. $parts[] = str_pad($thisRow[$thisField], $maxLengths[$thisField], ' ', $padside) . $padding;
  28. }
  29. }
  30. echo implode('', $parts) . "\n";
  31. }
  32. }
  33. function getMaxLengths()
  34. {
  35. $maxLengths = array('file' => 0, 'line' => 0, 'function' => 0);
  36. foreach($this->formatted as $thisRow)
  37. {
  38. foreach($maxLengths as $field => $max)
  39. {
  40. if(isset($thisRow[$field]) && strlen($thisRow[$field]) > $max)
  41. $maxLengths[$field] = strlen($thisRow[$field]);
  42. }
  43. }
  44. return $maxLengths;
  45. }
  46. function formatFields()
  47. {
  48. $this->formatted = array();
  49. foreach($this->info as $thisRow)
  50. {
  51. $lineInfo = $this->formatLine($thisRow);
  52. $this->formatted[] = $lineInfo;
  53. }
  54. }
  55. function formatLine($lineInfo)
  56. {
  57. $result = array();
  58. $result['file'] = isset($lineInfo['file']) ? $lineInfo['file'] : 'php function';
  59. $result['line'] = isset($lineInfo['line']) ? $lineInfo['line'] : 'na';
  60. $result['function'] = $this->formatFunctionInfo($lineInfo);
  61. return $result;
  62. }
  63. function formatFunctionInfo($lineInfo)
  64. {
  65. $call = '';
  66. $call .= isset($lineInfo['class']) ? ($lineInfo['class'] . $lineInfo['type']) : '';
  67. $call .= $lineInfo['function'] . '(';
  68. $argStrings = array();
  69. if(isset($lineInfo['args']))
  70. {
  71. foreach($lineInfo['args'] as $thisArg)
  72. {
  73. switch(gettype($thisArg))
  74. {
  75. case 'string':
  76. $argStrings[] = '"' . $thisArg . '"';
  77. break;
  78. case 'integer':
  79. $argStrings[] = $thisArg;
  80. break;
  81. case 'array':
  82. $argStrings[] = '<array>';
  83. break;
  84. case 'resource':
  85. $argStrings[] = 'resource: ' . $thisArg;
  86. break;
  87. case 'boolean':
  88. $argStrings[] = 'boolean: -' . $thisArg . '-';
  89. break;
  90. case 'NULL':
  91. $argStrings[] = 'NULL';
  92. break;
  93. default:
  94. die('unhandled type ' . gettype($thisArg));
  95. break;
  96. }
  97. }
  98. }
  99. $call .= implode(', ', $argStrings);
  100. $call .= ')';
  101. return $call;
  102. }
  103. }