logparser.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. class Controller {
  3. protected $model;
  4. protected $view;
  5. public function __construct() {
  6. // initialize needed constructors
  7. $this->model = new Model;
  8. $this->view = new View;
  9. }
  10. public function process($argv) {
  11. // do we already know which log we want to look at? check for arguments
  12. if(!isset($argv[1])) {
  13. echo "Please select a log to analyze. ie.:\n\tphp logparse 1\n\tphp logparse list\nto obtain a list of logs to view.\n";
  14. } elseif($argv[1] == 'list') {
  15. $helper = "--------------------------------------------\n" .
  16. "For the log you wish to view, run a command similar to:\n" .
  17. "\tphp logparse 6\n ..to view the log with #6 as it's key.\n" .
  18. "--------------------------------------------\n\n";
  19. echo $helper;
  20. // output a list of logs to choose from
  21. echo "\n\tKEY:\tLOG FILE\n";
  22. foreach($this->model->log_locations() as $key => $log) {
  23. echo "\t" . $key . "\t" . $log . "\n";
  24. }
  25. echo "\n\n" . $helper;
  26. } elseif (is_numeric($argv[1])){
  27. // notify the model which log we want to look at & send to the view
  28. $log = $this->selection($argv[1]);
  29. return $this->view->output($log);
  30. }
  31. }
  32. public function selection($key){
  33. // parse which key was called from the cli
  34. foreach($this->model->log_locations() as $log_key => $log) {
  35. // only open the requested log
  36. if ($log_key == $key) {
  37. $log_to_open = fopen($log, "rb");
  38. $detail = fread($log_to_open, filesize($log));
  39. fclose($log_to_open);
  40. // pass log output to the view via process() controller function
  41. return $detail;
  42. }
  43. }
  44. }
  45. }