Stub.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php
  2. namespace app\admin\command\Crud\library;
  3. class Stub
  4. {
  5. protected static $instance;
  6. protected $stubList = [];
  7. protected $options = [
  8. // 转义Html
  9. 'escapeHtml' => false
  10. ];
  11. // 生成FormItem组件的输入框类型
  12. protected static $formItemType = [
  13. 'string',
  14. 'number',
  15. 'radio',
  16. 'checkbox',
  17. 'switch',
  18. 'textarea',
  19. 'array',
  20. 'datetime',
  21. 'year',
  22. 'date',
  23. 'time',
  24. 'select',
  25. 'selects',
  26. 'remoteSelect',
  27. 'remoteSelects',
  28. 'editor',
  29. 'city',
  30. 'image',
  31. 'images',
  32. 'file',
  33. 'files',
  34. 'icon',
  35. ];
  36. /**
  37. * 获取单例
  38. * @param array $options
  39. * @return static
  40. */
  41. public static function instance(array $options = []): Stub
  42. {
  43. if (is_null(self::$instance)) {
  44. self::$instance = new static($options);
  45. }
  46. return self::$instance;
  47. }
  48. /**
  49. * @param array $options
  50. */
  51. public function __construct(array $options = [])
  52. {
  53. $this->options = array_merge($this->options, $options);
  54. }
  55. /**
  56. * 获取替换后的数据.
  57. * @param string $name
  58. * @param array $data
  59. * @return string
  60. */
  61. public function getReplacedStub(string $name, array $data): string
  62. {
  63. foreach ($data as $index => &$datum) {
  64. $datum = is_array($datum) ? '' : $datum;
  65. }
  66. unset($datum);
  67. $search = $replace = [];
  68. foreach ($data as $k => $v) {
  69. $search[] = "{%{$k}%}";
  70. $replace[] = $v;
  71. }
  72. $stubname = $this->getStub($name);
  73. if (isset($this->stubList[$stubname])) {
  74. $stub = $this->stubList[$stubname];
  75. } else {
  76. $this->stubList[$stubname] = $stub = file_get_contents($stubname);
  77. }
  78. $content = str_replace($search, $replace, $stub);
  79. return $this->escape($content);
  80. }
  81. /**
  82. * 获取基础模板
  83. * @param string $name
  84. * @return string
  85. */
  86. public function getStub(string $name): string
  87. {
  88. return app_path() . 'admin' . DIRECTORY_SEPARATOR . 'command' . DIRECTORY_SEPARATOR . 'Crud' . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR . $name . '.stub';
  89. }
  90. public static function buildModelFieldType($modelFieldType)
  91. {
  92. if (!$modelFieldType) return '';
  93. $maxStrLang = 0;
  94. foreach ($modelFieldType as $key => $item) {
  95. $strLang = strlen($key);
  96. $maxStrLang = max($strLang, $maxStrLang);
  97. }
  98. $str = "";
  99. foreach ($modelFieldType as $key => $item) {
  100. $str .= self::tab(2) . "'{$key}'" . str_pad('=>', ($maxStrLang - strlen($key) + 3), ' ', STR_PAD_LEFT) . " '{$item}',\n";
  101. }
  102. return "\n" . self::tab() . "protected \$type = [\n" . rtrim($str, "\n") . "\n" . self::tab() . "];";
  103. }
  104. public static function buildTableColumnKey($key, $item)
  105. {
  106. if (is_array($item)) {
  107. $itemJson = ' ' . $key . ': {';
  108. foreach ($item as $ik => $iitem) {
  109. $itemJson .= self::buildTableColumnKey($ik, $iitem);
  110. }
  111. $itemJson = rtrim($itemJson, ',');
  112. $itemJson .= ' }';
  113. } else {
  114. if ($item === 'false') {
  115. $itemJson = ' ' . $key . ': false,';
  116. } elseif (in_array($key, ['label', 'width', 'buttons']) || strpos($item, "t('") === 0 || strpos($item, "t(\"") === 0) {
  117. $itemJson = ' ' . $key . ': ' . $item . ',';
  118. } else {
  119. $itemJson = ' ' . $key . ': \'' . $item . '\',';
  120. }
  121. }
  122. return $itemJson;
  123. }
  124. public static function buildTableColumn(&$tableColumnList)
  125. {
  126. $columnJson = '';
  127. foreach ($tableColumnList as $column) {
  128. $columnJson .= ' {';
  129. foreach ($column as $key => $item) {
  130. $columnJson .= self::buildTableColumnKey($key, $item);
  131. }
  132. $columnJson = rtrim($columnJson, ',');
  133. $columnJson .= ' }' . ",\n";
  134. }
  135. $tableColumnList = rtrim($columnJson, "\n");
  136. }
  137. public static function buildFormField(&$formFieldList)
  138. {
  139. $fieldHtml = "\n";
  140. foreach ($formFieldList as $item) {
  141. if (in_array($item['type'], self::$formItemType)) {
  142. // FormItem
  143. $fieldHtml .= self::tab(4) . "<FormItem";
  144. foreach ($item as $key => $attr) {
  145. if (is_array($attr)) {
  146. $fieldHtml .= ' ' . $key . '="' . self::getJsonFromArray($attr) . '"';
  147. } else {
  148. $fieldHtml .= ' ' . $key . '="' . $attr . '"';
  149. }
  150. }
  151. $fieldHtml .= " />\n";
  152. }
  153. }
  154. $formFieldList = rtrim($fieldHtml, "\n");
  155. }
  156. public static function buildFormRules(&$formItemRules)
  157. {
  158. $rulesHtml = "";
  159. foreach ($formItemRules as $key => $formItemRule) {
  160. $rulesArrHtml = '';
  161. foreach ($formItemRule as $item) {
  162. $rulesArrHtml .= $item . ', ';
  163. }
  164. $rulesHtml .= self::tab() . $key . ': [' . rtrim($rulesArrHtml, ', ') . "],\n";
  165. }
  166. $formItemRules = $rulesHtml ? "\n" . $rulesHtml : '';
  167. }
  168. public static function getJsonFromArray($array)
  169. {
  170. if (is_array($array)) {
  171. $jsonStr = '';
  172. foreach ($array as $key => $item) {
  173. $keyStr = strpos($key, "-") === false ? ' ' . $key . ': ' : ' \'' . $key . '\': ';
  174. if (is_array($item)) {
  175. $jsonStr .= $keyStr . self::getJsonFromArray($item) . ',';
  176. } elseif ($item === 'false' || $item === 'true') {
  177. $jsonStr .= $keyStr . ($item === 'false' ? 'false' : 'true') . ',';
  178. } elseif (strpos($item, "t('") === 0 || strpos($item, "t(\"") === 0) {
  179. $jsonStr .= $keyStr . $item . ',';
  180. } elseif (($key == 'remote-url' && strpos($item, "+") !== false) || $key == 'rows') {
  181. $jsonStr .= $keyStr . $item . ',';
  182. } else {
  183. $jsonStr .= $keyStr . '\'' . $item . '\',';
  184. }
  185. }
  186. return '{' . rtrim($jsonStr, ',') . ' }';
  187. } else {
  188. return $array;
  189. }
  190. }
  191. public static function buildDblClickNotEditColumn(&$dblClickNotEditColumn)
  192. {
  193. $columnJson = '';
  194. foreach ($dblClickNotEditColumn as $item) {
  195. if ($item === 'undefined') {
  196. $columnJson .= $item . ', ';
  197. } else {
  198. $columnJson .= '\'' . $item . '\',';
  199. }
  200. }
  201. $dblClickNotEditColumn = '[' . rtrim($columnJson, ',') . ']';
  202. }
  203. public static function buildDefaultOrder($defaultOrder)
  204. {
  205. if (isset($defaultOrder[0]) && isset($defaultOrder[1])) {
  206. $defaultOrderStub = [
  207. 'prop' => $defaultOrder[0],
  208. 'order' => $defaultOrder[1],
  209. ];
  210. $defaultOrderStub = self::getJsonFromArray($defaultOrderStub);
  211. if ($defaultOrderStub) {
  212. return "\n" . self::tab(2) . "defaultOrder: " . $defaultOrderStub . ',';
  213. }
  214. }
  215. return '';
  216. }
  217. public static function writeToFile($pathname, $content)
  218. {
  219. if (!is_dir(dirname($pathname))) {
  220. mkdir(dirname($pathname), 0755, true);
  221. }
  222. return file_put_contents($pathname, $content);
  223. }
  224. public static function writeWebLangFile($langList, $webLangEnFile, $webLangZhCnFile)
  225. {
  226. // 英文语言包写入
  227. if (isset($langList['en']) && $langList['en']) {
  228. $enLangTs = '';
  229. foreach ($langList['en'] as $key => $item) {
  230. $enLangTs .= self::tab() . '"' . $key . '": "' . $item . "\",\n";
  231. }
  232. $enLangTs = "export default {\n" . $enLangTs . "}";
  233. self::writeToFile($webLangEnFile, $enLangTs);
  234. }
  235. // 中文语言包写入
  236. if (isset($langList['zh-cn']) && $langList['zh-cn']) {
  237. $zhCnLangTs = '';
  238. foreach ($langList['zh-cn'] as $key => $item) {
  239. $zhCnLangTs .= self::tab() . '"' . $key . '": "' . $item . "\",\n";
  240. }
  241. $zhCnLangTs = "export default {\n" . $zhCnLangTs . "}";
  242. self::writeToFile($webLangZhCnFile, $zhCnLangTs);
  243. }
  244. }
  245. /**
  246. * 设置是否转义
  247. * @param boolean $escape
  248. */
  249. public function setEscapeHtml(bool $escape)
  250. {
  251. $this->options['escapeHtml'] = $escape;
  252. }
  253. /**
  254. * 获取转义编码后的值
  255. * @param string|array $value
  256. * @return string
  257. */
  258. public function escape($value): string
  259. {
  260. if (!$this->options['escapeHtml']) {
  261. return $value;
  262. }
  263. if (is_array($value)) {
  264. $value = json_encode($value, JSON_UNESCAPED_UNICODE);
  265. }
  266. return htmlspecialchars($value, ENT_QUOTES, 'UTF-8', false);
  267. }
  268. public static function tab(int $num = 1): string
  269. {
  270. return str_pad('', 4 * $num);
  271. }
  272. }