Subversion Repositories Applications.gtt

Rev

Rev 94 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 94 Rev 187
Line 19... Line 19...
19
 * Since version PEAR1.3.2, ErrorStack no longer instantiates an exception class.  This can
19
 * Since version PEAR1.3.2, ErrorStack no longer instantiates an exception class.  This can
20
 * still be done quite handily in an error callback or by manipulating the returned array
20
 * still be done quite handily in an error callback or by manipulating the returned array
21
 * @category   Debugging
21
 * @category   Debugging
22
 * @package    PEAR_ErrorStack
22
 * @package    PEAR_ErrorStack
23
 * @author     Greg Beaver <cellog@php.net>
23
 * @author     Greg Beaver <cellog@php.net>
24
 * @copyright  2004-2006 Greg Beaver
24
 * @copyright  2004-2008 Greg Beaver
25
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
25
 * @license    http://opensource.org/licenses/bsd-license.php New BSD License
26
 * @version    CVS: $Id: ErrorStack.php,v 1.26 2006/10/31 02:54:40 cellog Exp $
-
 
27
 * @link       http://pear.php.net/package/PEAR_ErrorStack
26
 * @link       http://pear.php.net/package/PEAR_ErrorStack
28
 */
27
 */
Line 29... Line 28...
29
 
28
 
30
/**
29
/**
Line 130... Line 129...
130
 * $global_stack = &PEAR_ErrorStack::singleton('MyPackage');
129
 * $global_stack = &PEAR_ErrorStack::singleton('MyPackage');
131
 * // local error stack
130
 * // local error stack
132
 * $local_stack = new PEAR_ErrorStack('MyPackage');
131
 * $local_stack = new PEAR_ErrorStack('MyPackage');
133
 * </code>
132
 * </code>
134
 * @author     Greg Beaver <cellog@php.net>
133
 * @author     Greg Beaver <cellog@php.net>
135
 * @version    1.5.1
134
 * @version    1.10.1
136
 * @package    PEAR_ErrorStack
135
 * @package    PEAR_ErrorStack
137
 * @category   Debugging
136
 * @category   Debugging
138
 * @copyright  2004-2006 Greg Beaver
137
 * @copyright  2004-2008 Greg Beaver
139
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
138
 * @license    http://opensource.org/licenses/bsd-license.php New BSD License
140
 * @version    CVS: $Id: ErrorStack.php,v 1.26 2006/10/31 02:54:40 cellog Exp $
-
 
141
 * @link       http://pear.php.net/package/PEAR_ErrorStack
139
 * @link       http://pear.php.net/package/PEAR_ErrorStack
142
 */
140
 */
143
class PEAR_ErrorStack {
141
class PEAR_ErrorStack {
144
    /**
142
    /**
145
     * Errors are stored in the order that they are pushed on the stack.
143
     * Errors are stored in the order that they are pushed on the stack.
Line 192... Line 190...
192
     * configuration file, or in a database query.
190
     * configuration file, or in a database query.
193
     * @var false|string|array
191
     * @var false|string|array
194
     * @access protected
192
     * @access protected
195
     */
193
     */
196
    var $_contextCallback = false;
194
    var $_contextCallback = false;
197
    
195
 
198
    /**
196
    /**
199
     * If set to a valid callback, this will be called every time an error
197
     * If set to a valid callback, this will be called every time an error
200
     * is pushed onto the stack.  The return value will be used to determine
198
     * is pushed onto the stack.  The return value will be used to determine
201
     * whether to allow an error to be pushed or logged.
199
     * whether to allow an error to be pushed or logged.
202
     * 
200
     *
203
     * The return value must be one an PEAR_ERRORSTACK_* constant
201
     * The return value must be one an PEAR_ERRORSTACK_* constant
204
     * @see PEAR_ERRORSTACK_PUSHANDLOG, PEAR_ERRORSTACK_PUSH, PEAR_ERRORSTACK_LOG
202
     * @see PEAR_ERRORSTACK_PUSHANDLOG, PEAR_ERRORSTACK_PUSH, PEAR_ERRORSTACK_LOG
205
     * @var false|string|array
203
     * @var false|string|array
206
     * @access protected
204
     * @access protected
207
     */
205
     */
208
    var $_errorCallback = array();
206
    var $_errorCallback = array();
209
    
207
 
210
    /**
208
    /**
211
     * PEAR::Log object for logging errors
209
     * PEAR::Log object for logging errors
212
     * @var false|Log
210
     * @var false|Log
213
     * @access protected
211
     * @access protected
214
     */
212
     */
215
    var $_logger = false;
213
    var $_logger = false;
216
    
214
 
217
    /**
215
    /**
218
     * Error messages - designed to be overridden
216
     * Error messages - designed to be overridden
219
     * @var array
217
     * @var array
220
     * @abstract
218
     * @abstract
221
     */
219
     */
222
    var $_errorMsgs = array();
220
    var $_errorMsgs = array();
223
    
221
 
224
    /**
222
    /**
225
     * Set up a new error stack
223
     * Set up a new error stack
226
     * 
224
     *
227
     * @param string   $package name of the package this error stack represents
225
     * @param string   $package name of the package this error stack represents
228
     * @param callback $msgCallback callback used for error message generation
226
     * @param callback $msgCallback callback used for error message generation
229
     * @param callback $contextCallback callback used for context generation,
227
     * @param callback $contextCallback callback used for context generation,
230
     *                 defaults to {@link getFileLine()}
228
     *                 defaults to {@link getFileLine()}
231
     * @param boolean  $throwPEAR_Error
229
     * @param boolean  $throwPEAR_Error
232
     */
230
     */
233
    function PEAR_ErrorStack($package, $msgCallback = false, $contextCallback = false,
231
    function __construct($package, $msgCallback = false, $contextCallback = false,
234
                         $throwPEAR_Error = false)
232
                         $throwPEAR_Error = false)
235
    {
233
    {
236
        $this->_package = $package;
234
        $this->_package = $package;
237
        $this->setMessageCallback($msgCallback);
235
        $this->setMessageCallback($msgCallback);
238
        $this->setContextCallback($contextCallback);
236
        $this->setContextCallback($contextCallback);
Line 248... Line 246...
248
     * @param callback $msgCallback callback used for error message generation
246
     * @param callback $msgCallback callback used for error message generation
249
     * @param callback $contextCallback callback used for context generation,
247
     * @param callback $contextCallback callback used for context generation,
250
     *                 defaults to {@link getFileLine()}
248
     *                 defaults to {@link getFileLine()}
251
     * @param boolean  $throwPEAR_Error
249
     * @param boolean  $throwPEAR_Error
252
     * @param string   $stackClass class to instantiate
250
     * @param string   $stackClass class to instantiate
253
     * @static
251
     *
254
     * @return PEAR_ErrorStack
252
     * @return PEAR_ErrorStack
255
     */
253
     */
-
 
254
    public static function &singleton(
256
    function &singleton($package, $msgCallback = false, $contextCallback = false,
255
        $package, $msgCallback = false, $contextCallback = false,
257
                         $throwPEAR_Error = false, $stackClass = 'PEAR_ErrorStack')
256
        $throwPEAR_Error = false, $stackClass = 'PEAR_ErrorStack'
258
    {
257
    ) {
259
        if (isset($GLOBALS['_PEAR_ERRORSTACK_SINGLETON'][$package])) {
258
        if (isset($GLOBALS['_PEAR_ERRORSTACK_SINGLETON'][$package])) {
260
            return $GLOBALS['_PEAR_ERRORSTACK_SINGLETON'][$package];
259
            return $GLOBALS['_PEAR_ERRORSTACK_SINGLETON'][$package];
261
        }
260
        }
262
        if (!class_exists($stackClass)) {
261
        if (!class_exists($stackClass)) {
263
            if (function_exists('debug_backtrace')) {
262
            if (function_exists('debug_backtrace')) {
Line 295... Line 294...
295
    }
294
    }
Line 296... Line 295...
296
    
295
    
297
    /**
296
    /**
298
     * Set up a PEAR::Log object for all error stacks that don't have one
297
     * Set up a PEAR::Log object for all error stacks that don't have one
299
     * @param Log $log 
-
 
300
     * @static
298
     * @param Log $log 
301
     */
299
     */
302
    function setDefaultLogger(&$log)
300
    public static function setDefaultLogger(&$log)
303
    {
301
    {
304
        if (is_object($log) && method_exists($log, 'log') ) {
302
        if (is_object($log) && method_exists($log, 'log') ) {
305
            $GLOBALS['_PEAR_ERRORSTACK_DEFAULT_LOGGER'] = &$log;
303
            $GLOBALS['_PEAR_ERRORSTACK_DEFAULT_LOGGER'] = &$log;
306
        } elseif (is_callable($log)) {
304
        } elseif (is_callable($log)) {
307
            $GLOBALS['_PEAR_ERRORSTACK_DEFAULT_LOGGER'] = &$log;
305
            $GLOBALS['_PEAR_ERRORSTACK_DEFAULT_LOGGER'] = &$log;
308
	}
306
        }
Line 309... Line 307...
309
    }
307
    }
310
    
308
    
311
    /**
309
    /**
Line 356... Line 354...
356
     * 
354
     * 
357
     * This method sets the callback that can be used to generate error
355
     * This method sets the callback that can be used to generate error
358
     * messages for a singleton
356
     * messages for a singleton
359
     * @param array|string Callback function/method
357
     * @param array|string Callback function/method
360
     * @param string Package name, or false for all packages
358
     * @param string Package name, or false for all packages
361
     * @static
-
 
362
     */
359
     */
363
    function setDefaultCallback($callback = false, $package = false)
360
    public static function setDefaultCallback($callback = false, $package = false)
364
    {
361
    {
365
        if (!is_callable($callback)) {
362
        if (!is_callable($callback)) {
366
            $callback = false;
363
            $callback = false;
367
        }
364
        }
368
        $package = $package ? $package : '*';
365
        $package = $package ? $package : '*';
Line 430... Line 427...
430
     * Use this to temporarily disable all existing callbacks (can be used
427
     * Use this to temporarily disable all existing callbacks (can be used
431
     * to emulate the @ operator, for instance)
428
     * to emulate the @ operator, for instance)
432
     * @see PEAR_ERRORSTACK_PUSHANDLOG, PEAR_ERRORSTACK_PUSH, PEAR_ERRORSTACK_LOG
429
     * @see PEAR_ERRORSTACK_PUSHANDLOG, PEAR_ERRORSTACK_PUSH, PEAR_ERRORSTACK_LOG
433
     * @see staticPopCallback(), pushCallback()
430
     * @see staticPopCallback(), pushCallback()
434
     * @param string|array $cb
431
     * @param string|array $cb
435
     * @static
-
 
436
     */
432
     */
437
    function staticPushCallback($cb)
433
    public static function staticPushCallback($cb)
438
    {
434
    {
439
        array_push($GLOBALS['_PEAR_ERRORSTACK_OVERRIDE_CALLBACK'], $cb);
435
        array_push($GLOBALS['_PEAR_ERRORSTACK_OVERRIDE_CALLBACK'], $cb);
440
    }
436
    }
Line 441... Line 437...
441
    
437
    
442
    /**
438
    /**
443
     * Remove a temporary overriding error callback
439
     * Remove a temporary overriding error callback
444
     * @see staticPushCallback()
440
     * @see staticPushCallback()
445
     * @return array|string|false
-
 
446
     * @static
441
     * @return array|string|false
447
     */
442
     */
448
    function staticPopCallback()
443
    public static function staticPopCallback()
449
    {
444
    {
450
        $ret = array_pop($GLOBALS['_PEAR_ERRORSTACK_OVERRIDE_CALLBACK']);
445
        $ret = array_pop($GLOBALS['_PEAR_ERRORSTACK_OVERRIDE_CALLBACK']);
451
        if (!is_array($GLOBALS['_PEAR_ERRORSTACK_OVERRIDE_CALLBACK'])) {
446
        if (!is_array($GLOBALS['_PEAR_ERRORSTACK_OVERRIDE_CALLBACK'])) {
452
            $GLOBALS['_PEAR_ERRORSTACK_OVERRIDE_CALLBACK'] = array();
447
            $GLOBALS['_PEAR_ERRORSTACK_OVERRIDE_CALLBACK'] = array();
Line 602... Line 597...
602
     * @param array  $backtrace Protected parameter: use this to pass in the
597
     * @param array  $backtrace Protected parameter: use this to pass in the
603
     *                          {@link debug_backtrace()} that should be used
598
     *                          {@link debug_backtrace()} that should be used
604
     *                          to find error context
599
     *                          to find error context
605
     * @return PEAR_Error|array if compatibility mode is on, a PEAR_Error is also
600
     * @return PEAR_Error|array if compatibility mode is on, a PEAR_Error is also
606
     *                          thrown.  see docs for {@link push()}
601
     *                          thrown.  see docs for {@link push()}
607
     * @static
-
 
608
     */
602
     */
-
 
603
    public static function staticPush(
609
    function staticPush($package, $code, $level = 'error', $params = array(),
604
        $package, $code, $level = 'error', $params = array(),
610
                        $msg = false, $repackage = false, $backtrace = false)
605
        $msg = false, $repackage = false, $backtrace = false
611
    {
606
    ) {
612
        $s = &PEAR_ErrorStack::singleton($package);
607
        $s = &PEAR_ErrorStack::singleton($package);
613
        if ($s->_contextCallback) {
608
        if ($s->_contextCallback) {
614
            if (!$backtrace) {
609
            if (!$backtrace) {
615
                if (function_exists('debug_backtrace')) {
610
                if (function_exists('debug_backtrace')) {
616
                    $backtrace = debug_backtrace();
611
                    $backtrace = debug_backtrace();
Line 748... Line 743...
748
     * The optional parameter can be used to test the existence of any errors without the need of
743
     * The optional parameter can be used to test the existence of any errors without the need of
749
     * singleton instantiation
744
     * singleton instantiation
750
     * @param string|false Package name to check for errors
745
     * @param string|false Package name to check for errors
751
     * @param string Level name to check for a particular severity
746
     * @param string Level name to check for a particular severity
752
     * @return boolean
747
     * @return boolean
753
     * @static
-
 
754
     */
748
     */
755
    function staticHasErrors($package = false, $level = false)
749
    public static function staticHasErrors($package = false, $level = false)
756
    {
750
    {
757
        if ($package) {
751
        if ($package) {
758
            if (!isset($GLOBALS['_PEAR_ERRORSTACK_SINGLETON'][$package])) {
752
            if (!isset($GLOBALS['_PEAR_ERRORSTACK_SINGLETON'][$package])) {
759
                return false;
753
                return false;
760
            }
754
            }
Line 774... Line 768...
774
     * @param boolean $purge Set to purge the error stack of existing errors
768
     * @param boolean $purge Set to purge the error stack of existing errors
775
     * @param string  $level Set to a level name in order to retrieve only errors of a particular level
769
     * @param string  $level Set to a level name in order to retrieve only errors of a particular level
776
     * @param boolean $merge Set to return a flat array, not organized by package
770
     * @param boolean $merge Set to return a flat array, not organized by package
777
     * @param array   $sortfunc Function used to sort a merged array - default
771
     * @param array   $sortfunc Function used to sort a merged array - default
778
     *        sorts by time, and should be good for most cases
772
     *        sorts by time, and should be good for most cases
779
     * @static
773
     *
780
     * @return array 
774
     * @return array 
781
     */
775
     */
-
 
776
    public static function staticGetErrors(
782
    function staticGetErrors($purge = false, $level = false, $merge = false,
777
        $purge = false, $level = false, $merge = false,
783
                             $sortfunc = array('PEAR_ErrorStack', '_sortErrors'))
778
        $sortfunc = array('PEAR_ErrorStack', '_sortErrors')
784
    {
779
    ) {
785
        $ret = array();
780
        $ret = array();
786
        if (!is_callable($sortfunc)) {
781
        if (!is_callable($sortfunc)) {
787
            $sortfunc = array('PEAR_ErrorStack', '_sortErrors');
782
            $sortfunc = array('PEAR_ErrorStack', '_sortErrors');
788
        }
783
        }
789
        foreach ($GLOBALS['_PEAR_ERRORSTACK_SINGLETON'] as $package => $obj) {
784
        foreach ($GLOBALS['_PEAR_ERRORSTACK_SINGLETON'] as $package => $obj) {
Line 804... Line 799...
804
    
799
    
805
    /**
800
    /**
806
     * Error sorting function, sorts by time
801
     * Error sorting function, sorts by time
807
     * @access private
802
     * @access private
808
     */
803
     */
809
    function _sortErrors($a, $b)
804
    public static function _sortErrors($a, $b)
810
    {
805
    {
811
        if ($a['time'] == $b['time']) {
806
        if ($a['time'] == $b['time']) {
812
            return 0;
807
            return 0;
813
        }
808
        }
Line 827... Line 822...
827
     *         'function' => function name, 'class' => class name) or
822
     *         'function' => function name, 'class' => class name) or
828
     *         if this doesn't work, then false
823
     *         if this doesn't work, then false
829
     * @param unused
824
     * @param unused
830
     * @param integer backtrace frame.
825
     * @param integer backtrace frame.
831
     * @param array Results of debug_backtrace()
826
     * @param array Results of debug_backtrace()
832
     * @static
-
 
833
     */
827
     */
834
    function getFileLine($code, $params, $backtrace = null)
828
    public static function getFileLine($code, $params, $backtrace = null)
835
    {
829
    {
836
        if ($backtrace === null) {
830
        if ($backtrace === null) {
837
            return false;
831
            return false;
838
        }
832
        }
839
        $frame = 0;
833
        $frame = 0;
Line 855... Line 849...
855
            $filebacktrace = $backtrace[$frame];
849
            $filebacktrace = $backtrace[$frame];
856
            $ret = array('file' => $filebacktrace['file'],
850
            $ret = array('file' => $filebacktrace['file'],
857
                         'line' => $filebacktrace['line']);
851
                         'line' => $filebacktrace['line']);
858
            // rearrange for eval'd code or create function errors
852
            // rearrange for eval'd code or create function errors
859
            if (strpos($filebacktrace['file'], '(') && 
853
            if (strpos($filebacktrace['file'], '(') && 
860
            	  preg_match(';^(.*?)\((\d+)\) : (.*?)$;', $filebacktrace['file'],
854
            	  preg_match(';^(.*?)\((\d+)\) : (.*?)\\z;', $filebacktrace['file'],
861
                  $matches)) {
855
                  $matches)) {
862
                $ret['file'] = $matches[1];
856
                $ret['file'] = $matches[1];
863
                $ret['line'] = $matches[2] + 0;
857
                $ret['line'] = $matches[2] + 0;
864
            }
858
            }
865
            if (isset($funcbacktrace['function']) && isset($backtrace[1])) {
859
            if (isset($funcbacktrace['function']) && isset($backtrace[1])) {
Line 901... Line 895...
901
     * The returned error message will be "Error message: server error 500 -
895
     * The returned error message will be "Error message: server error 500 -
902
     * internal error"
896
     * internal error"
903
     * @param PEAR_ErrorStack
897
     * @param PEAR_ErrorStack
904
     * @param array
898
     * @param array
905
     * @param string|false Pre-generated error message template
899
     * @param string|false Pre-generated error message template
906
     * @static
900
     *
907
     * @return string
901
     * @return string
908
     */
902
     */
909
    function getErrorMessage(&$stack, $err, $template = false)
903
    public static function getErrorMessage(&$stack, $err, $template = false)
910
    {
904
    {
911
        if ($template) {
905
        if ($template) {
912
            $mainmsg = $template;
906
            $mainmsg = $template;
913
        } else {
907
        } else {
914
            $mainmsg = $stack->getErrorMessageTemplate($err['code']);
908
            $mainmsg = $stack->getErrorMessageTemplate($err['code']);