Logger.class.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. class Logger {
  3. CONST LOGGER_IDENTIFIER = 'logger';
  4. protected $name;
  5. protected $logs = array();
  6. public function __construct($name = 'messages'){
  7. $this->name = $name;
  8. }
  9. public function save(){
  10. $_SESSION[self::LOGGER_IDENTIFIER][$this->getName()] = serialize($this->logs);
  11. }
  12. public function appendLogs($msg){
  13. $this->logs[] = $msg;
  14. }
  15. public function flushLogs()
  16. {
  17. $logs = $this->getLogs();
  18. $this->destroy();
  19. return $logs;
  20. }
  21. public function destroy(){
  22. unset($_SESSION[self::LOGGER_IDENTIFIER][$this->getName()]);
  23. }
  24. protected function mergeLogsInSession(){
  25. if( isset($_SESSION[self::LOGGER_IDENTIFIER][$this->getName()])
  26. || !empty($_SESSION[self::LOGGER_IDENTIFIER][$this->getName()])
  27. ) {
  28. $this->logs = array_unique(array_merge($this->logs, unserialize($_SESSION[self::LOGGER_IDENTIFIER][$this->getName()])));
  29. }
  30. }
  31. public function hasLogs()
  32. {
  33. return count($this->logs) > 0;
  34. }
  35. public function setName($name)
  36. {
  37. $this->name = $name;
  38. }
  39. public function getName()
  40. {
  41. return $this->name;
  42. }
  43. public function setLogs($logs)
  44. {
  45. $this->logs = $logs;
  46. }
  47. public function getLogs()
  48. {
  49. $this->mergeLogsInSession();
  50. return $this->logs;
  51. }
  52. }
  53. ?>