import.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /**
  3. * Created by JetBrains PhpStorm.
  4. * User: dongyancen
  5. * Date: 13-12-10
  6. * Time: 上午1:48
  7. * To change this template use File | Settings | File Templates.
  8. */
  9. header('Content-Type: text/javascript');
  10. $import = 'import.js';
  11. function custom_strEmpty($s){
  12. $str = strlen(trim($s));
  13. if(empty($str)){
  14. return true;
  15. }
  16. return false;
  17. }
  18. function importSrc(){
  19. require_once 'config.php';
  20. global $import;
  21. if(file_exists(Config::$projroot.Config::$test_PATH.$import)){
  22. $cnt = file_get_contents(Config::$projroot.Config::$test_PATH.$import);
  23. }
  24. if($cnt == ''){
  25. if(Config::$DEBUG)
  26. print "fail read file : ".Config::$test_PATH.$import;
  27. return '';
  28. }
  29. $is = array();
  30. $flag_ownSrc = 1;
  31. $caseName = $_GET[ 'f' ];
  32. $this_src = Config::$projroot.Config::$src_PATH.$caseName.".js";
  33. //正则匹配,提取所有(///import xxx;)中的xxx
  34. preg_match_all('/\/\/\/import\s+([^;]+);?/ies', $cnt, $is, PREG_PATTERN_ORDER);
  35. foreach($is[1] as $i) {
  36. if(strcasecmp($i,$caseName)==0){
  37. $flag_ownSrc = 0;
  38. }
  39. $path = $i . '.js';
  40. $srcFile = Config::$projroot . Config::$src_PATH . $path;
  41. if (file_exists($srcFile)) {
  42. echo "document.write('<script charset=utf-8 src=\"$srcFile\"></script>');\n";
  43. }
  44. }
  45. if(file_exists($this_src)){
  46. $file=fopen($this_src,"r");
  47. while(!feof($file))
  48. {
  49. $statment = fgets($file);
  50. if(custom_strEmpty($statment)){
  51. continue ;
  52. }else if(preg_match('/\/\/\/import\s+([^;]+);?/ies', $statment,$r)){
  53. echo "document.write('<script charset=utf-8 src=\"".Config::$projroot . Config::$src_PATH.$r[1].".js\"></script>');\n";
  54. }else{
  55. break;
  56. }
  57. }
  58. fclose($file);
  59. }
  60. //加载与用例同名的原文件,并避免重复加载
  61. if($flag_ownSrc){
  62. echo "document.write('<script charset=utf-8 src=\"".$this_src."\"></script>');\n";
  63. }
  64. }
  65. importSrc();