Advertisement
fruffl

XML/TEXT Exception

Nov 15th, 2011
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 16.42 KB | None | 0 0
  1. <?PHP
  2.  
  3.     CLASS ILLI_System_Exception EXTENDS EXCEPTION
  4.     {
  5.         private $CONSTRUCTOR_ARGS       = NULL;
  6.         private static $EXPORT_QUEUE        = NULL;    
  7.         private $level                      = 0;       
  8.         private static $levelCounter        = 0;       
  9.         private static $isInnerException    = FALSE;
  10.        
  11.         const           ERROR_SUCCESS                           = 0x00000000;
  12.         const           ERROR_UNKNOWN                           = 0x00000042;
  13.         protected       function getStringByCode($code){ switch($code):
  14.                         case self::ERROR_SUCCESS:               return 'The operation completed successfully.';
  15.                         case self::ERROR_UNKNOWN:               return 'Unknown Error.';
  16.                     endswitch; }
  17.        
  18.         private function getMessageByCode($code)
  19.         {
  20.             try
  21.             {
  22.                 $message = $this->getStringByCode($code);
  23.                 if(NULL === $message)
  24.                     throw new ILLI_Exception_ArgumentNull
  25.                     (ILLI_Exception_ArgumentNull::ERROR_IS_NULL);
  26.                    
  27.                 if(!is_string($message))
  28.                     throw new ILLI_Exception_DataTypeExpected
  29.                     (ILLI_Exception_DataTypeExpected::ERROR_NOT_STRING);
  30.                    
  31.                 if(empty($message))
  32.                     throw new ILLI_Exception_ArgumentExpected
  33.                     (ILLI_Exception_ArgumentExpected::ERROR_EMPTY_STRING);
  34.                    
  35.                 return $message;
  36.             }
  37.             //catch(ILLI_Exception_ArgumentNull $e){ die(var_dump($this)); }
  38.             catch(ILLI_System_Exception $e){ return self::getStringByCode(self::ERROR_UNKNOWN); }
  39.         }
  40.        
  41.         public function __construct($code = self::ERROR_UNKNOWN, $innerOrArray = NULL, $innerOrArray = NULL)
  42.         {
  43.             self::$levelCounter++;
  44.             $this->level  = self::$levelCounter;
  45.            
  46.             try
  47.             {
  48.                 try
  49.                 {
  50.                     $this->CONSTRUCTOR_ARGS = new ILLI_System_Exception_Arguments(func_get_args(), $this);
  51.                 }
  52.                 catch(ILLI_System_Exception_Arguments_Exception $e)
  53.                 {
  54.                     throw new ILLI_System_Exception_Exception_ConstructorException
  55.                     (ILLI_System_Exception_Exception_ConstructorException::ERROR_INVALID_CONSTRUCTOR_ARGUMENTS, $e);
  56.                 }
  57.             }                  
  58.             catch(ILLI_System_Exception_Exception_ConstructorException $e)
  59.             {
  60.                 throw new ILLI_System_Exception_Exception
  61.                 (ILLI_System_Exception_Exception::ERROR_INTERNAL, $e);
  62.             }
  63.            
  64.             parent::__construct($this->getMessageByCode($this->getErrorCode()), $this->getErrorCode());
  65.            
  66.         }
  67.        
  68.         public function getErrorCodeInt32()
  69.         {
  70.             return sprintf('0x%1s', str_pad(strtoupper(dechex($this->getErrorCode())), 8, 0, STR_PAD_LEFT));
  71.         }
  72.        
  73.         public function getExceptionLevel()
  74.         {
  75.             return $this->level;
  76.         }
  77.        
  78.         public function getInnerException()
  79.         {
  80.             return $this->CONSTRUCTOR_ARGS->getInnerException();
  81.         }
  82.        
  83.         public function getErrorCode()
  84.         {
  85.             return $this->CONSTRUCTOR_ARGS->getErrorCode();
  86.         }
  87.        
  88.         private function getExportQueue()
  89.         {
  90.             return ((NULL === self::$EXPORT_QUEUE)
  91.                 ? (self::$EXPORT_QUEUE = new ILLI_System_Exception_Export_Queue)
  92.                 : self::$EXPORT_QUEUE);
  93.         }
  94.        
  95.         public function getErrorMessage($exportType = ILLI_System_Exception_Export::TEXT)
  96.         {
  97.             if(NULL !== $this->getInnerException())
  98.                 $this->getInnerException()->getErrorMessage();
  99.                
  100.             $message = new ILLI_System_Exception_Export_Partial_Message;
  101.             $this->getExportQueue()->addMessage
  102.             (
  103.                 $message
  104.                 ->setExceptionClass(get_class($this))
  105.                 ->setErrorCode($this->getErrorCodeInt32())
  106.                 ->setErrorLevel($this->getExceptionLevel())
  107.                 ->setLine(str_pad($this->getLine(), 8, 0, STR_PAD_LEFT))
  108.                 ->setFile($this->getFile())
  109.                 ->setMessage($this->getParsedMessage())
  110.             );
  111.            
  112.             if(self::$isInnerException === FALSE)
  113.             {
  114.                 self::$isInnerException = TRUE;
  115.                 $this->getFormatedMethodTrace();
  116.             }
  117.                
  118.             $EXPORT = new ILLI_System_Exception_Export($this->getExportQueue(), $exportType);
  119.             return $EXPORT->getAll() ;
  120.         }
  121.        
  122.         /**
  123.          * move info-formatter to exporthandler
  124.          */
  125.         private function getParsedMessage()
  126.         {
  127.             $message = $this->getMessage();
  128.             foreach($this->CONSTRUCTOR_ARGS->getArguments() as $i => $val)
  129.             {
  130.                 if(is_scalar($val))
  131.                 {
  132.                     $_message = str_replace('{'.$i.'}', (($val === NULL) ? 'NULL' : $val), $message);
  133.                     if($_message != $message)
  134.                     {
  135.                         $message = $_message;
  136.                         continue;
  137.                     }
  138.                 }
  139.                
  140.                 ob_start();
  141.                 var_dump($val);
  142.                 $message .= "\n".'             '.str_pad('Info #'.$i.'', 16, ' ', STR_PAD_RIGHT)
  143.                     .implode("\n                             ", explode("\n", ob_get_clean()));
  144.             }
  145.            
  146.             return preg_replace('#\{([0-9a-z]+)\}\s#i', '', $message);
  147.         }
  148.        
  149.         private function getFormatedMethodTrace($index = 0)
  150.         {
  151.             $trace = $this->getTrace();
  152.             $trace = array_reverse($trace);
  153.             foreach($trace as $line)
  154.             {
  155.                 $traceline = new ILLI_System_Exception_Export_Partial_Trace();             
  156.                 $this->getExportQueue()->addTrace
  157.                 (
  158.                     $traceline
  159.                     ->setFile($line['file'])
  160.                     ->setLine($line['line'])
  161.                     ->setFunction($line['function'])
  162.                     ->setClass($line['class'])
  163.                     ->setType($line['type'])
  164.                     ->setArgs($line['args'])
  165.                 );
  166.             }
  167.            
  168.             return $this;
  169.         }
  170.     }
  171.    
  172.    
  173. <?PHP
  174.     final class ILLI_System_Exception_Arguments
  175.     {
  176.         public function getArguments()
  177.         {
  178.             return $this->__args;
  179.         }
  180.        
  181.         public function getErrorCode()
  182.         {
  183.             return $this->__errorCode;
  184.         }
  185.        
  186.         public function getInnerException()
  187.         {
  188.             return $this->__innerException;
  189.         }
  190.        
  191.         public function __construct(array $arguments = array())
  192.         {
  193.             /*
  194.             // <strict>
  195.             $args = array();
  196.             foreach($arguments as $arg)
  197.                 if(NULL !== $arg) $args[] = $arg;
  198.                
  199.             $arguments = $args;
  200.             // </strict>
  201.             */
  202.            
  203.             $c = count($arguments);
  204.            
  205.             if($c === 0)
  206.                 throw new ILLI_System_Exception_Arguments_Exception_ParameterException
  207.                     (ILLI_System_Exception_Arguments_Exception_ParameterException::ERROR_TO_FEW_CONSTRUCTOR_ARGUMENTS);
  208.            
  209.             try
  210.             {  
  211.                 $this->setErrorCode($arguments[0]);
  212.             }
  213.             catch(ILLI_System_Exception_Arguments_Exception_ErrorCodeException $e)
  214.             {
  215.                 throw new ILLI_System_Exception_Arguments_Exception_ParameterException
  216.                     (ILLI_System_Exception_Arguments_Exception_ParameterException::ERROR_INVALID_ERRORCODE, $e);
  217.             }
  218.            
  219.             // <allow all>
  220.             // throw new exception(errorcode)
  221.             // throw new exception(errorcode, messageArg1, messageArg2, ... )
  222.             // throw new exception(errorcode, innerexcdeption)
  223.             // throw new exception(errorcode, innerexcdeption, messageArg1, messageArg2, ... )
  224.             if($c === 1)
  225.                 return;
  226.            
  227.             array_shift($arguments);
  228.            
  229.             if($arguments[0] instanceOf ILLI_Exception)
  230.             {
  231.                 $this->setInnerException($arguments[0]);
  232.                
  233.                 if($c === 1)
  234.                     return;
  235.                    
  236.                 array_shift($arguments);
  237.             }
  238.            
  239.             $this->setArguments($arguments);
  240.             // </allow all>
  241.            
  242.            
  243.            
  244.             /*
  245.             // <strict>
  246.             // throw new exception(errorcode)
  247.             // throw new exception(errorcode, array(messageArg1, messageArg2, ... ))
  248.             // throw new exception(errorcode, innerexcdeption)
  249.             // throw new exception(errorcode, innerexcdeption, array(messageArg1, messageArg2, ... ))
  250.             if($c === 2)
  251.             {
  252.                 if(!($arguments[1] instanceOf SystemException) && !is_array($arguments[1]))
  253.                     throw new ILLI_System_Exception_Arguments_Constructor_Exception(ILLI_System_Exception_Arguments_Constructor_Exception::ERROR_BAD_ARGUMENT_2_2);
  254.                    
  255.                 if($arguments[1] instanceOf SystemException)
  256.                     $this->setInnerException($arguments[1]);
  257.                    
  258.                 if(is_array($arguments[1]))
  259.                     $this->setArgements($arguments[1]);
  260.             }
  261.             else
  262.             if($c === 3)
  263.             {
  264.                 if(!($arguments[1] instanceOf SystemException))
  265.                     throw new ILLI_System_Exception_Arguments_Constructor_Exception(ILLI_System_Exception_Arguments_Constructor_Exception::ERROR_BAD_ARGUMENT_2_3);
  266.                    
  267.                 if(!is_array($arguments[2]))
  268.                     throw new ILLI_System_Exception_Arguments_Constructor_Exception(ILLI_System_Exception_Arguments_Constructor_Exception::ERROR_BAD_ARGUMENT_3_3);
  269.                    
  270.                 $this->setInnerException($arguments[1]);
  271.                 $this->setArgements($arguments[2]);
  272.             }
  273.             else
  274.             if($c > 3)
  275.             {
  276.                 throw new ILLI_System_Exception_Arguments_Constructor_Exception(ILLI_System_Exception_Arguments_Constructor_Exception::ERROR_TO_MANY_CONSTRUCTOR_ARGUMENTS);
  277.             }
  278.             // </strict>
  279.             */
  280.         }
  281.        
  282.         private function setArguments($args)
  283.         {
  284.             $this->__args = $args;
  285.             return $this;
  286.         }
  287.        
  288.         private function setErrorCode($code)
  289.         {
  290.             try
  291.             {  
  292.                 if(NULL === $code)
  293.                     throw new ILLI_Exception_ArgumentNull(ILLI_Exception_ArgumentNull::ERROR_IS_NULL);
  294.                                    
  295.                 if(!is_integer($code))
  296.                     throw new ILLI_Exception_DataTypeExpected
  297.                         (ILLI_Exception_DataTypeExpected::ERROR_NOT_INTEGER);
  298.                        
  299.                 if($code === 0 || $code < 0)
  300.                     throw new ILLI_Exception_ArgumentExpected
  301.                         (ILLI_Exception_ArgumentExpected::ERROR_NOT_POSITIVE_INTEGER, $code);
  302.             }
  303.             catch(SystemException $e)
  304.             {
  305.                 throw new ILLI_System_Exception_Arguments_Exception_ErrorCodeException
  306.                     (ILLI_System_Exception_Arguments_Exception_ErrorCodeException::ERROR_IN_ERROR_CODE, $e);
  307.             }
  308.                    
  309.                    
  310.             $this->__errorCode = $code;
  311.             return $this;
  312.         }
  313.        
  314.         private function setInnerException(ILLI_Exception $innerException)
  315.         {
  316.             $this->__innerException = $innerException;
  317.             return $this;
  318.         }
  319.        
  320.         private         $__innerException           = NULL;
  321.         private         $__errorCode                = 0;
  322.         private         $__args                 = array();
  323.     }
  324.  
  325. <?PHP
  326.  
  327.     CLASS ILLI_System_Exception_Export_Queue
  328.     {
  329.         private static $MESSAGES = NULL;
  330.         private static $TRACES = NULL;
  331.        
  332.         public function __construct()
  333.         {
  334.             if(NULL === self::$MESSAGES)    self::$MESSAGES = new ILLI_System_Exception_Export_Queue_Messages;             
  335.             if(NULL === self::$TRACES)  self::$TRACES   = new ILLI_System_Exception_Export_Queue_Traces;
  336.         }
  337.        
  338.         public function addMessage(ILLI_System_Exception_Export_Partial_Message $message)
  339.         {
  340.             self::$MESSAGES->add($message);
  341.             return $this;
  342.         }
  343.        
  344.         public function getMessageStack()
  345.         {
  346.             return self::$MESSAGES;
  347.         }
  348.        
  349.         public function addTrace(ILLI_System_Exception_Export_Partial_Trace $trace)
  350.         {
  351.             self::$TRACES->add($trace);
  352.             return $this;
  353.         }
  354.        
  355.         public function getTraceStack()
  356.         {
  357.             return self::$TRACES;
  358.         }
  359.     }
  360.  
  361. <?PHP
  362.  
  363.     ABSTRACT CLASS ILLI_System_Exception_Export_Partial_Abstract
  364.     {
  365.     }
  366.  
  367. <?PHP
  368.  
  369.     CLASS ILLI_System_Exception_Export_Partial_Message EXTENDS ILLI_System_Exception_Export_Partial_Abstract
  370.     {
  371.         private $exception  = '';
  372.         private $message    = '';
  373.         private $file       = '';
  374.         private $line       = 0;
  375.         private $level      = 0;
  376.         private $error      = 0;
  377.        
  378.         public function __construct() {}
  379.        
  380.         public function setExceptionClass($string)
  381.         {
  382.             $this->exception = $string;
  383.             return $this;
  384.         }
  385.        
  386.         public function getExceptionClass()
  387.         {
  388.             return $this->exception;
  389.         }
  390.        
  391.         public function setFile($string)
  392.         {
  393.             $this->file = $string;
  394.             return $this;
  395.         }
  396.        
  397.         public function getFile()
  398.         {
  399.             return $this->file;
  400.         }
  401.        
  402.         public function setMessage($string)
  403.         {
  404.             $this->message = $string;
  405.             return $this;
  406.         }
  407.        
  408.         public function getMessage()
  409.         {
  410.             return $this->message;
  411.         }
  412.        
  413.         public function setErrorCode($int)
  414.         {
  415.             $this->error = $int;
  416.             return $this;
  417.         }
  418.        
  419.         public function getErrorCode()
  420.         {
  421.             return $this->error;
  422.         }
  423.        
  424.         public function setErrorLevel($int)
  425.         {
  426.             $this->level = $int;
  427.             return $this;
  428.         }
  429.        
  430.         public function getErrorLevel()
  431.         {
  432.             return $this->level;
  433.         }
  434.        
  435.         public function setLine($int)
  436.         {
  437.             $this->line = $int;
  438.             return $this;
  439.         }
  440.        
  441.         public function getLine()
  442.         {
  443.             return $this->line;
  444.         }
  445.     }
  446.  
  447. <?PHP
  448.  
  449.     CLASS ILLI_System_Exception_Export_Partial_Trace EXTENDS ILLI_System_Exception_Export_Partial_Abstract
  450.     {
  451.         private $class      = '';
  452.         private $type       = '';
  453.         private $function   = '';
  454.         private $args       = array();
  455.         private $file       = '';
  456.         private $line       = 0;
  457.        
  458.         public function __construct(){}
  459.        
  460.        
  461.         public function setFile($string)
  462.         {
  463.             $this->file = $string;
  464.             return $this;
  465.         }
  466.        
  467.         public function getFile()
  468.         {
  469.             return $this->file;
  470.         }
  471.        
  472.         public function setLine($int)
  473.         {
  474.             $this->line = $int;
  475.             return $this;
  476.         }
  477.        
  478.         public function getLine()
  479.         {
  480.             return $this->line;
  481.         }
  482.        
  483.         public function setFunction($string)
  484.         {
  485.             $this->function = $string;
  486.             return $this;
  487.         }
  488.        
  489.         public function getFunction()
  490.         {
  491.             return $this->function;
  492.         }
  493.        
  494.         public function setClass($string)
  495.         {
  496.             $this->class = $string;
  497.             return $this;
  498.         }
  499.        
  500.         public function getClass()
  501.         {
  502.             return $this->class;
  503.         }
  504.        
  505.         public function setType($string)
  506.         {
  507.             $this->type = $string;
  508.             return $this;
  509.         }
  510.        
  511.         public function getType()
  512.         {
  513.             return $this->type;
  514.         }
  515.        
  516.         public function setArgs(array $array)
  517.         {
  518.             $this->args = $array;
  519.             return $this;
  520.         }
  521.        
  522.         public function getArgs()
  523.         {
  524.             return $this->args;
  525.         }
  526.     }
  527.  
  528. <?PHP
  529.  
  530.     ABSTRACT CLASS ILLI_System_Exception_Export_Queue_Abstract
  531.     {
  532.     }
  533.  
  534. <?PHP
  535.  
  536.     CLASS ILLI_System_Exception_Export_Queue_Messages EXTENDS ILLI_System_Exception_Export_Queue_Abstract
  537.     {
  538.         private static $messages = array();
  539.        
  540.         public function add(ILLI_System_Exception_Export_Partial_Message $message)
  541.         {
  542.             self::$messages[] = $message;
  543.             return $this;
  544.         }
  545.        
  546.         public function get($index = NULL)
  547.         {
  548.             if(NULL === $index)
  549.                 return self::$messages;
  550.             return self::$messages[$index];
  551.         }
  552.     }
  553.  
  554. <?PHP
  555.  
  556.     CLASS ILLI_System_Exception_Export_Queue_Traces EXTENDS ILLI_System_Exception_Export_Queue_Abstract
  557.     {
  558.         private static $traces = array();
  559.        
  560.         public function add(ILLI_System_Exception_Export_Partial_Trace $trace)
  561.         {
  562.             self::$traces[] = $trace;
  563.             return $this;
  564.         }
  565.        
  566.         public function get($index = NULL)
  567.         {
  568.             if(NULL === $index)
  569.                 return self::$traces;
  570.             return self::$traces[$index];
  571.         }
  572.     }
  573.  
  574. <?PHP
  575.  
  576.     ABSTRACT CLASS ILLI_System_Exception_Export_Type_Abstract
  577.     {
  578.         private $QUEUE = NULL;
  579.         private $parsedMessages  = NULL;
  580.         private $parsedTraces    = NULL;
  581.         private $exportException = NULL;
  582.         public function __construct(ILLI_System_Exception_Export_Queue $QUEUE)
  583.         {#
  584.             $this->QUEUE = $QUEUE;
  585.             $this->parsedMessages  = $this->parseMessages($QUEUE->getMessageStack());
  586.             $this->parsedTraces    = $this->parseTraces($QUEUE->getTraceStack());
  587.             $this->exportException = $this->parseExportException($this->parsedMessages, $this->parsedTraces);
  588.         }
  589.        
  590.         protected abstract function parseMessages(ILLI_System_Exception_Export_Queue_Messages $queue);
  591.         protected abstract function parseTraces(ILLI_System_Exception_Export_Queue_Traces $queue);
  592.         protected abstract function parseExportException($parsedMessages, $parsedTraces);
  593.        
  594.        
  595.         public function getParsedMessages()
  596.         {
  597.             return $this->parsedMessages;
  598.         }
  599.        
  600.         public function getParsedTraces()
  601.         {
  602.             return $this->parsedTraces;
  603.         }
  604.        
  605.         public function export()
  606.         {
  607.             return $this->exportException;
  608.         }
  609.     }
  610.  
  611. <?PHP
  612.  
  613.     CLASS ILLI_System_Exception_Export_Type_Text EXTENDS ILLI_System_Exception_Export_Type_Abstract
  614.     {
  615.         protected function parseMessages(ILLI_System_Exception_Export_Queue_Messages $queue)
  616.         {
  617.             $messages = '';
  618.             foreach($queue->get() as $item)
  619.             {
  620.                 $message = implode("\n", array(
  621.                     $item->getMessage(),
  622.                     '        '.$item->getExceptionClass(),
  623.                     '                Thrown in       '.$item->getFile(),
  624.                     '                     Line       '.$item->getLine(),
  625.                     '                ErrorCode       '.$item->getErrorCode(),
  626.                     '                Level           '.$item->getErrorLevel(),
  627.                 ));
  628.                
  629.                 $messages .= $message."\n\n";
  630.             }
  631.            
  632.             return $messages;
  633.         }
  634.        
  635.         protected function parseTraces(ILLI_System_Exception_Export_Queue_Traces $queue)
  636.         {
  637.             $traces = '';
  638.             foreach($queue->get() as $item)
  639.             {
  640.                 $args = '';
  641.                 $parameter = 1;
  642.                 foreach($item->getArgs() as $arg)
  643.                 {
  644.                     ob_start();
  645.                     var_dump($arg);
  646.                     $arg = explode("\n", ob_get_clean());
  647.                    
  648.                     $l = 0;
  649.                     foreach($arg as $line)
  650.                     {
  651.                         if(empty($line))
  652.                             continue;
  653.                        
  654.                         $args .= ($l === 0)
  655.                             ? "\n".'                Parameter '.str_pad($parameter, 3, '0', STR_PAD_LEFT).'   '.$line
  656.                             : "\n".'                                '.$line;
  657.                        
  658.                         $l++;
  659.                     }
  660.                     //$args .= "\n";
  661.                     $parameter++;
  662.                 }
  663.                
  664.                
  665.                
  666.                 $trace = implode("\n", array(
  667.                     $item->getClass().$item->getType().$item->getFunction().'()',
  668.                     $args,
  669.                     '                File            '.$item->getFile(),
  670.                     '                Line            '.$item->getLine(),
  671.                 ));
  672.                
  673.                 $traces .= $trace."\n\n";
  674.             }
  675.            
  676.             return $traces;
  677.         }
  678.        
  679.         protected function parseExportException($parsedMessages, $parsedTraces)
  680.         {
  681.             return "\n\nERROR\n\n".$parsedTraces."\n\n\n".$parsedMessages;
  682.         }
  683.     }
  684.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement