PageRenderTime 13ms CodeModel.GetById 6ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

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