Skip to content

Commit

Permalink
属性注释转下划线
Browse files Browse the repository at this point in the history
  • Loading branch information
yunwuxin committed Mar 18, 2020
1 parent ee6a792 commit 27c603a
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions src/InteractsWithModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace think\annotation;

use Doctrine\Common\Annotations\Annotation;
use Doctrine\Common\Annotations\Reader;
use ReflectionClass;
use think\annotation\model\relation\BelongsTo;
Expand All @@ -16,6 +17,7 @@
use think\annotation\model\relation\MorphTo;
use think\annotation\model\relation\MorphToMany;
use think\App;
use think\helper\Str;
use think\ide\ModelGenerator;
use think\Model;
use think\model\Collection;
Expand Down Expand Up @@ -159,50 +161,52 @@ protected function detectModelAnnotations()

$this->app->event->listen(ModelGenerator::class, function (ModelGenerator $generator) {

/** @var Annotation[] $annotations */
$annotations = $this->reader->getClassAnnotations($generator->getReflection());

foreach ($annotations as $annotation) {
$property = Str::snake($annotation->value);
switch (true) {
case $annotation instanceof HasOne:
$generator->addMethod($annotation->value, \think\model\relation\HasOne::class, [], '');
$generator->addProperty($annotation->value, $annotation->model, true);
$generator->addProperty($property, $annotation->model, true);
break;
case $annotation instanceof BelongsTo:
$generator->addMethod($annotation->value, \think\model\relation\BelongsTo::class, [], '');
$generator->addProperty($annotation->value, $annotation->model, true);
$generator->addProperty($property, $annotation->model, true);
break;
case $annotation instanceof HasMany:
$generator->addMethod($annotation->value, \think\model\relation\HasMany::class, [], '');
$generator->addProperty($annotation->value, $annotation->model . '[]', true);
$generator->addProperty($property, $annotation->model . '[]', true);
break;
case $annotation instanceof HasManyThrough:
$generator->addMethod($annotation->value, \think\model\relation\HasManyThrough::class, [], '');
$generator->addProperty($annotation->value, $annotation->model . '[]', true);
$generator->addProperty($property, $annotation->model . '[]', true);
break;
case $annotation instanceof HasOneThrough:
$generator->addMethod($annotation->value, \think\model\relation\HasOneThrough::class, [], '');
$generator->addProperty($annotation->value, $annotation->model, true);
$generator->addProperty($property, $annotation->model, true);
break;
case $annotation instanceof BelongsToMany:
$generator->addMethod($annotation->value, \think\model\relation\BelongsToMany::class, [], '');
$generator->addProperty($annotation->value, $annotation->model . '[]', true);
$generator->addProperty($property, $annotation->model . '[]', true);
break;
case $annotation instanceof MorphOne:
$generator->addMethod($annotation->value, \think\model\relation\MorphOne::class, [], '');
$generator->addProperty($annotation->value, $annotation->model, true);
$generator->addProperty($property, $annotation->model, true);
break;
case $annotation instanceof MorphMany:
$generator->addMethod($annotation->value, \think\model\relation\MorphMany::class, [], '');
$generator->addProperty($annotation->value, 'mixed', true);
$generator->addProperty($property, 'mixed', true);
break;
case $annotation instanceof MorphTo:
$generator->addMethod($annotation->value, \think\model\relation\MorphTo::class, [], '');
$generator->addProperty($annotation->value, 'mixed', true);
$generator->addProperty($property, 'mixed', true);
break;
case $annotation instanceof MorphToMany:
case $annotation instanceof MorphByMany:
$generator->addMethod($annotation->value, \think\model\relation\MorphToMany::class, [], '');
$generator->addProperty($annotation->value, Collection::class, true);
$generator->addProperty($property, Collection::class, true);
break;
}
}
Expand Down

0 comments on commit 27c603a

Please sign in to comment.