前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >针对hyperf框架改造----自定刷新表结构注释

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

作者头像
美团骑手
发布2021-12-24 18:38:18
3970
发布2021-12-24 18:38:18
举报
文章被收录于专栏:技术进阶

目的

  • 命令刷新model表结构注释

图示

代码语言:javascript
复制
<?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());
            }
        }
    }
}

PHP

Copy

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-06-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 目的
  • 图示
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档