exec.class.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. require_once __DIR__ . '/console.class.php';
  3. require_once __DIR__ . '/../functions/function.php';
  4. class modalConsoleExecProcessor extends modalConsoleProcessor
  5. {
  6. protected $queries = [];
  7. protected $queryTime = [];
  8. protected $memory = [];
  9. protected $memoryPeak = [];
  10. protected $time = [];
  11. public function process()
  12. {
  13. error_reporting(E_ALL);
  14. ini_set("display_errors", true);
  15. ob_start();
  16. $this->snapshot('before');
  17. /*try {
  18. $result = eval($this->code);
  19. } catch (ParseError $e) {
  20. return $this->response(false, $e->getMessage());
  21. }*/
  22. $result = modalConsoleEval($this->modx, $this->code);
  23. $this->snapshot('after');
  24. $output = ob_get_contents();
  25. ob_end_clean();
  26. if ($result === false) {
  27. return $this->response(false, error_get_last());
  28. } elseif (!empty($result) && is_string($result)) {
  29. $output .= $result;
  30. }
  31. if ($this->getProperty('save', false)) {
  32. $this->history->addItem(md5($this->code), $this->code)->save();
  33. }
  34. return $this->response(true, '', array_merge(['output' => $output, 'keys' => $this->history->getKeys()], ['profile' => $this->prepareResult()]));
  35. }
  36. public function snapshot($key)
  37. {
  38. $this->queries[$key] = isset($this->modx->executedQueries) ? $this->modx->executedQueries : 0;
  39. $this->queryTime[$key] = $this->modx->queryTime;
  40. $this->memoryPeak[$key] = memory_get_peak_usage(true);
  41. $this->memory[$key] = memory_get_usage(true);
  42. $this->time[$key] = microtime(true);
  43. }
  44. public function prepareResult()
  45. {
  46. $totalTime = round($this->getDiff($this->time), 3);
  47. $sqlTime = $this->getDiff($this->queryTime);
  48. $result = [
  49. "queries" => $this->getDiff($this->queries),
  50. //"SQL time" => sprintf("%2.3f s", $sqlTime),
  51. "time" => sprintf("%2.3f s / %2.3f s / %2.3f s", $sqlTime, abs($totalTime - $sqlTime), $totalTime),
  52. // "totaltime" => sprintf("%2.3f s", $totalTime),
  53. "memory" => sprintf("%2.3f MB / %2.3f MB", $this->getDiffMemory($this->memory), $this->getDiffMemory($this->memoryPeak)),
  54. ];
  55. return $result;
  56. }
  57. protected function getDiff($prop)
  58. {
  59. return $prop['after'] - $prop['before'];
  60. }
  61. protected function getDiffMemory($prop)
  62. {
  63. return round(($prop['after'] - $prop['before']) / 1048576, 2);
  64. }
  65. }
  66. return 'modalConsoleExecProcessor';