PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/libs/function/cli.php

https://github.com/monkeycraps/swoole_framework
PHP | 162 lines | 105 code | 4 blank | 53 comment | 25 complexity | c036e1c612dab398f88d319a8da469f3 MD5 | raw file
  1. <?php
  2. /**
  3. * 导入所有controller
  4. * @return unknown_type
  5. */
  6. function import_all_controller($apps_path)
  7. {
  8. $d = dir($apps_path.'/controllers');
  9. if(empty($d))
  10. {
  11. return false;
  12. }
  13. while($file = $d->read())
  14. {
  15. $name = basename($file,'.php');
  16. //不符合命名规则
  17. if(!preg_match('/^[a-z0-9_]+$/i',$name)) continue;
  18. //首字母大写的controller为基类控制器,不直接提供响应
  19. if(ord($name{0})>64 and ord($name{0})<91) continue;
  20. $path = $d->path.'/'.$file;
  21. import_controller($name,$path);
  22. }
  23. $d->close();
  24. }
  25. function import_controller($name, $path)
  26. {
  27. global $php;
  28. require($path);
  29. $php->env['controllers'][$name] = array('path'=>$path,'time'=>time());
  30. }
  31. /**
  32. * 检查是否加载了某个扩展
  33. * @param $ext_name
  34. * @return unknown_type
  35. */
  36. function require_ext($ext_name)
  37. {
  38. if(extension_loaded($ext_name)) return true;
  39. else return new Error('require php extension <b>'.$ext_name.'</b>');
  40. }
  41. /**
  42. * 导入所有model
  43. * @return unknown_type
  44. */
  45. function import_all_model()
  46. {
  47. global $php;
  48. $d = dir(APPSPATH.'/models');
  49. while($file=$d->read())
  50. {
  51. $name = basename($file,'.model.php');
  52. //不符合命名规则
  53. if(!preg_match('/^[a-z0-9_]+$/i',$name)) continue;
  54. //首字母大写的controller为基类控制器,不直接提供响应
  55. if(ord($name{0})>64 and ord($name{0})<91) continue;
  56. $path = $d->path.'/'.$file;
  57. require($path);
  58. $php->env['controllers'][$name] = $path;
  59. }
  60. $d->close();
  61. }
  62. /**
  63. * 创建控制器类的文件
  64. * @param $name
  65. * @return unknown_type
  66. */
  67. function create_controllerclass($name,$hello=false)
  68. {
  69. $content = "";
  70. $content .= "<?php\n";
  71. $content .= "class {$name} extends Controller\n";
  72. $content .= "{\n";
  73. $content .= " function __construct(\$swoole)\n";
  74. $content .= " {\n";
  75. $content .= " parent::__construct(\$swoole);\n";
  76. $content .= " }\n";
  77. //添加一个hello vie
  78. if($hello)
  79. {
  80. $content .= " function index(\$swoole)\n";
  81. $content .= " {\n";
  82. $content .= " echo 'hello world.This page build by <a href=http://www.swoole.com/>swoole</a>!';\n";
  83. $content .= " }\n";
  84. }
  85. $content .= "}";
  86. file_put_contents(WEBPATH.'/apps/controllers/'.$name.'.php',$content);
  87. }
  88. /**
  89. * 创建模型类的文件
  90. * @param $name
  91. * @return unknown_type
  92. */
  93. function create_modelclass($name,$table='')
  94. {
  95. $content = "";
  96. $content .= "<?php\n";
  97. $content .= "class {$name} extends Model\n";
  98. $content .= "{\n";
  99. $content .= " //Here write Database table's name\n";
  100. $content .= " var \$table = '{$table}';\n";
  101. $content .= "}";
  102. file_put_contents(WEBPATH.'/apps/models/'.$name.'.model.php',$content);
  103. }
  104. /**
  105. * 创建必需的目录
  106. * @return unknown_type
  107. */
  108. function create_require_dir()
  109. {
  110. /**
  111. * 建立MVC目录
  112. */
  113. if(!is_dir(WEBPATH.'/apps')) mkdir(WEBPATH.'/apps',0755);
  114. if(!is_dir(WEBPATH.'/apps/controllers')) mkdir(WEBPATH.'/apps/controllers',0755);
  115. if(!is_dir(WEBPATH.'/apps/models')) mkdir(WEBPATH.'/apps/models',0755);
  116. /**
  117. * 建立缓存的目录
  118. */
  119. if(!is_dir(WEBPATH.'/cache')) mkdir(WEBPATH.'/cache',0755);
  120. if(!is_dir(WEBPATH.'/cache/pages_c')) mkdir(WEBPATH.'/cache/pages_c',0777);
  121. if(!is_dir(WEBPATH.'/cache/templates_c')) mkdir(WEBPATH.'/cache/templates_c',0777);
  122. if(!is_dir(WEBPATH.'/cache/filecache')) mkdir(WEBPATH.'/cache/filecache',0777);
  123. /**
  124. * Smarty的模板目录
  125. */
  126. if(!is_dir(WEBPATH.'/templates')) mkdir(WEBPATH.'/templates',0755);
  127. /**
  128. * 建立静态文件的目录
  129. */
  130. if(!is_dir(WEBPATH.'/static')) mkdir(WEBPATH.'/static',0755);
  131. if(!is_dir(WEBPATH.'/static/images')) mkdir(WEBPATH.'/static/images',0755);
  132. if(!is_dir(WEBPATH.'/static/css')) mkdir(WEBPATH.'/static/css',0755);
  133. if(!is_dir(WEBPATH.'/static/uploads')) mkdir(WEBPATH.'/static/uploads',0755);
  134. if(!is_dir(WEBPATH.'/static/js')) mkdir(WEBPATH.'/static/js',0755);
  135. /**
  136. * 建立外部扩展类目录
  137. */
  138. if(!is_dir(WEBPATH.'/class')) mkdir(WEBPATH.'/class',0755);
  139. /**
  140. * 建立网站字典目录
  141. */
  142. if(!is_dir(WEBPATH.'/dict')) mkdir(WEBPATH.'/dict',0755);
  143. /**
  144. * 建立Swoole插件系统目录
  145. */
  146. if(!is_dir(WEBPATH.'/swoole_plugin')) mkdir(WEBPATH.'/swoole_plugin',0755);
  147. }
  148. function url_route_default($url_path)
  149. {
  150. $mvc = array('controller'=>'page','view'=>'index');
  151. if(!empty($url_path))
  152. {
  153. $request = explode('/',$url_path,2);
  154. $mvc['controller']=$request[0];
  155. $mvc['view']=$request[0];
  156. }
  157. return $mvc;
  158. }