Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?PHP
- /**
- * ILLI
- *
- * @category ILLI
- * @package ILLI
- * @subpackage System
- * @link http://illi.be
- * @license http://l.illi.be
- * @copyright 2007-2012, ILLI Conference
- */
- /**
- * ILLI System Exception
- *
- * This exception-handler provides inner- and outer-exception-handling
- * and several output-formats.
- * @todo notify ILLI when we have thrown an exception
- * @todo multi-level mapper
- *
- * @category ILLI
- * @package ILLI
- * @subpackage System
- * @link http://illi.be
- * @license http://l.illi.be
- * @copyright 2007-2012, ILLI Conference
- */
- ABSTRACT CLASS ILLI_System_Exception EXTENDS EXCEPTION IMPLEMENTS ILLI_System_E
- {
- /**
- * static exception counter
- *
- * @var int $__levelCounter
- * @private
- * @static
- */
- private static $__levelCounter = 0;
- /**
- * stores the relation between instance and {@link $__levelCounter}
- *
- * @var array $__logLevels
- * @private
- * @static
- */
- private static $__logLevels = array();
- /**
- * logger map
- *
- * @var ILLI_System_Exception_Logger $System_Exception_Logger
- * @private
- * @static
- */
- private static $System_Exception_Logger = NULL;
- /**
- * constructor argument handler
- *
- * @var ILLI_System_Exception_Arguments $System_ExceptionArguments
- * @private
- */
- private $System_ExceptionArguments = NULL;
- /**
- * exception output handler
- *
- * @var ILLI_System_Exception_Export_Queue $System_Exception_Export_Queue
- * @private
- */
- private $System_Exception_Export_Queue = NULL;
- /**
- * exception number
- *
- * @var int $__level
- * @private
- */
- private $__level = 0;
- /**
- * look-up for thrown inner-exception
- *
- * simple query
- *
- * ...catch(myException $e)
- * {
- * var_dump($e->hasCatched('anotherException'));
- * }
- *
- * @param string $exceptionClass
- * @return bool
- * @uses ILLI_Exception_ArgumentNull
- * @uses ILLI_Exception_ArgumentExpected
- * @uses ILLI_System_E::ARGUMENT_EXPECTED_STRING
- * @uses ILLI_System_E::ARGUMENT_EXPECTED_STRING_EMPTY
- * @uses ILLI_System_Exception::$__logLevels
- * @throws {@link ILLI_Exception_ArgumentNull}
- * @throws {@link ILLI_Exception_ArgumentExpected}
- */
- public static function hasCatched($exceptionClass)
- {
- if(NULL === $exceptionClass)
- throw new ILLI_Exception_ArgumentNull;
- if(!is_string($exceptionClass))
- throw new ILLI_Exception_ArgumentExpected
- (ILLI_System_E::ARGUMENT_EXPECTED_STRING);
- if(empty($exceptionClass))
- throw new ILLI_Exception_ArgumentExpected
- (ILLI_System_E::ARGUMENT_EXPECTED_STRING_EMPTY);
- $e = strtolower($exceptionClass);
- return array_key_exists($e, self::$__logLevels);
- }
- /**
- * get levels of inner exception as array
- *
- * @param string $exceptionClass
- * @return array integer-values
- * @uses ILLI_System_Exception::hasCatched()
- * @uses ILLI_System_Exception::$__logLevels
- */
- public static function getCatchedLevel($exceptionClass)
- {
- if(FALSE === self::hasCatched($exceptionClass))
- return array();
- $e = strtolower($exceptionClass);
- return self::$__logLevels[$e];
- }
- /**
- * get all catched exceptions including levels
- *
- * @param string $exceptionClass
- * @return array {@link $__logLevels}
- * @uses ILLI_System_Exception::$__logLevels
- */
- public static function getCatched()
- {
- return self::$__logLevels;
- }
- /**
- * get all logger-maps
- *
- * @return array
- * @uses ILLI_System_Exception::$System_Exception_Logger
- * @uses ILLI_System_Exception_Logger
- * @uses ILLI_System_Exception_Logger::getInternal()
- * @uses ILLI_System_Exception_Logger::getSystem()
- * @uses ILLI_System_Exception_Logger::getAll()
- */
- public static function getLog()
- {
- return array
- (
- 'internal' => self::$System_Exception_Logger->getInternal(),
- 'system' => self::$System_Exception_Logger->getSystem(),
- 'all' => self::$System_Exception_Logger->getAll()
- );
- }
- /**
- * get class-names of all catched exceptions
- *
- * @return array
- * @uses ILLI_System_Exception::$__logLevels
- */
- public static function getCatchedExceptions()
- {
- return array_keys(self::$__logLevels);
- }
- /**
- * constructor
- *
- * @param int32 $code valid error-code
- * @param ILLI_System_Exception|mixed $innerOrArray previous exception or argument(s)
- * @uses ILLI_System_Exception::registerException()
- * @uses ILLI_System_Exception::getMessageByCode()
- * @uses ILLI_System_Exception::getErrorCode()
- * @uses ILLI_System_Exception::$System_ExceptionArguments
- * @uses ILLI_System_Exception_Arguments
- * @uses ILLI_System_Exception_Arguments::getPreviousException()
- * @uses ILLI_System_Exception_Arguments_Exception
- * @uses ILLI_System_Exception_Exception_ConstructorException
- * @uses ILLI_System_Exception_Exception_ConstructorException::ERROR_INVALID_CONSTRUCTOR_ARGUMENTS
- * @uses ILLI_System_Exception_Exception
- * @uses ILLI_System_E::INTERNAL_ERROR
- * @uses ILLI_System_E::ERROR_UNKNOWN
- */
- public function __construct($code = self::ERROR_UNKNOWN, $innerOrArray = NULL, $innerOrArray = NULL)
- {
- try
- {
- try
- {
- $this->System_ExceptionArguments =
- new ILLI_System_Exception_Arguments(func_get_args());
- }
- catch(ILLI_System_Exception_Arguments_Exception $e1)
- {
- throw new ILLI_System_Exception_Exception_ConstructorException
- (ILLI_System_Exception_Exception_ConstructorException::ERROR_INVALID_CONSTRUCTOR_ARGUMENTS, $e1);
- }
- $this->registerException();
- parent::__construct
- (
- $this->getMessageByCode($this->getErrorCode()),
- $this->getErrorCode(),
- $this->System_ExceptionArguments->getPreviousException()
- );
- }
- catch(ILLI_System_Exception_Exception_ConstructorException $e2)
- {
- throw new ILLI_System_Exception_Exception
- (ILLI_System_E::INTERNAL_ERROR, $e2);
- }
- }
- /**
- * get error-code
- *
- * @return int
- * @uses ILLI_System_Exception::$System_ExceptionArguments
- * @uses ILLI_System_Exception_Arguments
- * @uses ILLI_System_Exception_Arguments::getErrorCode()
- */
- public function getErrorCode()
- {
- return $this->System_ExceptionArguments->getErrorCode();
- }
- /**
- * get error-code as 0x-string-format
- *
- * @return string
- * @uses ILLI_System_Exception::getErrorCode()
- */
- public function getErrorCodeInt32()
- {
- return sprintf('0x%1s', str_pad(strtoupper(dechex($this->getErrorCode())), 8, 0, STR_PAD_LEFT));
- }
- /**
- * get current level
- *
- * @return int
- * @uses ILLI_System_Exception::$__level
- */
- public function getExceptionLevel()
- {
- return $this->__level;
- }
- /**
- * export exception
- *
- * @param string $exportType
- * @return mixed
- * @uses ILLI_System_Exception::getTrace()
- * @uses ILLI_System_Exception::export()
- * @uses ILLI_System_Exception::getExportQueue()
- * @uses ILLI_System_Exception::getPrevious()
- * @uses ILLI_System_Exception_Export
- * @uses ILLI_System_Exception_Export::TEXT
- * @uses ILLI_System_Exception_Export::getMessage()
- * @uses ILLI_System_Exception_Export_Queue
- * @uses ILLI_System_Exception_Export_Queue::addMessage()
- * @uses ILLI_System_Exception_Export_Queue::addTrace()
- * @uses ILLI_System_Exception_Export_Partial_Message
- * @uses ILLI_System_Exception_Export_Partial_Message::setExceptionClass()
- * @uses ILLI_System_Exception_Export_Partial_Message::setErrorCode()
- * @uses ILLI_System_Exception_Export_Partial_Message::setErrorLevel()
- * @uses ILLI_System_Exception_Export_Partial_Message::setLine()
- * @uses ILLI_System_Exception_Export_Partial_Message::setFile()
- * @uses ILLI_System_Exception_Export_Partial_Message::setMessage()
- * @uses ILLI_System_Exception_Export_Partial_Message::setReferences()
- * @uses ILLI_System_Exception_Export_Partial_Trace
- * @uses ILLI_System_Exception_Export_Partial_Trace::setFile()
- * @uses ILLI_System_Exception_Export_Partial_Trace::setLine()
- * @uses ILLI_System_Exception_Export_Partial_Trace::setFunction()
- * @uses ILLI_System_Exception_Export_Partial_Trace::setClass()
- * @uses ILLI_System_Exception_Export_Partial_Trace::setType()
- * @uses ILLI_System_Exception_Export_Partial_Trace::setArgs()
- */
- public function export($exportType = ILLI_System_Exception_Export::TEXT)
- {
- $message = new ILLI_System_Exception_Export_Partial_Message;
- $this->getExportQueue()->addMessage
- (
- $message
- ->setExceptionClass
- (
- get_class($this)
- )
- ->setErrorCode
- (
- $this->getErrorCodeInt32()
- )
- ->setErrorLevel
- (
- $this->getExceptionLevel()
- )
- ->setLine
- (
- str_pad($this->getLine(), 8, 0, STR_PAD_LEFT)
- )
- ->setFile
- (
- $this->getFile()
- )
- ->setMessage
- (
- $this->getParsedMessage()
- )
- ->setReferences
- (
- $this->getParsedReferences()
- )
- );
- if($this->getPrevious() instanceOf ILLI_System_Exception)
- $this->getPrevious()->export();
- if(NULL === $this->getPrevious())
- {
- $trace = $this->getTrace();
- $trace = array_reverse($trace);
- foreach($trace as $line)
- {
- $traceline = new ILLI_System_Exception_Export_Partial_Trace;
- $this->getExportQueue()->addTrace
- (
- $traceline
- ->setFile
- (
- isset($line['file'])
- ? $line['file']
- : 'internal'
- )
- ->setLine
- (
- isset($line['line'])
- ? $line['line']
- : 'NaN'
- )
- ->setFunction
- (
- isset($line['function'])
- ? $line['function']
- : 'Unknown'
- )
- ->setClass
- (
- isset($line['class'])
- ? $line['class']
- : ''
- )
- ->setType
- (
- isset($line['type'])
- ? $line['type']
- : ''
- )
- ->setArgs
- (
- isset($line['args'])
- ? $line['args']
- : 'None'
- )
- );
- }
- }
- $EXPORT = new ILLI_System_Exception_Export
- (
- $this->getExportQueue(),
- $exportType
- );
- if(!is_string($e = $EXPORT->getMessage()))
- return $e;
- return $e;
- }
- /**
- * remove sensitive parts from exception-message
- *
- * @return string
- * @uses ILLI
- * @uses ILLI::getInstallDir()
- */
- private function cleanUp($string)
- {
- return str_replace
- (
- array
- (
- realpath(dirname($_SERVER['SCRIPT_FILENAME'])),
- realpath($_SERVER['DOCUMENT_ROOT']),
- dirname($_SERVER['SCRIPT_FILENAME']),
- $_SERVER['DOCUMENT_ROOT'],
- $_SERVER['PHP_DOCUMENT_ROOT'],
- ILLI::getInstallDir(),
- ),
- '',
- $string
- );
- }
- /**
- * register exception as thrown instance
- *
- * @void
- * @uses ILLI_System_Exception::$__levelCounter
- * @uses ILLI_System_Exception::$__level
- * @uses ILLI_System_Exception::$__logLevels
- * @uses ILLI_System_Exception::$System_Exception_Logger
- * @uses ILLI_System_Exception_Logger
- * @uses ILLI_System_Exception_Logger::getInstance()
- * @uses ILLI_System_Exception_Logger::registerException()
- */
- private function registerException()
- {
- self::$__levelCounter++;
- $this->__level = self::$__levelCounter;
- $Class = get_class($this);
- $class = strtolower($Class);
- if(FALSE === self::hasCatched($class))
- self::$__logLevels[$class] = array();
- self::$__logLevels[$class][] = $this->__level;
- if(NULL === self::$System_Exception_Logger)
- self::$System_Exception_Logger = ILLI_System_Exception_Logger::getInstance();
- self::$System_Exception_Logger->registerException($this);
- }
- /**
- * convert error-code to message
- *
- * @param int32 $code
- * @return string
- * @uses ILLI_System_E::SUCCESS
- * @uses ILLI_System_E::ERROR_UNKNOWN
- * @uses ILLI_System_E::INTERNAL_ERROR
- */
- protected function getStringByCode($code)
- {
- switch($code):
- case ILLI_System_E::SUCCESS: return 'The operation completed successfully.';
- case ILLI_System_E::ERROR_UNKNOWN: return 'Unknown Error.';
- case ILLI_System_E::INTERNAL_ERROR: return 'Internal Error.';
- endswitch;
- }
- /**
- * convert error-code to message
- *
- * @return ILLI_System_Exception_Export_Queue
- * @uses ILLI_System_Exception::$System_Exception_Export_Queue
- * @uses ILLI_System_Exception_Export_Queue
- */
- private function getExportQueue()
- {
- return ((NULL === $this->System_Exception_Export_Queue)
- ? ($this->System_Exception_Export_Queue = new ILLI_System_Exception_Export_Queue)
- : $this->System_Exception_Export_Queue);
- }
- /**
- * convert error-code to message
- *
- * @param int32 $code
- * @return string
- * @uses ILLI_System_Exception::getStringByCode()
- * @uses ILLI_System_E::ARGUMENT_EXPECTED_STRING
- * @uses ILLI_System_E::ARGUMENT_EXPECTED_STRING_EMPTY
- * @uses ILLI_System_E::ERROR_UNKNOWN
- * @uses ILLI_System_E::INTERNAL_ERROR
- * @uses ILLI_Exception_Argument
- * @uses ILLI_Exception_ArgumentNull
- * @uses ILLI_Exception_ArgumentExpected
- * @uses ILLI_Exception_ArgumentOutOfRange
- * @uses ILLI_Exception
- * @uses ILLI_System_Exception_Exception
- * @throws {@link ILLI_Exception_ArgumentNull}
- * @throws {@link ILLI_Exception_ArgumentExpected}
- * @throws {@link ILLI_Exception_ArgumentOutOfRange}
- * @throws {@link ILLI_System_Exception_Exception}
- */
- private function getMessageByCode($code)
- {
- try
- {
- $message = $this->getStringByCode($code);
- try
- {
- if(NULL === $message)
- throw new ILLI_Exception_ArgumentNull;
- if(!is_string($message))
- throw new ILLI_Exception_ArgumentExpected
- (ILLI_System_E::ARGUMENT_EXPECTED_STRING);
- if(empty($message))
- throw new ILLI_Exception_ArgumentExpected
- (ILLI_System_E::ARGUMENT_EXPECTED_STRING_EMPTY);
- }
- catch(ILLI_Exception_Argument $e)
- {
- throw new ILLI_Exception_ArgumentOutOfRange;
- }
- return $message;
- }
- catch(ILLI_Exception_ArgumentOutOfRange $e)
- {
- return self::getStringByCode(ILLI_System_E::ERROR_UNKNOWN);
- }
- catch(ILLI_Exception $e)
- {
- throw new ILLI_System_Exception_Exception
- (ILLI_System_E::INTERNAL_ERROR);
- }
- }
- /**
- * create exception-message
- *
- * @return string
- * @uses ILLI_System_Exception::$System_ExceptionArguments
- * @uses ILLI_System_Exception::getMessage()
- * @uses ILLI_System_Exception_Arguments::getArguments()
- */
- private function getParsedMessage()
- {
- $message = $this->getMessage();
- foreach($this->System_ExceptionArguments->getArguments() as $i => $val)
- {
- if(!is_scalar($val))
- continue;
- $_message = str_replace('{'.$i.'}', (($val === NULL) ? 'NULL' : $val), $message);
- if($_message != $message)
- {
- $message = $_message;
- continue;
- }
- }
- return preg_replace('#\{([0-9a-z]+)\}\s#i', '', $message);
- }
- /**
- * create message reference
- *
- * @return array
- * @uses ILLI_System_Exception::$System_ExceptionArguments
- * @uses ILLI_System_Exception_Arguments::getArguments()
- */
- private function getParsedReferences()
- {
- $messageinfo = array();
- foreach($this->System_ExceptionArguments->getArguments() as $i => $val)
- {
- ob_start();
- var_dump($val);
- $messageinfo[$i] = ob_get_clean();
- }
- return $messageinfo;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement