ModelRewriteTimestampsVisitor.php 3.52 KB
Newer Older
冯超鹏's avatar
冯超鹏 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace Hyperf\Database\Commands\Ast;

use Hyperf\Database\Commands\ModelData;
use Hyperf\Database\Commands\ModelOption;
use Hyperf\Database\Model\Model;
use Hyperf\Utils\Collection;
use PhpParser\Node;
use PhpParser\NodeTraverser;

class ModelRewriteTimestampsVisitor extends AbstractVisitor
{
    /**
     * @var Model
     */
    protected $class;

    /**
     * @var bool
     */
    protected $hasTimestamps = false;

    public function __construct(ModelOption $option, ModelData $data)
    {
        parent::__construct($option, $data);

        $class = $data->getClass();
        $this->class = new $class();
    }

    public function leaveNode(Node $node)
    {
        switch ($node) {
            case $node instanceof Node\Stmt\Property:
                if ($node->props[0]->name->toLowerString() === 'timestamps') {
                    $this->hasTimestamps = true;
                    if (! ($node = $this->rewriteTimestamps($node))) {
                        return NodeTraverser::REMOVE_NODE;
                    }
                }
                return $node;
        }
    }

    public function afterTraverse(array $nodes)
    {
        if ($this->hasTimestamps || $this->shouldRemovedTimestamps()) {
            return null;
        }

        foreach ($nodes as $namespace) {
            if (! $namespace instanceof Node\Stmt\Namespace_) {
                continue;
            }

            foreach ($namespace->stmts as $class) {
                if (! $class instanceof Node\Stmt\Class_) {
                    continue;
                }

                foreach ($class->stmts as $key => $node) {
                    if (isset($node->props, $node->props[0], $node->props[0]->name)
                        && $node->props[0]->name->toLowerString() === 'table') {
                        $newNode = $this->rewriteTimestamps();
                        array_splice($class->stmts, $key, 0, [$newNode]);
                        return null;
                    }
                }
            }
        }
    }

    protected function rewriteTimestamps(?Node\Stmt\Property $node = null): ?Node\Stmt\Property
    {
        if ($this->shouldRemovedTimestamps()) {
            return null;
        }

        $timestamps = $this->usesTimestamps() ? 'true' : 'false';
        $expr = new Node\Expr\ConstFetch(new Node\Name($timestamps));
        if ($node) {
            $node->props[0]->default = $expr;
        } else {
            $prop = new Node\Stmt\PropertyProperty('timestamps', $expr);
            $node = new Node\Stmt\Property(Node\Stmt\Class_::MODIFIER_PUBLIC, [$prop]);
        }

        return $node;
    }

    protected function usesTimestamps(): bool
    {
        $createdAt = $this->class->getCreatedAtColumn();
        $updatedAt = $this->class->getUpdatedAtColumn();
        $columns = Collection::make($this->data->getColumns());

        return $columns->where('column_name', $createdAt)->count() && $columns->where('column_name', $updatedAt)->count();
    }

    protected function shouldRemovedTimestamps(): bool
    {
        $useTimestamps = $this->usesTimestamps();
        $ref = new \ReflectionClass(get_class($this->class));

        if (! $ref->getParentClass()) {
            return false;
        }

        return $useTimestamps == ($ref->getParentClass()->getDefaultProperties()['timestamps'] ?? null);
    }
}