1<?php
2/**
3 * Customize error renderer with PEAR_ErrorStack and PEAR::Log
4 *
5 * PHP versions 4 and 5
6 *
7 * @category   HTML
8 * @package    HTML_CSS
9 * @subpackage Examples
10 * @author     Klaus Guenther <klaus@capitalfocus.org>
11 * @author     Laurent Laville <pear@laurent-laville.org>
12 * @copyright  2005-2009 Klaus Guenther, Laurent Laville
13 * @license    http://www.opensource.org/licenses/bsd-license.php  BSD
14 * @version    CVS: $Id: css_errorstack_logger.php,v 1.9 2009/01/19 23:22:39 farell Exp $
15 * @link       http://pear.php.net/package/HTML_CSS
16 * @since      File available since Release 1.0.0RC1
17 * @ignore
18 */
19
20require_once 'HTML/CSS.php';
21require_once 'HTML/CSS/Error.php';
22require_once 'PEAR/ErrorStack.php';
23require_once 'Log.php';
24
25/**
26 * This class creates a css error stack object with help of PEAR_ErrorStack
27 *
28 * PHP versions 4 and 5
29 *
30 * @category   HTML
31 * @package    HTML_CSS
32 * @subpackage Examples
33 * @author     Klaus Guenther <klaus@capitalfocus.org>
34 * @author     Laurent Laville <pear@laurent-laville.org>
35 * @copyright  2005-2009 Klaus Guenther, Laurent Laville
36 * @license    http://www.opensource.org/licenses/bsd-license.php  BSD
37 * @link       http://pear.php.net/package/HTML_CSS
38 * @since      File available since Release 1.0.0RC1
39 * @ignore
40 */
41class HTML_CSS_ErrorStack
42{
43    /**
44     * HTML_CSS_ErrorStack class constructor
45     */
46    function HTML_CSS_ErrorStack()
47    {
48        $s = &PEAR_ErrorStack::singleton('HTML_CSS');
49        $t = HTML_CSS_Error::_getErrorMessage();
50        $s->setErrorMessageTemplate($t);
51        $s->setMessageCallback(array(&$this,'getMessage'));
52        $s->setContextCallback(array(&$this,'getBacktrace'));
53
54        $ident = $_SERVER['REMOTE_ADDR'];
55        $conf  = array('lineFormat' => '%1$s - %2$s [%3$s] %4$s');
56        $file  = &Log::singleton('file', 'html_css_err.log', $ident, $conf);
57
58        $conf    = array('error_prepend' => '<font color="#ff0000"><tt>',
59                         'error_append'  => '</tt></font>');
60        $display = &Log::singleton('display', '', '', $conf);
61
62        $composite = &Log::singleton('composite');
63        $composite->addChild($display);
64        $composite->addChild($file);
65
66        $s->setLogger($composite);
67        $s->pushCallback(array(&$this,'errorHandler'));
68    }
69
70    /**
71     * Add an error to the HTML_CSS error stack
72     *
73     * @param int    $code   Package-specific error code
74     * @param string $level  Error level.  This is NOT spell-checked
75     * @param array  $params Associative array of error parameters
76     *
77     * @return PEAR_Error|array if compatibility mode is on
78     */
79    function push($code, $level, $params)
80    {
81        $s = &PEAR_ErrorStack::singleton('HTML_CSS');
82        return $s->push($code, $level, $params);
83    }
84
85    /**
86     * Default ErrorStack message callback is
87     * PEAR_ErrorStack::getErrorMessage()
88     *
89     * @param PEAR_ErrorStack &$stack   error stack
90     * @param array           $err      user info error with call context
91     * @param string|false    $template Pre-generated error message template
92     *
93     * @return string
94     */
95    function getMessage(&$stack, $err, $template = false)
96    {
97        global $prefs;
98
99        $message = $stack->getErrorMessage($stack, $err, $template);
100
101        $lineFormat    = '%1$s %2$s [%3$s]';
102        $contextFormat = 'in %1$s on line %2$s';
103
104        if (isset($prefs['handler']['display']['lineFormat'])) {
105            $lineFormat = $prefs['handler']['display']['lineFormat'];
106            $lineFormat = strip_tags($lineFormat);
107        }
108        if (isset($prefs['handler']['display']['contextFormat'])) {
109            $contextFormat = $prefs['handler']['display']['contextFormat'];
110            $contextFormat = strip_tags($contextFormat);
111        }
112
113        $context = $err['context'];
114
115        if ($context) {
116            $file = $context['file'];
117            $line = $context['line'];
118
119            $contextExec = sprintf($contextFormat, $file, $line);
120        } else {
121            $contextExec = '';
122        }
123
124        $msg = sprintf($lineFormat, '', $message, $contextExec);
125        return trim($msg);
126    }
127
128    /**
129     * Get the call backtrace from where the error was generated.
130     *
131     * @return mixed bool|array
132     */
133    function getBacktrace()
134    {
135        if (function_exists('debug_backtrace')) {
136            $backtrace = debug_backtrace();
137            $backtrace = $backtrace[count($backtrace)-1];
138        } else {
139            $backtrace = false;
140        }
141        return $backtrace;
142    }
143
144    /**
145     * Error Callback used by PEAR_ErrorStack on each error raised
146     *
147     * @param array $err user info error with call context
148     *
149     * @return void|int
150     */
151    function errorHandler($err)
152    {
153        global $halt_onException;
154
155        if ($halt_onException) {
156            if ($err['level'] == 'exception') {
157                return PEAR_ERRORSTACK_DIE;
158            }
159        }
160    }
161}
162
163// set it to on if you want to halt script on any exception
164$halt_onException = false;
165
166
167// Example A. ---------------------------------------------
168
169$stack =& new HTML_CSS_ErrorStack();
170
171$attribs = array();
172$prefs   = array('error_handler' => array(&$stack, 'push'));
173
174// A1. Error
175$css1 = new HTML_CSS($attribs, $prefs);
176
177$group1 = $css1->createGroup('body, html', 'grp1');
178$group2 = $css1->createGroup('p, html', 'grp1');
179
180
181// Example B. ---------------------------------------------
182
183$displayConfig = array(
184    'lineFormat' => '<b>%1$s</b>: %2$s<br/>%3$s<hr/>',
185    'contextFormat' =>   '<b>File:</b> %1$s <br/>'
186                       . '<b>Line:</b> %2$s '
187);
188$attribs       = array();
189$prefs         = array(
190    'error_handler' => array(&$stack, 'push'),
191    'handler' => array('display' => $displayConfig)
192);
193
194$css2 = new HTML_CSS($attribs, $prefs);
195
196// B1. Error
197$css2->getStyle('h1', 'class');
198
199// B2. Exception
200$css2->setXhtmlCompliance('true');
201
202print 'still alive !';
203?>