Posted By

mafro on 07/11/07


Tagged

message stack


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

kevins


Message Stack


 / Published in: PHP
 

PHP Error / Message Stack

  1. <?php
  2.  
  3. /**
  4. *
  5. * @author MJB
  6. * @date 18/01/06
  7. * @desc MessageStack & Message class - Error/Warning handling
  8. * @ver 1.6
  9. *
  10. * @notes * since the stack only exists for a single request-response
  11. * the object never needs to be manually cleared of messages
  12. * * smarty frontend
  13. * * now does static methods and line numbers
  14. */
  15.  
  16.  
  17. class MessageStack {
  18.  
  19. var $_msgs;
  20.  
  21. function MessageStack() {}
  22.  
  23. /**
  24. * @desc create new message on stack
  25. * @params overriden message if error passed from outside (ie Pear)
  26. */
  27. function add($source_class, $line_num, $error_code, $message="") {
  28. $this->_msgs[] = &new Message($source_class, $line_num, $error_code, $message);
  29. }
  30.  
  31.  
  32. /**
  33. * @desc parse Message objects into array for smarty
  34. */
  35. function display() {
  36. if(!isset($this->_msgs)) { return array(); }
  37.  
  38. foreach($this->_msgs as $msg) {
  39. $output[] = array("class" => $msg->getClass(),
  40. "code" => $msg->getCode(),
  41. "line" => $msg->getLine(),
  42. "message" => $msg->getMessage(),
  43. "link" => $msg->getLink());
  44. }
  45. return $output;
  46. }
  47.  
  48. function hasErrors() {
  49. return count($this->_msgs);
  50. }
  51.  
  52. }
  53.  
  54.  
  55. class Message {
  56.  
  57. var $_classMethod; //source class name & getMessage method
  58. var $_line; //erronous line
  59. var $_code; //error code translates to a message
  60. var $_message; //error message
  61. var $_link; //link to solution
  62.  
  63. function Message($source, $line, $code, $msg="") {
  64. $this->_classMethod = array($source, "getMessage");
  65. $this->_line = $line;
  66. $this->_code = $code;
  67. $this->_link = "";
  68.  
  69. //use passed msg or attempt to get from calling class
  70. if(strlen($msg)>0) {
  71. $this->_message = $msg;
  72.  
  73. }elseif(is_callable($this->_classMethod)) {
  74. //call getMessage on object statically
  75. $tmp = call_user_func($this->_classMethod, $code);
  76.  
  77. //parse message & link if given
  78. if(count($tmp)==2) {
  79. $this->_message = $tmp['text'];
  80. $this->_link = $tmp['link'];
  81. }else{
  82. $this->_message = $tmp['text'];
  83. }
  84.  
  85. }else{
  86. $this->_message = "Undefined error message";
  87. }
  88. }
  89.  
  90. function getClass() {
  91. return $this->_classMethod[0];
  92. }
  93. function getLine() {
  94. return (DEBUG) ? $this->_line : "";
  95. }
  96. function getCode() {
  97. return $this->_code; //code for info/error
  98. }
  99. function getMessage() {
  100. return $this->_message;
  101. }
  102. function getLink() {
  103. return $this->_link;
  104. }
  105.  
  106. }
  107.  
  108. ?>

Report this snippet  

You need to login to post a comment.