Advertisement
fruffl

xml-mapper for exception

Nov 15th, 2011
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.92 KB | None | 0 0
  1. <?PHP
  2. try
  3. {
  4.     $ILLI = ILLI::init();
  5.     ILLI::appendStaticInstance('TEST_LOADERS', 'Test/Loader/foo.php', array('foo' => 'bar', 'baz'));
  6. }
  7. catch(ILLI_Exception $e)
  8. {
  9.     print $e->getErrorMessage(ILLI_System_Exception_Export::XML);
  10. }
  11.  
  12. /* output: */
  13.  
  14. <?xml version="1.0"?>
  15. <illi:exception xmlns="http://www.w3.org/2005/Atom" xmlns:illi="http://www.w3.org/2005/Atom">
  16.   <messages>
  17.     <message exception="ILLI_Exception_FileContentExpected" errorcode="0x00010504" level="1">
  18.       <thrown file="/var/www/usr/luc/terminal2/web/dev/ILLI/rev.2.0.0-0/VENDOR/ILLI/Class.php" line="00000134"/>
  19.       <message><![CDATA[Class-file /var/www/usr/luc/terminal2/web/dev/ILLI/rev.2.0.0-0/VENDOR/Test/Loader/foo.php is empty or class-name TEST_LOADERS is invalid.]]></message>
  20.     </message>
  21.     <message exception="ILLI_Exception_SystemStaticInstanceLoad" errorcode="0x00010602" level="2">
  22.       <thrown file="/var/www/usr/luc/terminal2/web/dev/ILLI/rev.2.0.0-0/VENDOR/ILLI/Class.php" line="00000139"/>
  23.       <message><![CDATA[Unable to load instance TEST_LOADERS.]]></message>
  24.  
  25.     </message>
  26.     <message exception="ILLI_Exception_SystemStaticInstanceAppend" errorcode="0x00010603" level="3">
  27.       <thrown file="/var/www/usr/luc/terminal2/web/dev/ILLI/rev.2.0.0-0/VENDOR/ILLI/Class.php" line="00000098"/>
  28.       <message><![CDATA[Unable to append static instance TEST_LOADERS.]]></message>
  29.     </message>
  30.   </messages>
  31.   <traces>
  32.     <trace>
  33.       <despatcher method="ILLI::appendStaticInstance()" class="ILLI" type="::" function="appendStaticInstance"/>
  34.  
  35.       <location file="/var/www/usr/luc/terminal2/web/dev/index.php" line="40"/>
  36.       <arguments>
  37.         <argument parameter="1"><![CDATA[
  38. string(12) "TEST_LOADERS"
  39. ]]></argument>
  40.         <argument parameter="2"><![CDATA[
  41. string(19) "Test/Loader/foo.php"
  42. ]]></argument>
  43.         <argument parameter="3"><![CDATA[
  44. array(2) {
  45.   ["foo"]=>
  46.   string(3) "bar"
  47.   [0]=>
  48.   string(3) "baz"
  49. }
  50. ]]></argument>
  51.       </arguments>
  52.     </trace>
  53.     <trace>
  54.       <despatcher method="ILLI-&gt;loadStaticInstances()" class="ILLI" type="-&gt;" function="loadStaticInstances"/>
  55.  
  56.       <location file="/var/www/usr/luc/terminal2/web/dev/ILLI/rev.2.0.0-0/VENDOR/ILLI/Class.php" line="93"/>
  57.       <arguments/>
  58.     </trace>
  59.   </traces>
  60. </illi:exception>
  61.  
  62. /* class */
  63. <?PHP
  64.  
  65.     CLASS ILLI_System_Exception_Export_Type_Xml EXTENDS ILLI_System_Exception_Export_Type_Abstract
  66.     {
  67.         private $DOM = NULL;
  68.         private $DOM_MESSAGES = NULL;
  69.         private $DOM_TRACES = NULL;
  70.        
  71.         private function get_XML()
  72.         {
  73.             if($this->DOM instanceOf DOMDocument)
  74.                 return $this->DOM;
  75.            
  76.             $this->DOM = new DOMDocument;
  77.             $this->DOM->formatOutput = true;
  78.             $this->DOM->preserveWhiteSpace = true;
  79.             $this->DOM->validateOnParse = true;
  80.            
  81.             return $this->DOM;
  82.         }
  83.        
  84.         private function get_XMLMessages()
  85.         {
  86.             if(NULL === $this->DOM_MESSAGES)
  87.                 $this->DOM_MESSAGES = $this->get_XML()->createElement("messages");
  88.            
  89.             return $this->DOM_MESSAGES;
  90.         }
  91.        
  92.         private function get_XMLTraces()
  93.         {
  94.             if(NULL === $this->DOM_TRACES)
  95.                 $this->DOM_TRACES = $this->get_XML()->createElement("traces");
  96.            
  97.             return $this->DOM_TRACES;
  98.         }
  99.        
  100.         protected function parseMessages(ILLI_System_Exception_Export_Queue_Messages $queue)
  101.         {
  102.             $messages = $this->get_XMLMessages();
  103.            
  104.             foreach($queue->get() as $item)
  105.             {
  106.                 $message = $this->get_XML()->createElement('message');
  107.                 $message->setAttribute('exception', $item->getExceptionClass());
  108.                 $message->setAttribute('errorcode', $item->getErrorCode());
  109.                 $message->setAttribute('level', $item->getErrorLevel());
  110.                
  111.                 $thrown = $this->get_XML()->createElement('thrown');               
  112.                 $thrown->setAttribute('file', $item->getFile());
  113.                 $thrown->setAttribute('line', $item->getLine());
  114.                
  115.                 $messagetext = $this->get_XML()->createElement('message');
  116.                 $messagetext->appendChild(new DOMCdataSection($item->getMessage()));
  117.                
  118.                 $message->appendChild($thrown);
  119.                 $message->appendChild($messagetext);
  120.                 $messages->appendChild($message);
  121.             }
  122.            
  123.             return $messages;
  124.         }
  125.        
  126.         protected function parseTraces(ILLI_System_Exception_Export_Queue_Traces $queue)
  127.         {
  128.             $traces = $this->get_XMLTraces();
  129.             foreach($queue->get() as $item)
  130.             {
  131.                 $trace = $this->get_XML()->createElement('trace');
  132.                 $traces->appendChild($trace);
  133.                
  134.                 $despatcher = $this->get_XML()->createElement('despatcher');
  135.                 $trace->appendChild($despatcher);
  136.                
  137.                 $location = $this->get_XML()->createElement('location');
  138.                 $trace->appendChild($location);
  139.                
  140.                 $arguments = $this->get_XML()->createElement('arguments');
  141.                 $trace->appendChild($arguments);
  142.                
  143.                
  144.                 $despatcher->setAttribute('method', $item->getClass().$item->getType().$item->getFunction().'()');
  145.                 $despatcher->setAttribute('class', $item->getClass());
  146.                 $despatcher->setAttribute('type', $item->getType());
  147.                 $despatcher->setAttribute('function', $item->getFunction());               
  148.                
  149.                 $location->setAttribute('file', $item->getFile());
  150.                 $location->setAttribute('line', $item->getLine());
  151.                
  152.                 $p = 0;
  153.                 foreach($item->getArgs() as $arg)
  154.                 {
  155.                     $p++;
  156.                     ob_start();
  157.                     var_dump($arg);
  158.                     $arg = "\n".ob_get_clean();
  159.                    
  160.                     $argument = $this->get_XML()->createElement('argument');
  161.                     $arguments->appendChild($argument);                
  162.                    
  163.                     $argument->setAttribute('parameter', $p);
  164.                     $argument->appendChild(new DOMCdataSection($arg));
  165.                 }
  166.                
  167.             }
  168.            
  169.             return $traces;
  170.         }
  171.        
  172.         protected function parseExportException($parsedMessages, $parsedTraces)
  173.         {
  174.             $root = $this->get_XML()->createElement('illi:exception');
  175.             $root->setAttribute('xmlns', 'http://www.w3.org/2005/Atom');
  176.             $root->setAttribute('xmlns:illi', 'http://www.w3.org/2005/Atom');
  177.             $root->appendChild($this->get_XMLMessages());
  178.             $root->appendChild($this->get_XMLTraces());
  179.             $this->get_XML()->appendChild($root);
  180.            
  181.             return $this->get_XML()->saveXML();
  182.         }
  183.     }
  184.  
  185.  
  186.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement