Advertisement
fruffl

5.3.8 Exception

Feb 29th, 2012
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.14 KB | None | 0 0
  1. <?PHP
  2.     /**
  3.      * ILLI
  4.      *
  5.      * @category   ILLI_Exception
  6.      * @package    ILLI
  7.      * @subpackage Exception
  8.      * @link       http://illi.be
  9.      * @license    http://l.illi.be
  10.      * @copyright  ILLI Conference
  11.      */
  12.     NAMESPACE ILLI\Exception;
  13.     USE ILLI\System as System;
  14.  
  15.     /**
  16.      * ILLI Exception Base
  17.      *
  18.      * @category   ILLI_Exception
  19.      * @package    ILLI
  20.      * @subpackage Exception
  21.      * @namespace  ILLI\Exception
  22.      * @link       http://illi.be
  23.      * @license    http://l.illi.be
  24.      * @copyright  ILLI Conference
  25.      * @since      2.0.0-1
  26.      * @version    2.0.0-1
  27.      * @abstract
  28.      */
  29.     ABSTRACT CLASS Base extends \Exception
  30.     {
  31.         private $__INFO = NULL;
  32.         private static $__levelCounter = 0;
  33.         private static $__logLevels = array();     
  34.         private static $__LOGGER = NULL;
  35.        
  36.         private $__level = 0;
  37.         private $__ARGUMENTS = NULL;
  38.         private $__COLLECTOR = NULL;
  39.         private $__EXPORT = NULL;
  40.        
  41.         /** <refact> */
  42.             public static function hasCatched($exceptionClass)
  43.             {
  44.                 if(NULL === $exceptionClass)
  45.                     throw new System\ArgumentNullException;
  46.                    
  47.                 if(!is_string($exceptionClass))
  48.                     throw new System\ArgumentException(System\E::ARGUMENT_EXPECTED_STRING);
  49.                    
  50.                 if(empty($exceptionClass))
  51.                     throw new System\ArgumentException(System\E::ARGUMENT_EXPECTED_STRING_EMPTY);
  52.    
  53.                 $e = strtolower($exceptionClass);
  54.                 return array_key_exists($e, self::$__logLevels);
  55.             }
  56.            
  57.             public static function getCatchedLevel($exceptionClass)
  58.             {
  59.                 if(FALSE === self::hasCatched($exceptionClass))
  60.                     return array();
  61.                    
  62.                 $e = strtolower($exceptionClass);
  63.                    
  64.                 return self::$__logLevels[$e];
  65.             }
  66.            
  67.             public static function getCatched()
  68.             {
  69.                 return self::$__logLevels;
  70.             }
  71.            
  72.             private function registerException()
  73.             {
  74.                 self::$__levelCounter++;
  75.                 $this->__level = self::$__levelCounter;
  76.                
  77.                 $Class = get_class($this);
  78.                 $class = strtolower($Class);
  79.                    
  80.                 if(FALSE === self::hasCatched($class))
  81.                     self::$__logLevels[$class] = array();
  82.                
  83.                 self::$__logLevels[$class][] = $this->__level;
  84.             }
  85.            
  86.             public static function getCatchedExceptions()
  87.             {
  88.                 return array_keys(self::$__logLevels);
  89.             }
  90.         /** </refact> */
  91.        
  92.         public static function getLog()
  93.         {
  94.             return self::$__LOGGER->getFilteredExceptions();
  95.         }
  96.        
  97.         public function __construct($code = self::ERROR_UNKNOWN, $innerOrOptions = NULL, $options = NULL)
  98.         {
  99.             try
  100.             {
  101.                 $this->__ARGUMENTS = new Arguments(func_get_args());
  102.             }
  103.             catch(ArgumentsException $e1)
  104.             {
  105.                 /** <refact> */
  106.                 throw new BaseException
  107.                     (System\Exception_Exception_ConstructorException::ERROR_INVALID_CONSTRUCTOR_ARGUMENTS, $e1);
  108.                 /** </refact> */
  109.             }
  110.            
  111.             $this->__EXPORT = new Export($this->getCollector());
  112.            
  113.             self::$__LOGGER = Logger::getThread()
  114.                 ->addFilter(__NAMESPACE__.'\LoggerFilterRuntime')
  115.                 ->registerException($this);
  116.                
  117.             $this->registerException();
  118.            
  119.             parent::__construct
  120.             (
  121.                 $this->getParsedMessage(),
  122.                 $this->getErrorCode(),
  123.                 $this->__ARGUMENTS->getPreviousException()
  124.             );
  125.         }
  126.        
  127.         public function getErrorCode()
  128.         {
  129.             return $this->__ARGUMENTS->getErrorCode();
  130.         }
  131.        
  132.         public function getErrorCodeInt32()
  133.         {
  134.             return sprintf('0x%1s', str_pad(strtoupper(dechex($this->getErrorCode())), 8, 0, STR_PAD_LEFT));
  135.         }
  136.        
  137.         public function getExceptionLevel()
  138.         {
  139.             return $this->__level;
  140.         }
  141.        
  142.         public function export()
  143.         {
  144.             if($this->getPrevious() instanceOf Base)
  145.                 $this->getPrevious()->export();
  146.            
  147.             $this->getCollector()->addMessage($message = new CollectorMessagesComponent);
  148.            
  149.             $message
  150.             ->setExceptionClass(get_class($this))
  151.             ->setErrorCode($this->getErrorCodeInt32())
  152.             ->setErrorLevel($this->getExceptionLevel())
  153.             ->setLine(str_pad($this->getLine(), 8, 0, STR_PAD_LEFT))
  154.             ->setFile($this->getFile())
  155.             ->setMessage($this->getMessage())
  156.             ->setReferences($this->getParsedReferences());
  157.            
  158.             if(NULL !== $this->getPrevious())
  159.                 return $this->__EXPORT;
  160.            
  161.             foreach($this->getTrace() as $line)
  162.             {
  163.                 $this->getCollector()->addTrace($trace = new CollectorTracesComponent);
  164.                
  165.                 $trace
  166.                 ->setFile(isset($line['file']) ? $line['file'] : 'internal')
  167.                 ->setLine(isset($line['line']) ? $line['line'] : 'NaN')
  168.                 ->setClass(isset($line['class']) ? $line['class'] : '')
  169.                 ->setType(isset($line['type']) ? $line['type'] : '')
  170.                 ->setArgs(isset($line['args']) ? $line['args'] : 'None')
  171.                 ->setFunction(isset($line['function']) ? $line['function'] : 'Unknown');
  172.             }
  173.            
  174.             return $this->__EXPORT;
  175.         }
  176.        
  177.         private function cleanUp($string)
  178.         {
  179.             return str_replace
  180.             (
  181.                 array
  182.                 (
  183.                     realpath(dirname($_SERVER['SCRIPT_FILENAME'])),
  184.                     realpath($_SERVER['DOCUMENT_ROOT']),
  185.                     dirname($_SERVER['SCRIPT_FILENAME']),
  186.                     $_SERVER['DOCUMENT_ROOT'],
  187.                     $_SERVER['PHP_DOCUMENT_ROOT'],
  188.                     ILLI::getInstallDir(),
  189.                 ),
  190.                 '',
  191.                 $string
  192.             );
  193.         }
  194.        
  195.         /** <refact> */
  196.         protected function getStringByCode($code)
  197.         {
  198.             switch($code):
  199.                 //case System\E::SUCCESS:       return 'The operation completed successfully.';
  200.                 //case System\E::ERROR_UNKNOWN: return 'Unknown Error.';
  201.                 //case System\E::INTERNAL_ERROR:    return 'Internal Error.';
  202.             endswitch;
  203.         }      
  204.         /** </refact> */
  205.        
  206.         private function getCollector()
  207.         {
  208.             return ((NULL === $this->__COLLECTOR)
  209.                 ? ($this->__COLLECTOR = new Collector)
  210.                 : $this->__COLLECTOR);
  211.         }
  212.        
  213.         /** <refact> */
  214.         private function getMessageByCode($code)
  215.         {
  216.             try
  217.             {
  218.                 $message = $this->getStringByCode($code);
  219.                
  220.                 try
  221.                 {
  222.                     if(NULL === $message)
  223.                         throw new System\ArgumentNullException;
  224.                        
  225.                     if(!is_string($message))
  226.                         throw new System\ArgumentException
  227.                             (System\E::ARGUMENT_EXPECTED_STRING);
  228.                        
  229.                     if(empty($message))
  230.                         throw new System\ArgumentException
  231.                             (System\E::ARGUMENT_EXPECTED_STRING_EMPTY);
  232.                        
  233.                 }
  234.                 catch(ILLI_Exception_Argument $e)
  235.                 {
  236.                     throw new ILLI_Exception_ArgumentOutOfRange;
  237.                 }
  238.                
  239.                 return $message;
  240.                
  241.             }
  242.             catch(ILLI_Exception_ArgumentOutOfRange $e)
  243.             {
  244.                 return self::getStringByCode(System\E::ERROR_UNKNOWN);
  245.             }
  246.             catch(ILLI_Exception $e)
  247.             {
  248.                 throw new System\Exception_Exception
  249.                     (System\E::INTERNAL_ERROR);
  250.             }
  251.         }
  252.         /** </refact> */
  253.        
  254.         private function getParsedMessage()
  255.         {
  256.             $message = $this->getMessageByCode($this->getErrorCode());
  257.            
  258.             foreach($this->__ARGUMENTS->getArguments() as $i => $val)
  259.             {
  260.                 if(!is_scalar($val))
  261.                     continue;
  262.                
  263.                 $_message = str_replace('{'.$i.'}', (($val === NULL) ? 'NULL' : $val), $message);
  264.                 if($_message != $message)
  265.                 {
  266.                     $message = $_message;
  267.                     continue;
  268.                 }
  269.                
  270.             }
  271.            
  272.             $message = preg_replace('#\{([0-9a-z]+)\}\s#i', '', $message);
  273.             return $message;
  274.         }
  275.        
  276.         private function getParsedReferences()
  277.         {
  278.             $messageinfo = array();
  279.             foreach($this->__ARGUMENTS->getArguments() as $i => $val)
  280.             {
  281.                 ob_start();
  282.                 var_dump($val);
  283.                 $messageinfo[$i] = ob_get_clean();
  284.             }
  285.            
  286.             return $messageinfo;
  287.         }
  288.        
  289.         public function __toString()
  290.         {
  291.             return $this->export()->asText();
  292.         }
  293.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement