Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?PHP
- /**
- * ILLI
- *
- * @category ILLI_Exception
- * @package ILLI
- * @subpackage Exception
- * @link http://illi.be
- * @license http://l.illi.be
- * @copyright ILLI Conference
- */
- NAMESPACE ILLI\Exception;
- USE ILLI\System as System;
- /**
- * ILLI Exception Base
- *
- * @category ILLI_Exception
- * @package ILLI
- * @subpackage Exception
- * @namespace ILLI\Exception
- * @link http://illi.be
- * @license http://l.illi.be
- * @copyright ILLI Conference
- * @since 2.0.0-1
- * @version 2.0.0-1
- * @abstract
- */
- ABSTRACT CLASS Base extends \Exception
- {
- private $__INFO = NULL;
- private static $__levelCounter = 0;
- private static $__logLevels = array();
- private static $__LOGGER = NULL;
- private $__level = 0;
- private $__ARGUMENTS = NULL;
- private $__COLLECTOR = NULL;
- private $__EXPORT = NULL;
- /** <refact> */
- public static function hasCatched($exceptionClass)
- {
- if(NULL === $exceptionClass)
- throw new System\ArgumentNullException;
- if(!is_string($exceptionClass))
- throw new System\ArgumentException(System\E::ARGUMENT_EXPECTED_STRING);
- if(empty($exceptionClass))
- throw new System\ArgumentException(System\E::ARGUMENT_EXPECTED_STRING_EMPTY);
- $e = strtolower($exceptionClass);
- return array_key_exists($e, self::$__logLevels);
- }
- public static function getCatchedLevel($exceptionClass)
- {
- if(FALSE === self::hasCatched($exceptionClass))
- return array();
- $e = strtolower($exceptionClass);
- return self::$__logLevels[$e];
- }
- public static function getCatched()
- {
- return self::$__logLevels;
- }
- 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;
- }
- public static function getCatchedExceptions()
- {
- return array_keys(self::$__logLevels);
- }
- /** </refact> */
- public static function getLog()
- {
- return self::$__LOGGER->getFilteredExceptions();
- }
- public function __construct($code = self::ERROR_UNKNOWN, $innerOrOptions = NULL, $options = NULL)
- {
- try
- {
- $this->__ARGUMENTS = new Arguments(func_get_args());
- }
- catch(ArgumentsException $e1)
- {
- /** <refact> */
- throw new BaseException
- (System\Exception_Exception_ConstructorException::ERROR_INVALID_CONSTRUCTOR_ARGUMENTS, $e1);
- /** </refact> */
- }
- $this->__EXPORT = new Export($this->getCollector());
- self::$__LOGGER = Logger::getThread()
- ->addFilter(__NAMESPACE__.'\LoggerFilterRuntime')
- ->registerException($this);
- $this->registerException();
- parent::__construct
- (
- $this->getParsedMessage(),
- $this->getErrorCode(),
- $this->__ARGUMENTS->getPreviousException()
- );
- }
- public function getErrorCode()
- {
- return $this->__ARGUMENTS->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 export()
- {
- if($this->getPrevious() instanceOf Base)
- $this->getPrevious()->export();
- $this->getCollector()->addMessage($message = new CollectorMessagesComponent);
- $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->getMessage())
- ->setReferences($this->getParsedReferences());
- if(NULL !== $this->getPrevious())
- return $this->__EXPORT;
- foreach($this->getTrace() as $line)
- {
- $this->getCollector()->addTrace($trace = new CollectorTracesComponent);
- $trace
- ->setFile(isset($line['file']) ? $line['file'] : 'internal')
- ->setLine(isset($line['line']) ? $line['line'] : 'NaN')
- ->setClass(isset($line['class']) ? $line['class'] : '')
- ->setType(isset($line['type']) ? $line['type'] : '')
- ->setArgs(isset($line['args']) ? $line['args'] : 'None')
- ->setFunction(isset($line['function']) ? $line['function'] : 'Unknown');
- }
- return $this->__EXPORT;
- }
- 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
- );
- }
- /** <refact> */
- protected function getStringByCode($code)
- {
- switch($code):
- //case System\E::SUCCESS: return 'The operation completed successfully.';
- //case System\E::ERROR_UNKNOWN: return 'Unknown Error.';
- //case System\E::INTERNAL_ERROR: return 'Internal Error.';
- endswitch;
- }
- /** </refact> */
- private function getCollector()
- {
- return ((NULL === $this->__COLLECTOR)
- ? ($this->__COLLECTOR = new Collector)
- : $this->__COLLECTOR);
- }
- /** <refact> */
- private function getMessageByCode($code)
- {
- try
- {
- $message = $this->getStringByCode($code);
- try
- {
- if(NULL === $message)
- throw new System\ArgumentNullException;
- if(!is_string($message))
- throw new System\ArgumentException
- (System\E::ARGUMENT_EXPECTED_STRING);
- if(empty($message))
- throw new System\ArgumentException
- (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(System\E::ERROR_UNKNOWN);
- }
- catch(ILLI_Exception $e)
- {
- throw new System\Exception_Exception
- (System\E::INTERNAL_ERROR);
- }
- }
- /** </refact> */
- private function getParsedMessage()
- {
- $message = $this->getMessageByCode($this->getErrorCode());
- foreach($this->__ARGUMENTS->getArguments() as $i => $val)
- {
- if(!is_scalar($val))
- continue;
- $_message = str_replace('{'.$i.'}', (($val === NULL) ? 'NULL' : $val), $message);
- if($_message != $message)
- {
- $message = $_message;
- continue;
- }
- }
- $message = preg_replace('#\{([0-9a-z]+)\}\s#i', '', $message);
- return $message;
- }
- private function getParsedReferences()
- {
- $messageinfo = array();
- foreach($this->__ARGUMENTS->getArguments() as $i => $val)
- {
- ob_start();
- var_dump($val);
- $messageinfo[$i] = ob_get_clean();
- }
- return $messageinfo;
- }
- public function __toString()
- {
- return $this->export()->asText();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement