Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. <?php
  2. declare(strict_types = 1);
  3.  
  4. namespace SalmonDE\CrashLogger;
  5.  
  6. use RuntimeException;
  7. use function base64_decode;
  8. use function fclose;
  9. use function fgets;
  10. use function fopen;
  11. use function is_array;
  12. use function json_decode;
  13. use function trim;
  14. use function zlib_decode;
  15.  
  16. class CrashDumpReader {
  17.  
  18. private $filePath;
  19. private $data = null;
  20.  
  21. public function __construct(string $filePath){
  22. $this->filePath = $filePath;
  23. $this->readData();
  24. }
  25.  
  26. final private function readData(): void{
  27. $fileHandle = fopen($this->filePath, 'r');
  28.  
  29. $start = false;
  30. $end = false;
  31.  
  32. $data = '';
  33. while($line = fgets($fileHandle)){
  34. $line = trim($line);
  35.  
  36. if($start === true){
  37. if($end === false){
  38. if($line === '===END CRASH DUMP==='){
  39. $end = true;
  40. break;
  41. }else{
  42. $data .= $line;
  43. }
  44. }
  45. }elseif($line === '===BEGIN CRASH DUMP==='){
  46. $start = true;
  47. }
  48. }
  49. fclose($fileHandle);
  50.  
  51. if($start === true and $end === true and trim($data) !== ''){
  52. $data = base64_decode($data);
  53. $data = zlib_decode($data);
  54. $data = json_decode($data, true);
  55. $this->data = $data;
  56. }
  57. }
  58.  
  59. public function hasRead(): bool{
  60. return is_array($this->data);
  61. }
  62.  
  63. public function getFilePath(): string{
  64. return $this->filePath;
  65. }
  66.  
  67. public function getData(): ?array{
  68. return $this->data;
  69. }
  70.  
  71. public function getCreationTime(): int{
  72. if(!$this->hasRead()){
  73. throw new RuntimeException('No data was read');
  74. }
  75.  
  76. return $this->data['time'];
  77. }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement