ErrorHandler.php

Go to the documentation of this file.
00001 <?php
00002 
00031 class Robo47_ErrorHandler
00032 {
00033 
00038     protected $_oldErrorHandler = null;
00043     protected $_isErrorHandler = false;
00049     protected $_log = null;
00055     protected $_logCategory = 'errorHandler';
00061     protected $_errorPriorityMapping = array(
00062         E_ERROR => 3, // Zend_Log::ERR
00063         E_WARNING => 4, // Zend_Log::WARN
00064         E_NOTICE => 5, // Zend_Log::NOTICE
00065         E_USER_ERROR => 3, // Zend_Log::ERR
00066         E_USER_WARNING => 4, // Zend_Log::WARN
00067         E_USER_NOTICE => 5, // Zend_Log::NOTICE
00068         E_CORE_ERROR => 3, // Zend_Log::ERR
00069         E_CORE_WARNING => 4, // Zend_Log::WARN
00070         E_STRICT => 3, // Zend_Log::ERR
00071         E_RECOVERABLE_ERROR => 3, // Zend_Log::ERR
00072         'unknown' => 0, // Zend_Log::EMERG
00073 
00074     );
00075 
00080     public function __construct($log = null, $category = 'errorHandler')
00081     {
00082         $this->setLog($log);
00083         $this->setLogCategory($category);
00084     }
00085 
00092     public function setLog(Zend_Log $log = null)
00093     {
00094         $this->_log = $log;
00095         return $this;
00096     }
00097 
00103     public function getLog()
00104     {
00105         return $this->_log;
00106     }
00107 
00114     public function setLogCategory($category)
00115     {
00116         $this->_logCategory = $category;
00117         return $this;
00118     }
00119 
00125     public function getLogCategory()
00126     {
00127         return $this->_logCategory;
00128     }
00129 
00136     protected function _getErrorsPriority($error)
00137     {
00138         if (isset($this->_errorPriorityMapping[$error])) {
00139             return $this->_errorPriorityMapping[$error];
00140         } else {
00141             return $this->_errorPriorityMapping['unknown'];
00142         }
00143     }
00144 
00151     public function setErrorPriorityMapping(array $errorPriorityMapping)
00152     {
00153         $this->_errorPriorityMapping = $errorPriorityMapping;
00154         if (!isset($this->_errorPriorityMapping['unknown'])) {
00155             $this->_errorPriorityMapping['unknown'] = 0;
00156         }
00157         return $this;
00158     }
00159 
00165     public function getErrorPriorityMapping()
00166     {
00167         return $this->_errorPriorityMapping;
00168     }
00169 
00178     protected function _logError($errno, $errstr, $errfile, $errline)
00179     {
00180         if (null !== $this->getLog()) {
00181             $priority = $this->_getErrorsPriority($errno);
00182             $message = $errstr . ' in ' . $errfile . ':' . $errline;
00183             $category = array('category' => $this->getLogCategory());
00184             $this->getLog()->log($message, $priority, $category);
00185         }
00186         $displayErrors = ini_get('display_errors');
00187         ini_set('display_errors', 'Off');
00188         if (ini_get('log_errors')) {
00189             $path = ini_get('error_log');
00190             if (is_writeable(dirname($path))) {
00191                 $message = sprintf(
00192                     "PHP %s:  %s in %s on line %d",
00193                     $errno,
00194                     $errstr,
00195                     $errfile,
00196                     $errline
00197                 );
00198                 error_log($message, 0);
00199             }
00200         }
00201         ini_set('display_errors', $displayErrors);
00202     }
00203 
00209     public function registerAsErrorHandler()
00210     {
00211         $handler = array($this, 'handleError');
00212         $errorLevel = E_ALL | E_STRICT;
00213         $this->_oldErrorHandler = set_error_handler($handler, $errorLevel);
00214         $this->_isErrorHandler = true;
00215         return $this;
00216     }
00217 
00223     public function getOldErrorHandler()
00224     {
00225         return $this->_oldErrorHandler;
00226     }
00227 
00233     public function unregisterAsErrorHandler()
00234     {
00235         if ($this->_isErrorHandler) {
00236             set_error_handler($this->_oldErrorHandler);
00237             $this->_isErrorHandler = false;
00238         }
00239         return $this;
00240     }
00241 
00250     public function handleError($errno, $errstr, $errfile, $errline)
00251     {
00252         // suppress errors using @
00253         if (error_reporting() == 0) {
00254             return;
00255         }
00256         $this->_logError($errno, $errstr, $errfile, $errline);
00257         throw new Robo47_ErrorException($errstr, 0, $errno, $errfile, $errline);
00258     }
00259 }