From 6afe1b5c52d3cd9060023efc84e0895dfe5cfa5d Mon Sep 17 00:00:00 2001 From: Dominique Fournier Date: Sun, 8 Jul 2018 12:06:06 +0000 Subject: [PATCH] console : store the history with timestamp git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@4275 bf3deb0d-5f1a-0410-827f-c0cc1f45334c --- console.php | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/console.php b/console.php index 30e5e2b..c331cf0 100644 --- a/console.php +++ b/console.php @@ -735,7 +735,7 @@ class console // }}} /** Clear the history - * Do NOT write the empty history on disk + * This method do NOT write the empty history on disk */ public function clearHistory () // {{{ @@ -771,8 +771,10 @@ class console $this->consoleException ("History file '$historyFile' ". "can not be created: parent directory is not a directory"); file_put_contents ($historyFile, "__HISTORY__\n"); - file_put_contents ($historyFile, implode ("\n", $this->history), - FILE_APPEND); + $history = ""; + foreach ($this->history as $time => $command) + $history .= "$time $command\n"; + file_put_contents ($historyFile, $history, FILE_APPEND|LOCK_EX); return $this; } // }}} @@ -780,6 +782,7 @@ class console /** Read the history from the disk * If the file doesn't exists, return an empty array * @param string $historyFile The history file where the history is stored + * @return the read history with timestamp as key and command as value */ public function readHistory ($historyFile) // {{{ @@ -799,14 +802,20 @@ class console $this->consoleException ("History file '$historyFile' ". "is not an history file : do not touch\n"); array_shift ($historyArr); - $this->history = $historyArr; + foreach ($historyArr as $line) + { + @list ($time, $command) = @explode (" ", $line, 2); + if ($time === null || $command === null) + continue; + $this->history[$time] = $command; + } return $this->history; } // }}} /** Add a new entry in history. * The new line can not be empty : it is not stored, but without error - * Do NOT write the history on disk. + * This method do NOT write the history on disk : you must use writeHistory * @param string The new entry to add in history */ public function addHistory ($line) @@ -817,8 +826,9 @@ class console "it is not a string"); if (trim ($line) === "") return $this; - $this->history[] = $line; - $this->history = array_slice ($this->history, -$this->historyMaxSize); + $this->history[time()] = $line; + $this->history = array_slice ($this->history, -$this->historyMaxSize, null, + true); return $this; } // }}}