|
| 1 | +<?php |
| 2 | +namespace app\common\command; |
| 3 | + |
| 4 | +use think\console\Command; |
| 5 | +use think\console\Input; |
| 6 | +use think\console\input\Option; |
| 7 | +use think\console\Output; |
| 8 | +use think\facade\Env; |
| 9 | + |
| 10 | +class AST extends Command |
| 11 | +{ |
| 12 | + protected function configure() |
| 13 | + { |
| 14 | + $this->setName('AST') |
| 15 | + ->addOption('type', null, Option::VALUE_OPTIONAL, "要解析的类型,多个用,隔开,如 m,c\n m -- model, c -- controller") |
| 16 | + ->addOption('scope', null, Option::VALUE_OPTIONAL, "要解析的范围,多个用,隔开,如 c,m\n c -- const, m -- method") |
| 17 | + ->addOption('dryRun', null, Option::VALUE_OPTIONAL, "只执行,不保存") |
| 18 | + ->setDescription('Print AST of file'); |
| 19 | + } |
| 20 | + |
| 21 | + protected function execute(Input $input, Output $output) |
| 22 | + { |
| 23 | + $typeList = ['m', 'c']; |
| 24 | + $scopeList = ['c', 'm']; |
| 25 | + |
| 26 | + $dryRun = false; |
| 27 | + if ($input->hasOption('dryRun')) { |
| 28 | + $dryRun = $input->getOption('dryRun'); |
| 29 | + } |
| 30 | + |
| 31 | + if ($input->hasOption('type')) { |
| 32 | + $typeList = explode(',', $input->getOption('type')); |
| 33 | + } |
| 34 | + |
| 35 | + if ($input->hasOption('scope')) { |
| 36 | + $scopeList = explode(',', $input->getOption('scope')); |
| 37 | + } |
| 38 | + |
| 39 | + $date = date("Ymd_Hi"); |
| 40 | + if (in_array('m', $typeList)) { |
| 41 | + $this->generateTypeAST("./application/common/model/**.php", $scopeList, $dryRun ? false : Env::get('app_path') . "modelAST-{$date}.json"); |
| 42 | + } |
| 43 | + if (in_array('c', $typeList)) { |
| 44 | + $this->generateTypeAST("./application/index/controller/**.php", $scopeList, $dryRun ? false : Env::get('app_path') . "controllerAST-{$date}.json"); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + protected function generateTypeAST($globPattern, $scopeList, $savePath) |
| 49 | + { |
| 50 | + $ASTData = []; |
| 51 | + $generateConstant = in_array('c', $scopeList); |
| 52 | + $generateMethod = in_array('m', $scopeList); |
| 53 | + foreach (glob($globPattern) as $file) { |
| 54 | + $className = str_replace(['./application', '.php', '/'], ['/app', '', '\\'], $file); |
| 55 | + $class = new \ReflectionClass($className); |
| 56 | + if ($generateConstant) { |
| 57 | + $constants = $class->getReflectionConstants(); |
| 58 | + |
| 59 | + foreach ($constants as $constant) { |
| 60 | + if ($constant->class != $class->name) { |
| 61 | + continue; |
| 62 | + } |
| 63 | + if (!in_array($constant->name, ['EVENT_INSERT', 'EVENT_UPDATE', 'EVENT_DELETE'])) { |
| 64 | + $ASTData[$class->name]['constant'][] = [ |
| 65 | + 'name' => $constant->name, |
| 66 | + 'value' => $constant->getValue(), |
| 67 | + 'doc' => str_replace(["/**\n", "* ", '*/'], '', $constant->getDocComment()), |
| 68 | + ]; |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + if ($generateMethod) { |
| 74 | + $methods = $class->getMethods(\ReflectionMethod::IS_PUBLIC); |
| 75 | + |
| 76 | + foreach ($methods as $method) { |
| 77 | + if ($method->class != $class->name) { |
| 78 | + continue; |
| 79 | + } |
| 80 | + $args = []; |
| 81 | + foreach ($method->getParameters() as $arg) { |
| 82 | + $args[] = [ |
| 83 | + 'name' => $arg->name, |
| 84 | + 'default' => $arg->isDefaultValueAvailable() ? $arg->getDefaultValue() : '', |
| 85 | + ]; |
| 86 | + } |
| 87 | + $ASTData[$class->name]['method'][] = [ |
| 88 | + 'name' => $method->name, |
| 89 | + '__toString' => $method->__toString(), |
| 90 | + 'args' => $args, |
| 91 | + 'doc' => str_replace(["/**\n", "* ", '*/'], '', $method->getDocComment()), |
| 92 | + ]; |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + // dump($ASTData); |
| 97 | + if ($savePath) { |
| 98 | + file_put_contents($savePath, json_encode($ASTData, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)); |
| 99 | + } else { |
| 100 | + dump($ASTData); |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments