前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >Owl Admin强制生成代码

Owl Admin强制生成代码

原创
作者头像
用砖头敲代码
发布2024-12-07 16:09:39
发布2024-12-07 16:09:39
790
举报
文章被收录于专栏:用砖头敲代码用砖头敲代码

文件类

文件类型的,如迁移,模型,控制,Service,受/vendor/slowlyo/owl-admin/src/Support/CodeGenerator/BaseGenerator.php控制

代码语言:php
复制
 protected function writeFile($name, $type)
    {
        $name = str_replace('/', '\\', $name);
        $path = static::guessClassFileName($name);
        $dir  = dirname($path);

        $files = app('files');

        if (!is_dir($dir)) {
            $files->makeDirectory($dir, 0755, true);
        }
        // 注释判断即可
        // if ($files->exists($path)) {
        //     abort(Response::HTTP_BAD_REQUEST, "{$type} [{$name}] already exists!");
        // }

        $content = $this->assembly();

        $files->put($path, $content);
        $files->chmod($path, 0777);

        return $path;
    }

数据库

有两处,一处是数据表的判断,另外一处是迁移文件模板,分别在/vendor/slowlyo/owl-admin/src/Support/CodeGenerator/Generator.php/vendor/slowlyo/owl-admin/src/Support/CodeGenerator/stubs/migration.stub

代码语言:php
复制
public function generate($id, $needs = [])
    {
        $record = AdminCodeGenerator::find($id);
        $model  = AdminCodeGenerator::find($id);
        $needs  = collect(filled($needs) ? $needs : $record->needs);

        $successMessage = fn($type, $path) => "<b class='text-success'>{$type} generated successfully!</b><br>{$path}<br><br>";

        $paths   = [];
        $message = '';
        try {
            // Model
            if ($needs->contains('need_model')) {
                $path = ModelGenerator::make($model)->generate();

                $message .= $successMessage('Model', $path);
                $paths[] = $path;
            }

            // Controller
            if ($needs->contains('need_controller')) {
                $path = ControllerGenerator::make($record)->generate();

                $message .= $successMessage('Controller', $path);
                $paths[] = $path;
            }

            // Service
            if ($needs->contains('need_service')) {
                $path = ServiceGenerator::make($record)->generate();

                $message .= $successMessage('Service', $path);
                $paths[] = $path;
            }

            // Route
            RouteGenerator::handle($record->menu_info);

            // Migration
            $migratePath = '';
            if ($needs->contains('need_database_migration')) {
                $path = MigrationGenerator::make($record)->generate();

                $message     .= $successMessage('Migration', $path);
                $migratePath = str_replace(base_path(), '', $path);
                $paths[]     = $path;
            }

            // 创建数据库表
            if ($needs->contains('need_create_table')) {
                // 注释掉这个判断即可
                // if (Schema::hasTable($record->table_name)) {
                //     abort(HttpResponse::HTTP_BAD_REQUEST, "Table [{$record->table_name}] already exists!");
                // }

                if ($migratePath) {
                    Artisan::call('migrate', ['--path' => $migratePath]);
                } else {
                    Artisan::call('migrate');
                }
                $message .= $successMessage('Table', Artisan::output());
            }
        } catch (\Throwable $e) {
            app('files')->delete($paths);

            RouteGenerator::refresh();

            admin_abort($e->getMessage());
        }

        return $message;
    }
代码语言:php
复制
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        // 加一行强制删除
        Schema::dropIfExists('{{ table }}');
        Schema::create('{{ table }}', function (Blueprint $table) {
            {{ content }}
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('{{ table }}');
    }
};

注意事项

本教程Owl Admin版本为v4.0.7,其他版本可能有所不同,建议项目上线时将代码恢复原样。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

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

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文件类
  • 数据库
  • 注意事项
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档