/php/phantm.php

https://github.com/theospears/phantm · PHP · 117 lines · 79 code · 27 blank · 11 comment · 11 complexity · 5da73c8e1c138300326d736a32b9b65f MD5 · raw file

  1. <?php
  2. /**
  3. * Phantm
  4. *
  5. * Dumps the state of all variables, so that it can be precisely imported
  6. * into phantm
  7. *
  8. * Call it like that: phantm_collect_state(get_defined_vars());
  9. * @param string $path
  10. * @return string
  11. */
  12. function phantm_incl($path) {
  13. if (isset($GLOBALS['__phantm_fh_path'])) {
  14. $p = $GLOBALS['__phantm_fh_path'];
  15. } else {
  16. $p = basename($_SERVER['SCRIPT_FILENAME'])."--".date('d-m-y--H\hi\ms').".incl";
  17. $GLOBALS['__phantm_fh_path'] = $p;
  18. }
  19. $fh = fopen($p, "a");
  20. $bt = debug_backtrace();
  21. $file = $bt[0]['file'];
  22. $line = $bt[0]['line'];
  23. fwrite($fh, strlen($file).":".$file.":".$line.":".strlen($path).":".$path."\n");
  24. fclose($fh);
  25. copy($p, "last.incl");
  26. return $path;
  27. }
  28. function phantm_collect_state(array $vars) {
  29. $bt = debug_backtrace();
  30. $file = $bt[0]['file'];
  31. $line = $bt[0]['line'];
  32. // remap global entries to superglobals
  33. $vars['GLOBALS']['GLOBALS'] = &$vars['GLOBALS'];
  34. $vars['GLOBALS']['_GET'] = &$vars['_GET'];
  35. $vars['GLOBALS']['_POST'] = &$vars['_POST'];
  36. $vars['GLOBALS']['_REQUEST'] = &$vars['_REQUEST'];
  37. $vars['GLOBALS']['_COOKIE'] = &$vars['_COOKIE'];
  38. $vars['GLOBALS']['_SESSION'] = &$vars['_SESSION'];
  39. $vars['GLOBALS']['_FILES'] = &$vars['_FILES'];
  40. $vars['GLOBALS']['_ENV'] = &$vars['_ENV'];
  41. $vars['GLOBALS']['_SERVER'] = &$vars['_SERVER'];
  42. if (ini_get('register_long_arrays')) {
  43. $vars['HTTP_GET_VARS'] = &$vars['_GET'];
  44. $vars['HTTP_POST_VARS'] = &$vars['_POST'];
  45. $vars['HTTP_ENV_VARS'] = &$vars['_ENV'];
  46. $vars['HTTP_COOKIE_VARS'] = &$vars['_COOKIE'];
  47. $vars['HTTP_SERVER_VARS'] = &$vars['_SERVER'];
  48. $vars['GLOBALS']['HTTP_GET_VARS'] = &$vars['_GET'];
  49. $vars['GLOBALS']['HTTP_POST_VARS'] = &$vars['_POST'];
  50. $vars['GLOBALS']['HTTP_ENV_VARS'] = &$vars['_ENV'];
  51. $vars['GLOBALS']['HTTP_COOKIE_VARS'] = &$vars['_COOKIE'];
  52. $vars['GLOBALS']['HTTP_SERVER_VARS'] = &$vars['_SERVER'];
  53. }
  54. $path = basename(basename($_SERVER['SCRIPT_FILENAME']))."--".date('d-m-y--H\hi\ms').".dump";
  55. $fh = fopen($path, "w");
  56. if (!$fh) die("Failed to write dump...");
  57. fwrite($fh, "# Dumped state of ".$file." at line ".$line." \n");
  58. fwrite($fh, "# Date: ".date("r")."\n");
  59. fwrite($fh, "# Included files:\n");
  60. $files = get_included_files();
  61. foreach ($files as $f) {
  62. $f = realpath($f);
  63. if (($f === __FILE__) || ($f === $file)) continue;
  64. fwrite($fh, filemtime($f).":".$f."\n");
  65. }
  66. fwrite($fh, "# Function declarations:\n");
  67. $funcs = get_defined_functions();
  68. foreach ($funcs['user'] as $f) {
  69. if ($f == 'phantm_incl' || $f == 'phantm_collect_state') continue;
  70. $rf = new ReflectionFunction($f);
  71. fwrite($fh, $f.":".$rf->getStartLine().":".$rf->getFileName()."\n");
  72. }
  73. fwrite($fh, "# Classes declarations:\n");
  74. $classes = get_declared_classes();
  75. foreach ($classes as $c) {
  76. $rc = new ReflectionClass($c);
  77. if (!$rc->isUserDefined()) continue;
  78. fwrite($fh, $c.":".$rc->getStartLine().":".$rc->getFileName()."\n");
  79. }
  80. fwrite($fh, "# Constants:\n");
  81. $allConsts = get_defined_constants(true);
  82. $consts = !empty($allConsts['user']) ? $allConsts['user'] : array();
  83. fwrite($fh, strtr(serialize($consts), array("\\" => "\\\\", "\n" => "\\n", "\r" => "\\r"))."\n");
  84. fwrite($fh, "# Heap state:\n");
  85. unset($vars['GLOBALS']);
  86. fwrite($fh, strtr(serialize($vars), array("\\" => "\\\\", "\n" => "\\n", "\r" => "\\r"))."\n");
  87. fclose($fh);
  88. copy($path, "last.dump");
  89. exit("\n--- phantm: Done recording state to ".$path.", shutting down ---\n");
  90. }