针对hyperf框架改造----自定刷新表结构注释

目的

  • 命令刷新model表结构注释

图示

<?php

declare(strict_types=1);
namespace AppCommand;

use HyperfCommandAnnotationCommand;
use HyperfCommandCommand as HyperfCommand;
use PsrContainerContainerInterface;

/**
 * @Command
 */
class ModelRefreshCommand extends HyperfCommand
{
    /**
     * @var ContainerInterface
     */
    protected $container;

    /**
     * 执行的命令行.
     *
     * @var string
     */
    protected $name = 'model:refresh';

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
        parent::__construct();
    }

    public function configure()
    {
        parent::configure();
        $this->setDescription('项目model初始化');
    }

    public function handle()
    {
        $this->handleDirectoryFile(function ($pathName) {
            $pathInfo = pathinfo($pathName);
            $entity = str_replace('/', '', sprintf('%s%s%s', $pathInfo['dirname'], '/', $pathInfo['filename']));
            $entity = str_replace('src', '', $entity);
            if (class_exists($entity)) {
                $model = new $entity();
                $this->info($model->getModel()->getTable() . 'model开始刷新');
                $this->call('gen:model', [
                    'table' => $model->getModel()->getTable(),
                    '--path' => $pathInfo['dirname'],
                ]);
                $this->info($model->getModel()->getTable() . 'model刷新成功');
            }
        }, 'src', 'Model');
    }

    /**
     * 处理目录文件.
     * @param callable $callback 闭包方法
     * @param string $baseDir 基础目录
     * @param string $needle 需要判断目录的条件
     */
    public function handleDirectoryFile(callable $callback, string $baseDir = 'src', string $needle = ''): void
    {
        $model = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($baseDir));
        foreach ($model as $key => $val) {
            if (! is_file($val->getPathName())) {
                continue;
            }
            if (((! $needle) || (strpos($val->getPathName(), $needle) !== false)) && $callback) {
                $callback($val->getPathName());
            }
        }
    }
}

zhaohao

大家好,欢迎来到赵豪博客!赵豪,94年生人,PHP程序员一枚,因为对PHP开发有着相对比较浓厚的兴趣,所以现在从事着PHP程序员的工作。 今天再次开通这个博客,这里将记录我的职业生涯的点点滴滴,感谢来访与关注!如果我的博客能给您带来一些帮助那真是一件非常荣幸的事情~

相关推荐

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

微信扫一扫

微信扫一扫

微信扫一扫,分享到朋友圈

针对hyperf框架改造----自定刷新表结构注释
返回顶部

显示

忘记密码?

显示

显示

获取验证码

Close