控制台应用,yzncms本身基于tp5.1框架,里面的队列用不了,bug,坑
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Command.php 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Yzncms [ 御宅男工作室 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2018 http://yzncms.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: fastadmin: https://www.fastadmin.net/
  10. // +----------------------------------------------------------------------
  11. // +----------------------------------------------------------------------
  12. // | 在线命令管理
  13. // +----------------------------------------------------------------------
  14. namespace app\admin\controller;
  15. use app\common\controller\Adminbase;
  16. use think\console\Input;
  17. use think\Db;
  18. use think\Exception;
  19. use think\facade\Config;
  20. class Command extends Adminbase
  21. {
  22. /**
  23. * Command模型对象
  24. */
  25. protected $modelClass = null;
  26. protected $noNeedRight = ['get_controller_list', 'get_field_list'];
  27. public function initialize()
  28. {
  29. parent::initialize();
  30. $this->modelClass = new \app\admin\model\Command;
  31. $this->assign("statusList", $this->modelClass->getStatusList());
  32. }
  33. /**
  34. * 添加
  35. */
  36. public function add()
  37. {
  38. $tableList = [];
  39. $list = Db::query("SHOW TABLES");
  40. foreach ($list as $key => $row) {
  41. $tableList[reset($row)] = reset($row);
  42. }
  43. $this->assign("tableList", $tableList);
  44. return $this->fetch();
  45. }
  46. /**
  47. * 获取字段列表
  48. * @internal
  49. */
  50. public function get_field_list()
  51. {
  52. $dbname = Config::get('database.database');
  53. $prefix = Config::get('database.prefix');
  54. $table = $this->request->request('table');
  55. //从数据库中获取表字段信息
  56. $sql = "SELECT * FROM `information_schema`.`columns` "
  57. . "WHERE TABLE_SCHEMA = ? AND table_name = ? "
  58. . "ORDER BY ORDINAL_POSITION";
  59. //加载主表的列
  60. $columnList = Db::query($sql, [$dbname, $table]);
  61. $fieldlist = [];
  62. foreach ($columnList as $index => $item) {
  63. $fieldlist[] = $item['COLUMN_NAME'];
  64. }
  65. $this->success("", null, ['fieldlist' => $fieldlist]);
  66. }
  67. /**
  68. * 获取控制器列表
  69. * @internal
  70. */
  71. public function get_controller_list()
  72. {
  73. //搜索关键词,客户端输入以空格分开,这里接收为数组
  74. $word = (array) $this->request->request("q_word/a");
  75. $word = implode('', $word);
  76. $adminPath = dirname(__DIR__) . DS;
  77. $controllerDir = $adminPath . 'controller' . DS;
  78. $files = new \RecursiveIteratorIterator(
  79. new \RecursiveDirectoryIterator($controllerDir), \RecursiveIteratorIterator::LEAVES_ONLY
  80. );
  81. $list = [];
  82. foreach ($files as $name => $file) {
  83. if (!$file->isDir()) {
  84. $filePath = $file->getRealPath();
  85. $name = str_replace($controllerDir, '', $filePath);
  86. $name = str_replace(DS, "/", $name);
  87. if (!preg_match("/(.*)\.php\$/", $name)) {
  88. continue;
  89. }
  90. if (!$word || stripos($name, $word) !== false) {
  91. $list[] = ['id' => $name, 'name' => $name];
  92. }
  93. }
  94. }
  95. $pageNumber = $this->request->request("pageNumber");
  96. $pageSize = $this->request->request("pageSize");
  97. return json(['data' => array_slice($list, ($pageNumber - 1) * $pageSize, $pageSize), 'count' => count($list)]);
  98. }
  99. /**
  100. * 详情
  101. */
  102. public function detail($ids)
  103. {
  104. $row = $this->modelClass->get($ids);
  105. if (!$row) {
  106. $this->error('记录未找到');
  107. }
  108. $this->assign("row", $row);
  109. return $this->fetch();
  110. }
  111. /**
  112. * 执行
  113. */
  114. public function execute($ids)
  115. {
  116. $row = $this->modelClass->get($ids);
  117. if (!$row) {
  118. $this->error('记录未找到');
  119. }
  120. $result = $this->doexecute($row['type'], json_decode($row['params'], true));
  121. $this->success("Crud成功", null, ['result' => $result]);
  122. }
  123. /**
  124. * 生成命令
  125. */
  126. public function command($action = '')
  127. {
  128. $commandtype = $this->request->request("commandtype");
  129. $params = $this->request->request();
  130. $allowfields = [
  131. 'crud' => 'table,controller,model,fields,force,local,delete,menu',
  132. 'menu' => 'controller,force,delete',
  133. ];
  134. $argv = [];
  135. $allowfields = isset($allowfields[$commandtype]) ? explode(',', $allowfields[$commandtype]) : [];
  136. $allowfields = array_filter(array_intersect_key($params, array_flip($allowfields)));
  137. if (isset($params['local']) && !$params['local']) {
  138. $allowfields['local'] = $params['local'];
  139. } else {
  140. unset($allowfields['local']);
  141. }
  142. foreach ($allowfields as $key => $param) {
  143. $argv[] = "--{$key}=" . (is_array($param) ? implode(',', $param) : $param);
  144. }
  145. if ($commandtype == 'crud') {
  146. $extend = 'setcheckboxsuffix,enumradiosuffix,imagefield,filefield,intdatesuffix,switchsuffix,citysuffix,selectpagesuffix,selectpagessuffix,ignorefields,sortfield,editorsuffix,headingfilterfield,tagsuffix,jsonsuffix,fixedcolumns';
  147. $extendArr = explode(',', $extend);
  148. foreach ($params as $index => $item) {
  149. if (in_array($index, $extendArr)) {
  150. foreach (explode(',', $item) as $key => $value) {
  151. if ($value) {
  152. $argv[] = "--{$index}={$value}";
  153. }
  154. }
  155. }
  156. }
  157. $isrelation = (int) $this->request->request('isrelation');
  158. if ($isrelation && isset($params['relation'])) {
  159. foreach ($params['relation'] as $index => $relation) {
  160. foreach ($relation as $key => $value) {
  161. $value = (is_array($value) ? implode(',', $value) : $value);
  162. $value && $argv[] = "--{$key}=" . $value;
  163. }
  164. }
  165. }
  166. } elseif ($commandtype == 'menu') {
  167. foreach (explode(',', $params['controllerfile']) as $index => $param) {
  168. if ($param) {
  169. $argv[] = "--controller=" . substr($param, 0, -4);
  170. }
  171. }
  172. }
  173. if ($action == 'execute') {
  174. if (Config::get('app_debug')) {
  175. $result = $this->doexecute($commandtype, $argv);
  176. $this->success("", null, ['result' => $result]);
  177. } else {
  178. $this->error("只允许在开发环境下执行命令");
  179. }
  180. } else {
  181. $this->success("", null, ['command' => "php think {$commandtype} " . implode(' ', $argv)]);
  182. }
  183. return;
  184. }
  185. protected function doexecute($commandtype, $argv)
  186. {
  187. if (!Config::get('app_debug')) {
  188. $this->error("只允许在开发环境下执行命令");
  189. }
  190. if (preg_match("/([;\|&]+)/", implode(' ', $argv))) {
  191. $this->error("不支持的命令参数");
  192. }
  193. $commandName = "\\app\\admin\\command\\" . ucfirst($commandtype);
  194. $input = new Input($argv);
  195. $output = new \addons\command\library\Output();
  196. $command = new $commandName($commandtype);
  197. $data = [
  198. 'type' => $commandtype,
  199. 'params' => json_encode($argv),
  200. 'command' => "php think {$commandtype} " . implode(' ', $argv),
  201. 'execute_time' => time(),
  202. ];
  203. $this->modelClass->save($data);
  204. try {
  205. $command->run($input, $output);
  206. $result = implode("\n", $output->getMessage());
  207. $this->modelClass->status = 1;
  208. } catch (Exception $e) {
  209. $result = implode("\n", $output->getMessage()) . "\n";
  210. $result .= $e->getMessage();
  211. $this->modelClass->status = 0;
  212. }
  213. $result = trim($result);
  214. $this->modelClass->content = $result;
  215. $this->modelClass->save();
  216. return $result;
  217. }
  218. }