dump.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. <?php
  2. include "vendor/autoload.php";
  3. // 调试输出
  4. function vd($msg)
  5. {
  6. print_r($msg);
  7. echo PHP_EOL;
  8. }
  9. function vde($msg)
  10. {
  11. vd($msg);
  12. exit;
  13. }
  14. // 解析参数
  15. function explainArgv($argv)
  16. {
  17. $args = [];
  18. foreach ($argv as $value) {
  19. list($k, $v) = explode('=', $value);
  20. $k = ltrim(ltrim(trim($k), '--'), '-');
  21. $v = trim($v);
  22. $args[$k] = $v;
  23. }
  24. return $args;
  25. }
  26. function usage()
  27. {
  28. echo '使用示例:' . PHP_EOL;
  29. echo 'php ./dump.php -app=test' . PHP_EOL;
  30. // echo 'php ./dump.php -app=test -split=true' . PHP_EOL;
  31. }
  32. class Dump
  33. {
  34. // 文件夹路径符
  35. private static $DS = '/';
  36. /**
  37. * @var string
  38. */
  39. private $appName;
  40. /**
  41. * 是否需要分子目录
  42. *
  43. * @var boolean
  44. */
  45. private $split = false;
  46. /**
  47. * 源文件目录
  48. */
  49. private $sourceDir;
  50. /**
  51. * 脚本目录
  52. */
  53. private $scriptDir;
  54. /**
  55. * 导出文件目录
  56. */
  57. private $outputDir;
  58. /**
  59. * 生成json目录
  60. */
  61. private $jsonDir;
  62. /**
  63. * 生成model目录
  64. */
  65. private $modelDir;
  66. /**
  67. * 默认配置地址
  68. */
  69. private $defaultConfigFile;
  70. /**
  71. * Dump constructor.
  72. *
  73. * @param array $args
  74. */
  75. public function __construct($args)
  76. {
  77. if (!isset($args['app'])) {
  78. vd('参数错误');
  79. usage();
  80. exit;
  81. }
  82. // 初始化导出参数
  83. $this->appName = $args['app'];
  84. if (isset($args['split'])) {
  85. $this->split = $args['split'];
  86. }
  87. $this->sourceDir = __DIR__ . self::$DS . 'projects' . self::$DS . $this->appName . self::$DS . 'source';
  88. $this->scriptDir = __DIR__ . self::$DS . 'projects' . self::$DS . $this->appName . self::$DS . 'script';
  89. $this->outputDir = __DIR__ . self::$DS . 'projects' . self::$DS . $this->appName . self::$DS . 'output';
  90. $this->jsonDir = __DIR__ . self::$DS . 'projects' . self::$DS . $this->appName . self::$DS . 'json';
  91. $this->modelDir = __DIR__ . self::$DS . 'projects' . self::$DS . $this->appName . self::$DS . 'model';
  92. // 检查目录文件是否缺失
  93. if (!is_dir($this->sourceDir)) {
  94. vde('源文件目录缺失:' . $this->sourceDir);
  95. }
  96. if (!is_dir($this->scriptDir)) {
  97. vde('脚本目录缺失:' . $this->scriptDir);
  98. }
  99. if (!is_dir($this->outputDir)) {
  100. vde('输出配置目录缺失:' . $this->outputDir);
  101. }
  102. if (!is_dir($this->jsonDir)) {
  103. vde('输出json目录缺失:' . $this->jsonDir);
  104. }
  105. if (!is_dir($this->modelDir)) {
  106. vde('输出model目录缺失:' . $this->modelDir);
  107. }
  108. // 初始化默认配置文件
  109. $this->defaultConfigFile = __DIR__ . self::$DS . 'default-script.php';
  110. }
  111. /**
  112. * 执行主函数
  113. */
  114. public function run()
  115. {
  116. // 遍历源目录, 找出所有可执行的文件
  117. $files = [];
  118. $sourceDir = dir($this->sourceDir);
  119. while ($file = $sourceDir->read()) {
  120. if ($file == '.' || $file == '..') {
  121. continue;
  122. }
  123. $pathInfo = pathinfo($file);
  124. // 现在仅支持xlsx文件
  125. if ($pathInfo['extension'] != 'xlsx') {
  126. continue;
  127. }
  128. $files[] = $pathInfo;
  129. }
  130. $sourceDir->close();
  131. if (empty($files)) {
  132. vde('源目录为空, 未找到.xlsx文件, 源目录:' . $this->sourceDir);
  133. }
  134. // 执行导出
  135. $success = 0;
  136. foreach ($files as $fileInfo) {
  137. if ($this->dump($fileInfo)) {
  138. ++$success;
  139. }
  140. }
  141. vd(sprintf("导出完成,共[%d]个文件, 成功[%d]个文件.", count($files), $success));
  142. }
  143. /**
  144. * 导出表
  145. *
  146. * @param array $fileInfo
  147. * @return bool
  148. */
  149. private function dump($fileInfo)
  150. {
  151. // 找到文件对应的执行脚本
  152. // 如果找不到, 则使用默认的配置
  153. $script = $this->scriptDir . self::$DS . $fileInfo['filename'] . '.php';
  154. if (!file_exists($script)) {
  155. vd(sprintf('[%s]对应的脚本文件不存在, 脚本文件:%s', $fileInfo['basename'], $script));
  156. // return false;
  157. $script = $this->defaultConfigFile;
  158. vd(sprintf('[%s]使用默认配置, 脚本文件:%s', $fileInfo['basename'], $script));
  159. }
  160. // 引入脚本
  161. include $script;
  162. // 读取excel文件内容
  163. $sourceData = $this->getDataFromSource($this->sourceDir . self::$DS . $fileInfo['basename']);
  164. if (count($sourceData) < $dataLine) {
  165. vd(sprintf('[%s]文件没有数据', $fileInfo['basename']));
  166. return false;
  167. }
  168. // 如果未设置字段名列, 那么自动生成, 到处所有列
  169. if (empty($columns)) {
  170. foreach ($sourceData[$columnLine - 1] as $key => $columnName) {
  171. $columnName = trim($columnName);
  172. if (strlen($columnName) == 0) {
  173. continue;
  174. }
  175. $columnType = trim($sourceData[$typeLine - 1][$key]);
  176. $columns[$columnName] = $columnType;
  177. }
  178. }
  179. // 处理导出列对应的索引
  180. $indexes = [];
  181. // vd($sourceData[$columnLine - 1]);
  182. foreach ($sourceData[$columnLine - 1] as $index => $columnName) {
  183. $columnName = trim($columnName);
  184. if (isset($columns[$columnName])) {
  185. $indexes[$columnName] = $index;
  186. }
  187. }
  188. // 普通处理逻辑
  189. $data = [];
  190. foreach ($sourceData as $k => $v) {
  191. // 非数据列不处理
  192. if ($k < $dataLine - 1) {
  193. continue;
  194. }
  195. $row = [];
  196. foreach ($columns as $columnName => $columnType) {
  197. $columnValue = trim($v[$indexes[$columnName]]);
  198. switch ($columnType) {
  199. case 'float':
  200. $columnValue = floatval($columnValue);
  201. break;
  202. case 'number':
  203. $columnValue = intval($columnValue);
  204. break;
  205. case 'json':
  206. $columnValue = json_decode($columnValue, true);
  207. break;
  208. default:
  209. $columnValue = strval($columnValue);
  210. break;
  211. }
  212. $row[$columnName] = $columnValue;
  213. }
  214. // 跳过空行
  215. if (!$row[$keyColumn]) {
  216. continue;
  217. }
  218. $data[$row[$keyColumn]] = $row;
  219. }
  220. // 额外处理逻辑
  221. $handle = $fileInfo['filename'] . '_handle';
  222. if (function_exists($handle)) {
  223. $data = $handle($data);
  224. }
  225. // 写入PHP文件
  226. $this->writePHP($fileInfo['filename'], $data);
  227. // 生成json文件
  228. $this->writeJSON($fileInfo['filename'], $data);
  229. // 生成model文件
  230. $this->writeModel($fileInfo['filename'], $clientColumns);
  231. return true;
  232. }
  233. /**
  234. * @param string $file 源文件地址
  235. * @return array
  236. */
  237. private function getDataFromSource($file)
  238. {
  239. $reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
  240. // $reader->setReadDataOnly(['Sheet 1']);
  241. $reader->setReadDataOnly(true);
  242. $spreadsheet = $reader->load($file);
  243. // $data = $spreadsheet->getActiveSheet()->toArray();
  244. $data = $spreadsheet->getSheet(5)->toArray();
  245. vde($data);
  246. return $data;
  247. }
  248. /**
  249. * 写入php文件
  250. *
  251. * @param string $filename 写入文件路径
  252. * @param array $data 写入文件内容
  253. * @return void
  254. */
  255. private function writePHP($filename, $data)
  256. {
  257. $file = $this->outputDir . self::$DS . $filename . '.config.php';
  258. // 写入配置文件
  259. $configs = var_export($data, true);
  260. // 整理输出格式
  261. $config = <<<TXT
  262. <?php
  263. return {$configs};
  264. TXT;
  265. // 写入导出文件
  266. file_put_contents($file, $config);
  267. }
  268. /**
  269. * 生成json文件
  270. *
  271. * @param string $filename 写入文件名称
  272. * @param array $data 待写入的文件内容
  273. */
  274. private function writeJSON($filename, $data)
  275. {
  276. $file = $this->jsonDir . self::$DS . $filename . '.json';
  277. // 写入导出文件
  278. file_put_contents($file, json_encode($data, JSON_UNESCAPED_UNICODE));
  279. }
  280. /**
  281. * 写入php model 文件
  282. *
  283. * @param string $filename 写入文件路径
  284. * @param array $clientColumns 输出给客户端的字段列表
  285. * @return void
  286. */
  287. private function writeModel($filename, $clientColumns)
  288. {
  289. $columnString = "[]";
  290. if (count($clientColumns) > 0) {
  291. $columnString = '["' . join('", "', $clientColumns) . '"]';
  292. }
  293. $className = ucwords($filename);
  294. $file = $this->modelDir . self::$DS . $className . '.php';
  295. // 整理输出格式
  296. $config = <<<TXT
  297. <?php
  298. class {$className}
  299. {
  300. /**
  301. * Returns table name mapped in the model.
  302. *
  303. * @return string
  304. */
  305. public static function getSource()
  306. {
  307. return '{$filename}';
  308. }
  309. /**
  310. * 获取导出给客户端所需要的字段
  311. *
  312. * @return array
  313. */
  314. public static function getToClientColumns()
  315. {
  316. return {$columnString};
  317. }
  318. /**
  319. * 生成客户端需要的字段, 生成列表
  320. *
  321. * @return array
  322. */
  323. public static function getListForClient()
  324. {
  325. \$data = [];
  326. if (empty(self::getToClientColumns())) {
  327. \$data = array_values(self::getList());
  328. } else {
  329. foreach (self::getList() as \$key => \$value) {
  330. foreach (\$value as \$k => \$v) {
  331. if (!in_array(\$k, self::getToClientColumns())) {
  332. unset(\$value[\$k]);
  333. }
  334. }
  335. \$data[] = \$value;
  336. }
  337. }
  338. return \$data;
  339. }
  340. /**
  341. * 获取单条配置内容
  342. *
  343. * @return array
  344. */
  345. public static function get(\$configId)
  346. {
  347. return IConfig::get()->loadAppConfig(self::getSource(), \$configId);
  348. }
  349. /**
  350. * 获取所有配置
  351. *
  352. * @return array
  353. */
  354. public static function getList()
  355. {
  356. return IConfig::get()->loadAppConfig(self::getSource());
  357. }
  358. }
  359. TXT;
  360. // 写入导出文件
  361. file_put_contents($file, $config);
  362. }
  363. }
  364. /*
  365. * 使用说明
  366. * php ./dump.php -app=shot -split=false
  367. */
  368. // 读取 & 解析命令行参数
  369. array_shift($argv);
  370. $args = explainArgv($argv);
  371. // 执行导出
  372. $dump = new Dump($args);
  373. $dump->run();