Browse Source

Ajout d'une classe de log

Simounet 5 years ago
parent
commit
ff2a7dc8a0
2 changed files with 63 additions and 0 deletions
  1. 62 0
      Logger.class.php
  2. 1 0
      common.php

+ 62 - 0
Logger.class.php

@@ -0,0 +1,62 @@
+<?php
+
+class Logger {
+
+    CONST LOGGER_IDENTIFIER = 'logger';
+    protected $name;
+    protected $logs = array();
+
+    public function __construct($name = 'messages'){
+        $this->name = $name;
+    }
+
+    public function save(){
+        $_SESSION[self::LOGGER_IDENTIFIER][$this->getName()] = serialize($this->logs);
+    }
+
+    public function appendLogs($msg){
+        $this->logs[] = $msg;
+    }
+
+    public function loadLogs()
+    {
+        $logs = $this->getLogs();
+        $this->destroy();
+        return $logs;
+    }
+
+    protected function destroy(){
+        unset($_SESSION[self::LOGGER_IDENTIFIER][$this->getName()]);
+    }
+
+    protected function isEmpty(){
+        return ! isset($_SESSION[self::LOGGER_IDENTIFIER][$this->getName()])
+            || empty($_SESSION[self::LOGGER_IDENTIFIER][$this->getName()]);
+    }
+
+    public function setName($name)
+    {
+        $this->name = $name;
+    }
+
+    public function getName()
+    {
+        return $this->name;
+    }
+
+    public function setLogs($logs)
+    {
+        $this->logs = $logs;
+    }
+
+    public function getLogs()
+    {
+        if($this->isEmpty()){
+            return $this->logs;
+        }
+        return unserialize( $_SESSION[self::LOGGER_IDENTIFIER][$this->getName()] );
+    }
+
+}
+
+?>

+ 1 - 0
common.php

@@ -50,6 +50,7 @@ class_exists('User') or require_once('User.class.php');
 class_exists('Folder') or require_once('Folder.class.php');
 class_exists('Configuration') or require_once('Configuration.class.php');
 class_exists('Opml') or require_once('Opml.class.php');
+class_exists('Logger') or require_once('Logger.class.php');
 
 
 //error_reporting(E_ALL);