PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/_examples/load.php

https://github.com/leaven/gmu
PHP | 173 lines | 150 code | 18 blank | 5 comment | 25 complexity | bbee065f51a6a2d9ea7ca7ab99738b80 MD5 | raw file
  1. <?php
  2. error_reporting(E_ERROR | E_WARNING | E_PARSE);
  3. // ------- functions -----------
  4. function _buildFile(&$files, $arr){
  5. global $baseDir, $cssDir, $caches, $theme;
  6. foreach($arr as $index => $file) {
  7. $file = trim($file);
  8. if(!$file)continue;
  9. if(file_exists($baseDir.$file)) {
  10. $files[$file] = array(
  11. 'js' => $file
  12. );
  13. $content=isset($caches[$file])?$caches[$file]:($caches[$file] = file_get_contents($baseDir.$file));
  14. //@import core/zepto.js, core/zepto.event.js, core/zepto.ui.js
  15. if(preg_match('/@import\s(.+?)$/ims', $content, $match)){
  16. $import = $match[1];
  17. $import = preg_replace("/\\s+/", "", $import);
  18. $imports = array_unique(explode(",", $import));
  19. $files[$file]['children'] = array();
  20. _buildFile($files[$file]['children'], $imports);
  21. }
  22. $files[$file]['css'] = array();
  23. if(preg_match('/@importcss\s(.+?)$/ims', $content, $match)){
  24. $import = $match[1];
  25. $import = preg_replace("/\\s+/", "", $import);
  26. $imports = array_unique(explode(",", $import));
  27. foreach($imports as $import){
  28. if(is_file($cssDir.$import)){
  29. $files[$file]['css'][] = $import;
  30. }
  31. if($theme){
  32. $import = preg_replace('/css$/', $theme.'.css', $import);
  33. if(is_file($cssDir.$import)){
  34. $files[$file]['css'][] = $import;
  35. }
  36. }
  37. }
  38. }
  39. if(preg_match('/^(webapp|pad)\/(.+?)(?:\.(.+?))?\.js/i', $file, $match)){// 如果是组件代码
  40. $cssFile = $match[1].'/'.$match[2].'/'.$match[2].(isset($match[3]) ?'.'.$match[3]:'').'.css';
  41. if(is_file($cssDir.$cssFile)){
  42. $files[$file]['css'][] = $cssFile;
  43. }
  44. //find theme
  45. if($theme){
  46. $cssFile = preg_replace('/\.css$/', '.'.$theme.'.css', $cssFile);
  47. if(is_file($cssDir.$cssFile)){
  48. $files[$file]['css'][] = $cssFile;
  49. }
  50. }
  51. }
  52. }
  53. }
  54. }
  55. function _gennerateFile($files, &$arr, &$arr2){
  56. global $caches;
  57. foreach($files as $file){
  58. if(isset($file['children']) && is_array($file['children'])){
  59. _gennerateFile($file['children'], $arr, $arr2);
  60. }
  61. $js = $file['js'];
  62. if(!isset($arr[$js])){
  63. $arr[$js] = $caches[$js];
  64. }
  65. if(isset($file['css'])){
  66. $arr2 = array_unique(array_merge($arr2, $file['css']));
  67. }
  68. }
  69. return $arr;
  70. }
  71. function fixImagePath($cssFile){
  72. static $basepath;
  73. $content = file_get_contents($cssFile);
  74. if(!$basepath) {
  75. if(!isset($_SERVER['HTTP_REFERER']))return $content;
  76. $basepath = $_SERVER['HTTP_REFERER'];
  77. //$basepath = 'http://127.0.0.1/mobile/gmu/_examples/webapp/dialog/dialog.html';
  78. $basepath = parse_url($basepath);
  79. $basepath = dirname($_SERVER['DOCUMENT_ROOT'].$basepath['path']);
  80. }
  81. preg_match_all('/url\((([\'"]?)(?!data)([^\'"]+?)\2)\)/im', $content, $m);
  82. if(isset($m[3])) {
  83. foreach($m[3] as $image) {
  84. if(!preg_match('/\.(gif|png|jpg|jpeg)$/i', $image))continue;
  85. $imagePath = realpath(dirname($cssFile).'/'.$image);
  86. if(!$imagePath) continue;
  87. $relativePath = getRelativePath($imagePath, $basepath);
  88. $content = str_replace($image, $relativePath, $content);
  89. }
  90. }
  91. return $content;
  92. }
  93. function getRelativePath($path, $relativePath){
  94. $relativePath = rtrim($relativePath, '/');
  95. $newPath = '';
  96. $path = explode("/", str_replace('\\', '/', $path));
  97. $relativePath = explode("/", str_replace('\\', '/', $relativePath));
  98. foreach( $path as $k => $v) {
  99. if($v != $relativePath[$k] )break;
  100. }
  101. array_splice($path, 0, $k);
  102. array_splice($relativePath, 0, $k);
  103. $newPath = str_pad($newPath, count($relativePath)*3, '../');
  104. $newPath .= implode("/", $path);
  105. return $newPath;
  106. }
  107. //--------logic -------
  108. $type = isset($_REQUEST['type']) ? $_REQUEST['type'] : '';
  109. $js = isset($_REQUEST['js']) ? $_REQUEST['js'] : '';
  110. $theme = isset($_REQUEST['theme']) ? $_REQUEST['theme'] : '';
  111. $baseUrl = isset($_REQUEST['base']) ? $_REQUEST['base'] : '../../../_src/';
  112. $mode = isset($_REQUEST['mode']) ? $_REQUEST['mode'] : '';
  113. $baseDir = dirname(dirname(__FILE__)).'/_src/';
  114. $cssDir = dirname(dirname(__FILE__)).'/assets/';
  115. $allowedTypes = array('webapp', 'pad', 'core');
  116. if (!in_array($type, $allowedTypes)) {
  117. $type = current($allowedTypes);
  118. }
  119. $js = preg_replace("/ +/", "", $js);
  120. $jses = array_unique(explode(",", $js));
  121. $caches = array(
  122. 'core/zepto.js' => file_get_contents($baseDir.'core/zepto.js')
  123. );
  124. $files = array(
  125. 'core/zepto.js' => array(
  126. 'js' => 'core/zepto.js'
  127. )
  128. );
  129. _buildFile($files, $jses);
  130. $alljs = array();
  131. $allCss = array('reset.css');
  132. _gennerateFile($files, $alljs, $allCss);
  133. if($mode){
  134. header("Content-type: text/xml");
  135. $xml = '<?xml version="1.0" encoding="UTF-8" ?>'."\n";
  136. $xml .= '<root>';
  137. $xml .= '<jses>';
  138. foreach($alljs as $key => $val) {
  139. $xml .= '<js name="'.$key.'"><![CDATA['.$val.']]></js>';
  140. }
  141. $xml .='</jses>';
  142. $xml .= '<csses>';
  143. foreach($allCss as $val) {
  144. is_file($cssDir.$val) && ($xml .= '<css name="'.$val.'"><![CDATA['.fixImagePath($cssDir.$val).']]></css>');
  145. }
  146. $xml .='</csses>';
  147. $xml .= '</root>';
  148. echo $xml;
  149. return;
  150. }
  151. header('Content-Type: application/javascript');
  152. ?>
  153. <?php foreach($alljs as $key => $val):?>
  154. document.write('<script type="text/javascript" name="<?php echo $key?>" src="<?php echo $baseUrl.$key?>"></script>');
  155. <?php endforeach; ?>