PageRenderTime 24ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/tool/generator/src/WindGenerateProject.php

http://github.com/phpwind/windframework
PHP | 227 lines | 133 code | 17 blank | 77 comment | 16 complexity | c81e005c3b793a32c3f2a573189244d9 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. Wind::import('WIND:utility.WindFolder');
  3. Wind::import('WIND:utility.WindFile');
  4. /**
  5. * 生成工程流程
  6. *
  7. * @author Shi Long <long.shi@alibaba-inc.com>
  8. * @copyright ©2003-2103 phpwind.com
  9. * @license http://www.windframework.com
  10. * @version $Id$
  11. * @package tool
  12. * @subpackage generator
  13. */
  14. class WindGenerateProject {
  15. /**
  16. * 工程目录
  17. */
  18. public $dir;
  19. /**
  20. * 工程名称
  21. */
  22. public $name;
  23. /**
  24. * 工程缓存目录
  25. */
  26. public $dataDir;
  27. /**
  28. * 工程模板目录
  29. */
  30. public $templateDir;
  31. /**
  32. * 工程业务逻辑目录
  33. */
  34. public $srcDir;
  35. /**
  36. * 工程配置目录
  37. */
  38. public $confDir;
  39. /**
  40. * 工程可访问资源目录
  41. */
  42. public $wwwDir;
  43. private $confFile;
  44. /**
  45. * 创建工程接口
  46. *
  47. */
  48. public function generate() {
  49. WindFolder::mkRecur($this->dir);
  50. if (!is_writable($this->dir)) return false;
  51. $result = $this->generateData()
  52. && $this->generateTemplate()
  53. && $this->generateSrc()
  54. && $this->generateConf()
  55. && $this->generateWww();
  56. return $result;
  57. }
  58. /**
  59. * 解析配置文件
  60. *
  61. * @param string $config
  62. */
  63. public function setConfig($config) {
  64. if (is_string($config)) $config = @include Wind::getRealPath($config, true);
  65. $this->dataDir = $config['dataDir'];
  66. $this->templateDir = $config['templateDir'];
  67. $this->srcDir = $config['srcDir'];
  68. $this->confDir = $config['confDir'];
  69. $this->wwwDir = $config['wwwDir'];
  70. }
  71. /**
  72. * 生成data目录
  73. *
  74. */
  75. protected function generateData() {
  76. WindFolder::mkRecur($this->dir . '/' . $this->dataDir);
  77. $this->dataDir = $this->_resolveDirName($this->dataDir);
  78. return true;
  79. }
  80. /**
  81. * 生成模板目录
  82. *
  83. */
  84. protected function generateTemplate() {
  85. WindFolder::mkRecur($this->dir . '/' . $this->templateDir);
  86. $this->templateDir = $this->_resolveDirName($this->templateDir);
  87. return true;
  88. }
  89. /**
  90. * 生成src目录及IndexController.php
  91. *
  92. */
  93. protected function generateSrc() {
  94. $content = <<<EOF
  95. <?php
  96. class IndexController extends WindController {
  97. public function run() {
  98. //TODO insert your code here
  99. echo 'hello, wind Framework!';
  100. }
  101. }
  102. ?>
  103. EOF;
  104. $dir = $this->dir . '/' . $this->srcDir;
  105. WindFolder::mkRecur($dir);
  106. if (!WindFile::write($dir . '/IndexController.php', $content)) return false;
  107. $this->srcDir = $this->_resolveDirName($this->srcDir);
  108. return true;
  109. }
  110. /**
  111. * 生成conf目录及config.php
  112. *
  113. */
  114. protected function generateConf() {
  115. $content = <<<EOD
  116. <?php
  117. return array(
  118. 'web-apps' => array(
  119. '%s' => array(
  120. 'modules' => array(
  121. 'default' => array(
  122. 'controller-path' => '%s',
  123. 'controller-suffix' => '%s',
  124. 'template-dir' => '%s',
  125. 'compile-dir' => '%s',
  126. )
  127. )
  128. )
  129. )
  130. );
  131. EOD;
  132. $alias = strtoupper($this->name) . ':';
  133. $content = sprintf($content,
  134. $this->name,
  135. $alias . $this->srcDir,
  136. 'Controller',
  137. $alias . $this->templateDir,
  138. $alias . $this->dataDir . '.compile'
  139. );
  140. $dir = $this->dir . '/' . $this->confDir;
  141. WindFolder::mkRecur($dir);
  142. $this->confFile = $dir . '/config.php';
  143. if (!WindFile::write($this->confFile, $content)) return false;
  144. return true;
  145. }
  146. /**
  147. * 生成www目录及index.php
  148. *
  149. */
  150. protected function generateWww() {
  151. $content = <<<EOS
  152. <?php
  153. error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING);
  154. define('WIND_DEBUG', 1);
  155. require_once '%s';
  156. Wind::register(realpath('%s'), '%s');
  157. Wind::application('%s', '%s')->run();
  158. EOS;
  159. $dir = $this->dir . '/' . $this->wwwDir;
  160. WindFolder::mkRecur($dir);
  161. $content = sprintf($content,
  162. $this->_resolveRelativePath($dir, Wind::getRealPath('WIND:Wind.php', true)) ,
  163. $this->_resolveRelativePath($dir, $this->dir),
  164. strtoupper($this->name),
  165. $this->name,
  166. $this->_resolveRelativePath($dir, $this->confFile));
  167. if (!WindFile::write($dir . '/index.php', $content)) return false;
  168. return true;
  169. }
  170. /**
  171. * 处理路径
  172. *
  173. * @param string $dir
  174. * @return string
  175. */
  176. private function _resolveDirName($dir) {
  177. return str_replace('/', '.', $dir);
  178. }
  179. /**
  180. * 计算路径1对于路径2的相对路径
  181. *
  182. * @param string $sourcePath
  183. * @param string $targetPath
  184. * @return string
  185. */
  186. private function _resolveRelativePath($sourcePath, $targetPath) {
  187. list($sourcePath, $targetPath) = array(realpath($sourcePath), realpath($targetPath));
  188. $src_paths = explode('/', $sourcePath);
  189. $tgt_paths = explode('/', $targetPath);
  190. $src_count = count($src_paths);
  191. $tgt_count = count($tgt_paths);
  192. $relative_path = '';
  193. //默认把不同点设在最后一个
  194. $break_point = $src_count;
  195. $i = 0;
  196. //计算两个路径不相同的点,然后开始往上数..
  197. for ($i = 0; $i < $src_count; $i++) {
  198. if ($src_paths[$i] == $tgt_paths[$i]) continue;
  199. $relative_path .= '../';
  200. $break_point == $src_count && $break_point = $i;
  201. }
  202. $relative_path || $relative_path = './';
  203. //往上..后,继续算目标路径的接下来的path
  204. for ($i = $break_point; $i < $tgt_count; $i++) {
  205. $relative_path .= $tgt_paths[$i] . '/';
  206. }
  207. return rtrim($relative_path, '/');
  208. }
  209. }
  210. ?>