Skip to content

Commit 3514cd7

Browse files
committed
完善
1 parent f63b9da commit 3514cd7

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
## ThinkPHP6 自动生成用于IDE提示的注释
2+
3+
### 安装
4+
5+
~~~
6+
composer require topthink/think-ide-helper
7+
~~~
8+
9+
### 1.模型注释
10+
11+
~~~
12+
//所有模型
13+
php think ide-helper:model
14+
15+
//指定模型
16+
php think ide-helper:model app\\model\\User app\\model\\Post
17+
~~~
18+
19+
#### 可选参数
20+
~~~
21+
--dir="models" [-D] 指定自动搜索模型的目录,相对于应用基础目录的路径,可指定多个,默认为app/model
22+
23+
--ignore="app\\model\\User,app\\model\\Post" [-I] 忽略的模型,可指定多个
24+
25+
--overwrite [-O] 强制覆盖已有的属性注释
26+
27+
--reset [-R] 重置模型的所有的注释
28+
~~~

src/ModelGenerator.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,18 @@
2424
use think\helper\Arr;
2525
use think\helper\Str;
2626
use think\Model;
27+
use think\model\Collection;
2728
use think\model\Relation;
2829
use think\model\relation\BelongsTo;
2930
use think\model\relation\BelongsToMany;
3031
use think\model\relation\HasMany;
3132
use think\model\relation\HasManyThrough;
3233
use think\model\relation\HasOne;
34+
use think\model\relation\HasOneThrough;
3335
use think\model\relation\MorphMany;
3436
use think\model\relation\MorphOne;
3537
use think\model\relation\MorphTo;
38+
use think\model\relation\MorphToMany;
3639
use Throwable;
3740

3841
class ModelGenerator
@@ -98,12 +101,12 @@ public function addProperty($name, $type = null, $read = null, $write = null, $c
98101
}
99102
}
100103

101-
public function addMethod($name, $return = 'mixed', $arguments = [], $type = 'static')
104+
public function addMethod($name, $return = 'mixed', $arguments = [], $static = true)
102105
{
103106
$methods = array_change_key_case($this->methods, CASE_LOWER);
104107
if (!isset($methods[strtolower($name)])) {
105108
$this->methods[$name] = [];
106-
$this->methods[$name]['type'] = $type;
109+
$this->methods[$name]['static'] = $static ? 'static' : '';
107110
$this->methods[$name]['arguments'] = $arguments;
108111
$this->methods[$name]['return'] = $return;
109112
}
@@ -129,7 +132,7 @@ public function generate()
129132
return;
130133
}
131134

132-
$this->model = new ($this->class);
135+
$this->model = new $this->class;
133136

134137
$this->getPropertiesFromTable();
135138
$this->getPropertiesFromMethods();
@@ -307,7 +310,7 @@ protected function getPropertiesFromMethods()
307310
if ($return instanceof Relation) {
308311

309312
$name = Str::snake($methodName);
310-
if ($return instanceof HasOne || $return instanceof BelongsTo || $return instanceof MorphOne) {
313+
if ($return instanceof HasOne || $return instanceof BelongsTo || $return instanceof MorphOne || $return instanceof HasOneThrough) {
311314
$this->addProperty($name, "\\" . get_class($return->getModel()), true, null);
312315
}
313316

@@ -318,6 +321,10 @@ protected function getPropertiesFromMethods()
318321
if ($return instanceof MorphTo || $return instanceof MorphMany) {
319322
$this->addProperty($name, "mixed", true, null);
320323
}
324+
325+
if ($return instanceof MorphToMany) {
326+
$this->addProperty($name, "\\" . Collection::class, true, null);
327+
}
321328
}
322329
} catch (Exception $e) {
323330
} catch (Throwable $e) {
@@ -405,7 +412,7 @@ protected function createPhpDocs()
405412

406413
$arguments = implode(', ', $method['arguments']);
407414

408-
$tags[] = $tagFactory->create("@method {$method['type']} {$method['return']} {$name}({$arguments})", $context);
415+
$tags[] = $tagFactory->create("@method {$method['static']} {$method['return']} {$name}({$arguments})", $context);
409416
}
410417

411418
$tags = $this->sortTags($tags);

0 commit comments

Comments
 (0)