Logger.class.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 loadLogs()
  16. {
  17. $logs = $this->getLogs();
  18. $this->destroy();
  19. return $logs;
  20. }
  21. protected function destroy(){
  22. unset($_SESSION[self::LOGGER_IDENTIFIER][$this->getName()]);
  23. }
  24. protected function isEmpty(){
  25. return ! isset($_SESSION[self::LOGGER_IDENTIFIER][$this->getName()])
  26. || empty($_SESSION[self::LOGGER_IDENTIFIER][$this->getName()]);
  27. }
  28. public function setName($name)
  29. {
  30. $this->name = $name;
  31. }
  32. public function getName()
  33. {
  34. return $this->name;
  35. }
  36. public function setLogs($logs)
  37. {
  38. $this->logs = $logs;
  39. }
  40. public function getLogs()
  41. {
  42. if($this->isEmpty()){
  43. return $this->logs;
  44. }
  45. return unserialize( $_SESSION[self::LOGGER_IDENTIFIER][$this->getName()] );
  46. }
  47. }
  48. ?>