/
pakeException.class.php
91 lines (78 loc) · 2.54 KB
/
pakeException.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
/*
* This file is part of the pake package.
* (c) 2004, 2005 Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* pakeException is the base class for all pake related exceptions and
* provides an additional method for printing up a detailed view of an
* exception.
*
* @package pake
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @version SVN: $Id$
*/
class pakeException extends Exception
{
public static function strlen($string)
{
return function_exists('mb_strlen') ? mb_strlen($string) : strlen($string);
}
function render($e)
{
$title = ' ['.get_class($e).'] ';
$len = self::strlen($title);
$lines = array();
foreach (explode("\n", $e->getMessage()) as $line)
{
$lines[] = ' '.$line.' ';
$len = max(self::strlen($line) + 4, $len);
}
$messages = array(
str_repeat(' ', $len),
$title.str_repeat(' ', $len - self::strlen($title)),
);
foreach ($lines as $line)
{
$messages[] = $line.str_repeat(' ', $len - self::strlen($line));
}
$messages[] = str_repeat(' ', $len);
fwrite(STDERR, "\n");
foreach ($messages as $message)
{
fwrite(STDERR, pakeColor::colorize($message, 'ERROR', STDERR)."\n");
}
fwrite(STDERR, "\n");
$pake = pakeApp::get_instance();
if ($pake->get_trace())
{
fwrite(STDERR, "exception trace:\n");
$trace = $this->trace($e);
for ($i = 0, $count = count($trace); $i < $count; $i++)
{
$class = (isset($trace[$i]['class']) ? $trace[$i]['class'] : '');
$type = (isset($trace[$i]['type']) ? $trace[$i]['type'] : '');
$function = $trace[$i]['function'];
$file = isset($trace[$i]['file']) ? $trace[$i]['file'] : 'n/a';
$line = isset($trace[$i]['line']) ? $trace[$i]['line'] : 'n/a';
fwrite(STDERR, sprintf(" %s%s%s at %s:%s\n", $class, $type, $function, pakeColor::colorize($file, 'INFO', STDERR), pakeColor::colorize($line, 'INFO', STDERR)));
}
}
fwrite(STDERR, "\n");
}
function trace($exception)
{
// exception related properties
$trace = $exception->getTrace();
array_unshift($trace, array(
'function' => '',
'file' => ($exception->getFile() != null) ? $exception->getFile() : 'n/a',
'line' => ($exception->getLine() != null) ? $exception->getLine() : 'n/a',
'args' => array(),
));
return $trace;
}
}