Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?PHP
- CLASS ILLI_System_Exception EXTENDS EXCEPTION
- {
- private $CONSTRUCTOR_ARGS = NULL;
- private static $EXPORT_QUEUE = NULL;
- private $level = 0;
- private static $levelCounter = 0;
- private static $isInnerException = FALSE;
- const ERROR_SUCCESS = 0x00000000;
- const ERROR_UNKNOWN = 0x00000042;
- protected function getStringByCode($code){ switch($code):
- case self::ERROR_SUCCESS: return 'The operation completed successfully.';
- case self::ERROR_UNKNOWN: return 'Unknown Error.';
- endswitch; }
- private function getMessageByCode($code)
- {
- try
- {
- $message = $this->getStringByCode($code);
- if(NULL === $message)
- throw new ILLI_Exception_ArgumentNull
- (ILLI_Exception_ArgumentNull::ERROR_IS_NULL);
- if(!is_string($message))
- throw new ILLI_Exception_DataTypeExpected
- (ILLI_Exception_DataTypeExpected::ERROR_NOT_STRING);
- if(empty($message))
- throw new ILLI_Exception_ArgumentExpected
- (ILLI_Exception_ArgumentExpected::ERROR_EMPTY_STRING);
- return $message;
- }
- //catch(ILLI_Exception_ArgumentNull $e){ die(var_dump($this)); }
- catch(ILLI_System_Exception $e){ return self::getStringByCode(self::ERROR_UNKNOWN); }
- }
- public function __construct($code = self::ERROR_UNKNOWN, $innerOrArray = NULL, $innerOrArray = NULL)
- {
- self::$levelCounter++;
- $this->level = self::$levelCounter;
- try
- {
- try
- {
- $this->CONSTRUCTOR_ARGS = new ILLI_System_Exception_Arguments(func_get_args(), $this);
- }
- catch(ILLI_System_Exception_Arguments_Exception $e)
- {
- throw new ILLI_System_Exception_Exception_ConstructorException
- (ILLI_System_Exception_Exception_ConstructorException::ERROR_INVALID_CONSTRUCTOR_ARGUMENTS, $e);
- }
- }
- catch(ILLI_System_Exception_Exception_ConstructorException $e)
- {
- throw new ILLI_System_Exception_Exception
- (ILLI_System_Exception_Exception::ERROR_INTERNAL, $e);
- }
- parent::__construct($this->getMessageByCode($this->getErrorCode()), $this->getErrorCode());
- }
- public function getErrorCodeInt32()
- {
- return sprintf('0x%1s', str_pad(strtoupper(dechex($this->getErrorCode())), 8, 0, STR_PAD_LEFT));
- }
- public function getExceptionLevel()
- {
- return $this->level;
- }
- public function getInnerException()
- {
- return $this->CONSTRUCTOR_ARGS->getInnerException();
- }
- public function getErrorCode()
- {
- return $this->CONSTRUCTOR_ARGS->getErrorCode();
- }
- private function getExportQueue()
- {
- return ((NULL === self::$EXPORT_QUEUE)
- ? (self::$EXPORT_QUEUE = new ILLI_System_Exception_Export_Queue)
- : self::$EXPORT_QUEUE);
- }
- public function getErrorMessage($exportType = ILLI_System_Exception_Export::TEXT)
- {
- if(NULL !== $this->getInnerException())
- $this->getInnerException()->getErrorMessage();
- $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())
- );
- if(self::$isInnerException === FALSE)
- {
- self::$isInnerException = TRUE;
- $this->getFormatedMethodTrace();
- }
- $EXPORT = new ILLI_System_Exception_Export($this->getExportQueue(), $exportType);
- return $EXPORT->getAll() ;
- }
- /**
- * move info-formatter to exporthandler
- */
- private function getParsedMessage()
- {
- $message = $this->getMessage();
- foreach($this->CONSTRUCTOR_ARGS->getArguments() as $i => $val)
- {
- if(is_scalar($val))
- {
- $_message = str_replace('{'.$i.'}', (($val === NULL) ? 'NULL' : $val), $message);
- if($_message != $message)
- {
- $message = $_message;
- continue;
- }
- }
- ob_start();
- var_dump($val);
- $message .= "\n".' '.str_pad('Info #'.$i.'', 16, ' ', STR_PAD_RIGHT)
- .implode("\n ", explode("\n", ob_get_clean()));
- }
- return preg_replace('#\{([0-9a-z]+)\}\s#i', '', $message);
- }
- private function getFormatedMethodTrace($index = 0)
- {
- $trace = $this->getTrace();
- $trace = array_reverse($trace);
- foreach($trace as $line)
- {
- $traceline = new ILLI_System_Exception_Export_Partial_Trace();
- $this->getExportQueue()->addTrace
- (
- $traceline
- ->setFile($line['file'])
- ->setLine($line['line'])
- ->setFunction($line['function'])
- ->setClass($line['class'])
- ->setType($line['type'])
- ->setArgs($line['args'])
- );
- }
- return $this;
- }
- }
- <?PHP
- final class ILLI_System_Exception_Arguments
- {
- public function getArguments()
- {
- return $this->__args;
- }
- public function getErrorCode()
- {
- return $this->__errorCode;
- }
- public function getInnerException()
- {
- return $this->__innerException;
- }
- public function __construct(array $arguments = array())
- {
- /*
- // <strict>
- $args = array();
- foreach($arguments as $arg)
- if(NULL !== $arg) $args[] = $arg;
- $arguments = $args;
- // </strict>
- */
- $c = count($arguments);
- if($c === 0)
- throw new ILLI_System_Exception_Arguments_Exception_ParameterException
- (ILLI_System_Exception_Arguments_Exception_ParameterException::ERROR_TO_FEW_CONSTRUCTOR_ARGUMENTS);
- try
- {
- $this->setErrorCode($arguments[0]);
- }
- catch(ILLI_System_Exception_Arguments_Exception_ErrorCodeException $e)
- {
- throw new ILLI_System_Exception_Arguments_Exception_ParameterException
- (ILLI_System_Exception_Arguments_Exception_ParameterException::ERROR_INVALID_ERRORCODE, $e);
- }
- // <allow all>
- // throw new exception(errorcode)
- // throw new exception(errorcode, messageArg1, messageArg2, ... )
- // throw new exception(errorcode, innerexcdeption)
- // throw new exception(errorcode, innerexcdeption, messageArg1, messageArg2, ... )
- if($c === 1)
- return;
- array_shift($arguments);
- if($arguments[0] instanceOf ILLI_Exception)
- {
- $this->setInnerException($arguments[0]);
- if($c === 1)
- return;
- array_shift($arguments);
- }
- $this->setArguments($arguments);
- // </allow all>
- /*
- // <strict>
- // throw new exception(errorcode)
- // throw new exception(errorcode, array(messageArg1, messageArg2, ... ))
- // throw new exception(errorcode, innerexcdeption)
- // throw new exception(errorcode, innerexcdeption, array(messageArg1, messageArg2, ... ))
- if($c === 2)
- {
- if(!($arguments[1] instanceOf SystemException) && !is_array($arguments[1]))
- throw new ILLI_System_Exception_Arguments_Constructor_Exception(ILLI_System_Exception_Arguments_Constructor_Exception::ERROR_BAD_ARGUMENT_2_2);
- if($arguments[1] instanceOf SystemException)
- $this->setInnerException($arguments[1]);
- if(is_array($arguments[1]))
- $this->setArgements($arguments[1]);
- }
- else
- if($c === 3)
- {
- if(!($arguments[1] instanceOf SystemException))
- throw new ILLI_System_Exception_Arguments_Constructor_Exception(ILLI_System_Exception_Arguments_Constructor_Exception::ERROR_BAD_ARGUMENT_2_3);
- if(!is_array($arguments[2]))
- throw new ILLI_System_Exception_Arguments_Constructor_Exception(ILLI_System_Exception_Arguments_Constructor_Exception::ERROR_BAD_ARGUMENT_3_3);
- $this->setInnerException($arguments[1]);
- $this->setArgements($arguments[2]);
- }
- else
- if($c > 3)
- {
- throw new ILLI_System_Exception_Arguments_Constructor_Exception(ILLI_System_Exception_Arguments_Constructor_Exception::ERROR_TO_MANY_CONSTRUCTOR_ARGUMENTS);
- }
- // </strict>
- */
- }
- private function setArguments($args)
- {
- $this->__args = $args;
- return $this;
- }
- private function setErrorCode($code)
- {
- try
- {
- if(NULL === $code)
- throw new ILLI_Exception_ArgumentNull(ILLI_Exception_ArgumentNull::ERROR_IS_NULL);
- if(!is_integer($code))
- throw new ILLI_Exception_DataTypeExpected
- (ILLI_Exception_DataTypeExpected::ERROR_NOT_INTEGER);
- if($code === 0 || $code < 0)
- throw new ILLI_Exception_ArgumentExpected
- (ILLI_Exception_ArgumentExpected::ERROR_NOT_POSITIVE_INTEGER, $code);
- }
- catch(SystemException $e)
- {
- throw new ILLI_System_Exception_Arguments_Exception_ErrorCodeException
- (ILLI_System_Exception_Arguments_Exception_ErrorCodeException::ERROR_IN_ERROR_CODE, $e);
- }
- $this->__errorCode = $code;
- return $this;
- }
- private function setInnerException(ILLI_Exception $innerException)
- {
- $this->__innerException = $innerException;
- return $this;
- }
- private $__innerException = NULL;
- private $__errorCode = 0;
- private $__args = array();
- }
- <?PHP
- CLASS ILLI_System_Exception_Export_Queue
- {
- private static $MESSAGES = NULL;
- private static $TRACES = NULL;
- public function __construct()
- {
- if(NULL === self::$MESSAGES) self::$MESSAGES = new ILLI_System_Exception_Export_Queue_Messages;
- if(NULL === self::$TRACES) self::$TRACES = new ILLI_System_Exception_Export_Queue_Traces;
- }
- public function addMessage(ILLI_System_Exception_Export_Partial_Message $message)
- {
- self::$MESSAGES->add($message);
- return $this;
- }
- public function getMessageStack()
- {
- return self::$MESSAGES;
- }
- public function addTrace(ILLI_System_Exception_Export_Partial_Trace $trace)
- {
- self::$TRACES->add($trace);
- return $this;
- }
- public function getTraceStack()
- {
- return self::$TRACES;
- }
- }
- <?PHP
- ABSTRACT CLASS ILLI_System_Exception_Export_Partial_Abstract
- {
- }
- <?PHP
- CLASS ILLI_System_Exception_Export_Partial_Message EXTENDS ILLI_System_Exception_Export_Partial_Abstract
- {
- private $exception = '';
- private $message = '';
- private $file = '';
- private $line = 0;
- private $level = 0;
- private $error = 0;
- public function __construct() {}
- public function setExceptionClass($string)
- {
- $this->exception = $string;
- return $this;
- }
- public function getExceptionClass()
- {
- return $this->exception;
- }
- public function setFile($string)
- {
- $this->file = $string;
- return $this;
- }
- public function getFile()
- {
- return $this->file;
- }
- public function setMessage($string)
- {
- $this->message = $string;
- return $this;
- }
- public function getMessage()
- {
- return $this->message;
- }
- public function setErrorCode($int)
- {
- $this->error = $int;
- return $this;
- }
- public function getErrorCode()
- {
- return $this->error;
- }
- public function setErrorLevel($int)
- {
- $this->level = $int;
- return $this;
- }
- public function getErrorLevel()
- {
- return $this->level;
- }
- public function setLine($int)
- {
- $this->line = $int;
- return $this;
- }
- public function getLine()
- {
- return $this->line;
- }
- }
- <?PHP
- CLASS ILLI_System_Exception_Export_Partial_Trace EXTENDS ILLI_System_Exception_Export_Partial_Abstract
- {
- private $class = '';
- private $type = '';
- private $function = '';
- private $args = array();
- private $file = '';
- private $line = 0;
- public function __construct(){}
- public function setFile($string)
- {
- $this->file = $string;
- return $this;
- }
- public function getFile()
- {
- return $this->file;
- }
- public function setLine($int)
- {
- $this->line = $int;
- return $this;
- }
- public function getLine()
- {
- return $this->line;
- }
- public function setFunction($string)
- {
- $this->function = $string;
- return $this;
- }
- public function getFunction()
- {
- return $this->function;
- }
- public function setClass($string)
- {
- $this->class = $string;
- return $this;
- }
- public function getClass()
- {
- return $this->class;
- }
- public function setType($string)
- {
- $this->type = $string;
- return $this;
- }
- public function getType()
- {
- return $this->type;
- }
- public function setArgs(array $array)
- {
- $this->args = $array;
- return $this;
- }
- public function getArgs()
- {
- return $this->args;
- }
- }
- <?PHP
- ABSTRACT CLASS ILLI_System_Exception_Export_Queue_Abstract
- {
- }
- <?PHP
- CLASS ILLI_System_Exception_Export_Queue_Messages EXTENDS ILLI_System_Exception_Export_Queue_Abstract
- {
- private static $messages = array();
- public function add(ILLI_System_Exception_Export_Partial_Message $message)
- {
- self::$messages[] = $message;
- return $this;
- }
- public function get($index = NULL)
- {
- if(NULL === $index)
- return self::$messages;
- return self::$messages[$index];
- }
- }
- <?PHP
- CLASS ILLI_System_Exception_Export_Queue_Traces EXTENDS ILLI_System_Exception_Export_Queue_Abstract
- {
- private static $traces = array();
- public function add(ILLI_System_Exception_Export_Partial_Trace $trace)
- {
- self::$traces[] = $trace;
- return $this;
- }
- public function get($index = NULL)
- {
- if(NULL === $index)
- return self::$traces;
- return self::$traces[$index];
- }
- }
- <?PHP
- ABSTRACT CLASS ILLI_System_Exception_Export_Type_Abstract
- {
- private $QUEUE = NULL;
- private $parsedMessages = NULL;
- private $parsedTraces = NULL;
- private $exportException = NULL;
- public function __construct(ILLI_System_Exception_Export_Queue $QUEUE)
- {#
- $this->QUEUE = $QUEUE;
- $this->parsedMessages = $this->parseMessages($QUEUE->getMessageStack());
- $this->parsedTraces = $this->parseTraces($QUEUE->getTraceStack());
- $this->exportException = $this->parseExportException($this->parsedMessages, $this->parsedTraces);
- }
- protected abstract function parseMessages(ILLI_System_Exception_Export_Queue_Messages $queue);
- protected abstract function parseTraces(ILLI_System_Exception_Export_Queue_Traces $queue);
- protected abstract function parseExportException($parsedMessages, $parsedTraces);
- public function getParsedMessages()
- {
- return $this->parsedMessages;
- }
- public function getParsedTraces()
- {
- return $this->parsedTraces;
- }
- public function export()
- {
- return $this->exportException;
- }
- }
- <?PHP
- CLASS ILLI_System_Exception_Export_Type_Text EXTENDS ILLI_System_Exception_Export_Type_Abstract
- {
- protected function parseMessages(ILLI_System_Exception_Export_Queue_Messages $queue)
- {
- $messages = '';
- foreach($queue->get() as $item)
- {
- $message = implode("\n", array(
- $item->getMessage(),
- ' '.$item->getExceptionClass(),
- ' Thrown in '.$item->getFile(),
- ' Line '.$item->getLine(),
- ' ErrorCode '.$item->getErrorCode(),
- ' Level '.$item->getErrorLevel(),
- ));
- $messages .= $message."\n\n";
- }
- return $messages;
- }
- protected function parseTraces(ILLI_System_Exception_Export_Queue_Traces $queue)
- {
- $traces = '';
- foreach($queue->get() as $item)
- {
- $args = '';
- $parameter = 1;
- foreach($item->getArgs() as $arg)
- {
- ob_start();
- var_dump($arg);
- $arg = explode("\n", ob_get_clean());
- $l = 0;
- foreach($arg as $line)
- {
- if(empty($line))
- continue;
- $args .= ($l === 0)
- ? "\n".' Parameter '.str_pad($parameter, 3, '0', STR_PAD_LEFT).' '.$line
- : "\n".' '.$line;
- $l++;
- }
- //$args .= "\n";
- $parameter++;
- }
- $trace = implode("\n", array(
- $item->getClass().$item->getType().$item->getFunction().'()',
- $args,
- ' File '.$item->getFile(),
- ' Line '.$item->getLine(),
- ));
- $traces .= $trace."\n\n";
- }
- return $traces;
- }
- protected function parseExportException($parsedMessages, $parsedTraces)
- {
- return "\n\nERROR\n\n".$parsedTraces."\n\n\n".$parsedMessages;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement