LogFile.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace RocketTheme\Toolbox\File;
  3. /**
  4. * Implements Log File reader.
  5. *
  6. * @package RocketTheme\Toolbox\File
  7. * @author RocketTheme
  8. * @license MIT
  9. */
  10. class LogFile extends File
  11. {
  12. /**
  13. * @var array|File[]
  14. */
  15. static protected $instances = array();
  16. /**
  17. * Constructor.
  18. */
  19. protected function __construct()
  20. {
  21. parent::__construct();
  22. $this->extension = '.log';
  23. }
  24. /**
  25. * Check contents and make sure it is in correct format.
  26. *
  27. * @param array $var
  28. * @return array
  29. */
  30. protected function check($var)
  31. {
  32. return (array) $var;
  33. }
  34. /**
  35. * Encode contents into RAW string (unsupported).
  36. *
  37. * @param string $var
  38. * @return string|void
  39. * @throws \Exception
  40. */
  41. protected function encode($var)
  42. {
  43. throw new \Exception('Saving log file is forbidden.');
  44. }
  45. /**
  46. * Decode RAW string into contents.
  47. *
  48. * @param string $var
  49. * @return array mixed
  50. */
  51. protected function decode($var)
  52. {
  53. $lines = (array) preg_split('#(\r\n|\n|\r)#', $var);
  54. $results = array();
  55. foreach ($lines as $line) {
  56. preg_match('#^\[(.*)\] (.*) @ (.*) @@ (.*)$#', $line, $matches);
  57. if ($matches) {
  58. $results[] = ['date' => $matches[1], 'message' => $matches[2], 'url' => $matches[3], 'file' => $matches[4]];
  59. }
  60. }
  61. return $results;
  62. }
  63. }